VB.NET连接Office Access数据库显示列表数据

前端之家收集整理的这篇文章主要介绍了VB.NET连接Office Access数据库显示列表数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

VB.NET连接Access数据库显示数据列表

        Try
            Dim Apppath As String = Application.StartupPath '获取启动了应用程序的可执行文件的路径,不包括可执行文件名称
            Dim DatabasePath As String = Apppath & "\MyTest.mdb;" '数据库文件总路径名
            Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & DatabasePath & _
                "Persist Security Info=False"  '连接字符串
            'Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\AccessDB1\bin\Debug\MyTest.mdb;;Persist Security Info=False"

            Dim sqlStr As String = "select * FROM TestInfo  where 1=1 "
            If Not String.IsNullOrEmpty(Trim(Me.txtName.Text)) Then '带条件查询
                sqlStr &= " and XH like '%" & Trim(Me.txtName.Text) & "%' "
            End If

            Dim OleDbConn As OleDbConnection = New OleDbConnection(ConnString)
            OleDbConn.Open()
            Dim MyDataSet As DataSet = New DataSet()
            Dim MyOleDataAdapter As OleDbDataAdapter = New OleDbDataAdapter()
            MyOleDataAdapter.SelectCommand = New OleDbCommand(sqlStr,OleDbConn)
            MyOleDataAdapter.Fill(MyDataSet)

            Me.Dg.DataSource = MyDataSet.Tables(0) '绑定DataGrid数据源
            MyOleDataAdapter.Dispose()
            MyDataSet.Dispose()
            OleDbConn.Close()
            OleDbConn.Dispose()

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
代码的下载

VB.NET连接Office Access数据库显示列表数据

猜你在找的VB相关文章