VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • vb.net 如何连接Access数据库

Access数据库连接
采用OleDbConnection对象


代码 1

  Private Function getAccessConnection() As OleDbConnection

      Dim dbConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Resources/mag.mdb;Persist Security Info=true"
      Dim dbConnection As OleDbConnection = New OleDbConnection(dbConnectionString)

        Try
          dbConnection.Open()
        Catch Ex As Exception
          'MsgBox(Err.Description)
        End Try

      Return dbConnection

  End Function

代码 2

  private Sub fillDataGridView()

      Dim sqlStr As String = "select * from Table"
      Dim DataAdapter As New OleDbDataAdapter
      Dim dataSet As New DataSet
      Dim DataGridView As New DataGridView
      Dim dbConnection As OleDbConnection = getAccessConnection()

        If dbConnection.State.ToString = "Closed" Then
          MsgBox(Chr(13) & " access 数据库连接失败 " & Chr(13), , "警告")
          Exit Sub
        End If

        DataAdapter.SelectCommand = New OleDbCommand(sqlStr,dbConnection)

        Try
          DataAdapter.Fill(dataSet,"Table")
          DataGridView.DataSource = dataSet.Tables("Table").DefaultView
        Catch Ex As Exception
          MsgBox(Err.Description)
        Finally
          dataSet.Dispose
          DataAdapter.Dispose
          dbConnection.Close
          dbConnection.Dispose
        End Try

  End Sub




相关教程