关键词搜索

源码搜索 ×
×

vb.net 如何连接Access数据库

发布2021-01-19浏览1703次

详情内容

Access数据库vb.net教程
连接
采用OleDbConnection对c#教程

代码 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

    PS:
    Access数据库文件,可以通过 My Project -> 资源 -> 添加资源 -> 添加python基础教程现有文件 这样的方式来添加
    调试状态下,每次运行项目,Resources下面的数据库将会覆盖Debug下的数据库

    本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
    本文链接:https://blog.csdn.net/sdlin/article/detailshttps://files.jxasp.com/image/2192347

    相关技术文章

    点击QQ咨询
    开通会员
    返回顶部
    ×
    微信扫码支付
    微信扫码支付
    确定支付下载
    请使用微信描二维码支付
    ×

    提示信息

    ×

    选择支付方式

    • 微信支付
    • 支付宝付款
    确定支付下载