0

好的,首先我解释一下我做了什么。

首先,我将一个访问数据库导入到我的 vb 应用程序中,然后将其命名为数据集等。

这就是我的数据集的外观:1 表 4 列

到目前为止,我有这个:

Dim ds As ElementsDataSet
    Dim dt As ElementsDataSet.ElementsDataTable
    Dim conn As SqlConnection = New SqlConnection("Data Source=|DataDirectory|\Elements.accdb")
    Dim selectString As String = "Select Atomic Mass FROM Elements WHERE No =" & mol
    Dim cmd As New SqlCommand(selectString, conn)
If conn.State = ConnectionState.Closed Then conn.Open()

Dim datareader As SqlDataReader = cmd.ExecuteReader()

While datareader.Read = True

    MessageBox.Show(datareader.Item("Atomic Mass"))

End While

datareader.Close()

执行此操作后,我收到此错误:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
4

1 回答 1

0

问题是您正在使用 aSQLConnection打开 Access 数据库。这行不通。

您要么需要 SQLServer 数据库,要么需要使用OleDbConnectionAccess 数据库。

这是一篇帮助您连接到 Access 数据库的 Microsoft 知识库文章: 如何使用 ASP.NET、ADO.NET 和 Visual Basic .NET 从 Access 数据库中检索和显示记录,以及CodeProject 上的这篇文章:http:// /www.codeproject.com/Articles/8477/Using-ADO-NET-for-beginners

Private Sub ReadRecords()
    Dim conn As OleDbConnection = Nothing
    Dim reader As OleDbDataReader = Nothing
    Try
        conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Temp\Elements.accdb"))
        conn.Open()

        Dim cmd As New OleDbCommand("Select Atomic Mass FROM Elements WHERE No =" & mol, conn)
        reader = cmd.ExecuteReader()    
        While reader.Read = True
        MessageBox.Show(reader.Item("Atomic Mass"))
        End While
    Finally

        If reader <> Nothing Then
            reader.Close()
        End If
        If conn <> Nothing Then
            conn.Close()
        End If
    End Try
End Sub
于 2013-01-14T05:27:22.273 回答