使用Visual Basic 2010 oleDB返回select值



我正在尝试用visual basic 2010读取excel单元格(我真的是新手),我想我终于做到了,但我不知道如何返回结果。它应该会出现在剪贴板或消息框中,从那里我会找到我的路:D我找了两个小时都没找到解决办法。。。请帮我

感谢

      Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
    cn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=C:UsersmarcelfDocumentsVisual Studio 2010ProjectsWindowsApplication1WindowsApplication1binDebugDB.xls;extended properties=excel 8.0;"
    cn.Open()
    With cm
        .Connection = cn
        .CommandText = "SELECT * FROM [ccs$C1:B20] WHERE 'id' = 'German'"
        .ExecuteNonQuery()
    End With
    cn.Close()
    MsgBox(????)

End Sub

编辑:她是最新的代码。我得到"找不到可安装的ISAM

        Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
    Dim con As New OleDbConnection
    Try
        Using con
            'added HDR=No to the extended properties of the connection string
            con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;data source=C:UsersmarcelfDocumentsVisual Studio 2010ProjectsWindowsApplication1WindowsApplicat'ion1binDebugDB.xls;extended properties=excel 12.0;HDR=Yes"
            con.Open()
            Using cmd = New OleDbCommand
                cmd.Connection = con
                cmd.CommandText = "SELECT * FROM [ccs$C1:C20] WHERE 'id' = 'German'"
                Using oRDR As OleDbDataReader = cmd.ExecuteReader
                    While (oRDR.Read)
                        MsgBox(oRDR.GetValue(0)) 'gets the first returned column
                    End While
                End Using
                con.Close()
            End Using
        End Using
    Catch ex As Exception
        Throw New Exception(ex.Message)
    Finally
        con.Close()
    End Try
End Sub

编辑:这就是对我有效的:

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
    Dim con As New OleDbConnection
    Try
        Using con
            'added HDR=No to the extended properties of the connection string
            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:UsersmarcelfDocumentsVisual Studio 2010ProjectsWindowsApplication1WindowsApplication1binDebugDB.xls;Mode=3;User ID=Admin;Password=;Extended Properties=Excel 8.0"
            con.Open()
            Using cmd = New OleDbCommand
                cmd.Connection = con
                cmd.CommandText = "SELECT Service FROM [ccs$] WHERE id='" & ComboBox1.SelectedItem & "'"
                Using oRDR As OleDbDataReader = cmd.ExecuteReader
                    While (oRDR.Read)
                        MsgBox(oRDR.GetValue(0)) 'gets the first returned column
                    End While
                End Using
                con.Close()
            End Using
        End Using
    Catch ex As Exception
        Throw New Exception(ex.Message)
    Finally
        con.Close()
    End Try
End Sub

欢迎使用SO。将来您应该向我们展示您的所有代码。由于您标记了OleDB和vb.net,以下内容实现了您想要实现的目标。我包含了Form Class,以便可以显示Imports语句,但您可以在确保Class包含Imports后复制并粘贴click事件代码。注意,我还没有测试过这个代码,但它应该可以工作。如果您需要澄清或其他帮助,请告诉我。

Imports System.Data.OleDb
Public Class Form1
    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Dim con As New OleDbConnection
        Try
            Using con
                'added HDR=No to the extended properties of the connection string
                ' **EDIT**
                con.ConnectionString = "Provider=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:UsersmarcelfDocumentsVisual Studio 2010ProjectsWindowsApplication1WindowsApplicat'ion1binDebugDB.xls;Mode=3;User ID=Admin;Password=;Extended Properties=Excel 8.0"
                con.Open()
                Using cmd = New OleDbCommand
                    cmd.Connection = con
                    cmd.CommandText = "SELECT * FROM [ccs$C1:B20] WHERE 'id' = 'German'"
                    Using oRDR As OleDbDataReader = cmd.ExecuteReader
                        While (oRDR.Read)
                            MsgBox(oRDR.GetValue(0)) 'gets the first returned column
                        End While
                    End Using
                    con.Close()
                End Using
            End Using
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            con.Close()
        End Try
    End Sub
End Class

最新更新