【问题标题】:select data from Dataset Vb.Net从 Dataset Vb.Net 中选择数据
【发布时间】:2012-12-27 08:50:18
【问题描述】:

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

首先我将一个访问数据库导入到我的 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)

【问题讨论】:

    标签: vb.net dataset


    【解决方案1】:

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

    您要么需要 SQLServer 数据库,要么将 OleDbConnection 用于 Access 数据库。

    这是帮助您连接到 Access 数据库的 Microsoft 知识库文章: 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
    

    【讨论】:

    • 感谢您这解决了我的问题,尽管我没有使用 Jet 作为提供程序,而是使用 Microsoft.ACE.OLEDB.12.0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    相关资源
    最近更新 更多