Vb.net 使用Visual Basic 2010 oleDB返回选择值

Vb.net 使用Visual Basic 2010 oleDB返回选择值,vb.net,excel,oledb,Vb.net,Excel,Oledb,我正在尝试用VisualBasic2010阅读excel单元格(我对这一点非常陌生),我想我终于做到了,但我不知道如何返回结果。 它应该在剪贴板或msgBox中结束,从那里我会找到我的方法:D 我搜索了两个小时,但没有找到答案。。。请帮帮我 谢谢 Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click cn.Con

我正在尝试用VisualBasic2010阅读excel单元格(我对这一点非常陌生),我想我终于做到了,但我不知道如何返回结果。 它应该在剪贴板或msgBox中结束,从那里我会找到我的方法: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:\Users\marcelf\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\DB.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:\Users\marcelf\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplicat'ion1\bin\Debug\DB.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:\Users\marcelf\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\DB.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类,以便可以显示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:\Users\marcelf\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplicat'ion1\bin\Debug\DB.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

您好,谢谢您的帮助,这很有意义,但我收到一个错误:“找不到可安装的ISAM。”指向“抛出新异常(例如Message)”您需要使用“Provider=Microsoft.ACE.OLEDB.12.0”“而不是ole。我编辑了我的答案。您可能还需要更改HDR=Yes。我相信您的问题在于连接字符串。我用一个在.xls文件中对我有用的文件替换了它。如果这不起作用,您必须搜索包含错误消息的线程。它起作用了!谢谢你,罗恩,你是个明星!!很高兴听到这个消息。因此,提问者通常会接受正确的答案。