2013-01-13 110 views
0

好的,先生解释一下我做了什么。从数据集Vb.Net中选择数据

首先,我导入的Access数据库到我的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) 

回答

0

问题是您正在使用SQLConnection来打开Access数据库。这不会工作。

您可能需要SQLServer数据库或将OleDbConnection用于Access数据库。

下面是一个Microsoft知识库文章,以帮助您连接到Access数据库: How To Retrieve and Display Records from an Access Database by Using ASP.NET, ADO.NET, and Visual Basic .NET而这其中超过在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 
+0

感谢你这个固定的,虽然不是使用Jet作为供应商的IM我的问题使用Microsoft.ACE.OLEDB.12.0 – Ciaran