在vb.net中如何查询MDB数据库的数据
1个回答
展开全部
以下是完整模块
Imports System.Data
Imports System.IO
Imports System.Data.OleDb
Module Module1
Public cn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\mdb数据库名字.mdb") '定义连接
Public DataBaseRST As Integer '用来返回数据库执行结果
Public Function DataModify(ByVal str As String) As Boolean '进行数据库修改操作
Dim cmdinsert As New OleDbCommand
Try
cmdinsert.CommandText = str
cmdinsert.Connection = cn
If cn.State = ConnectionState.Closed Then cn.Open()
DataBaseRST = cmdinsert.ExecuteNonQuery() '用来返回执行的结果
cn.Close()
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
Public Function Search(ByVal str As String, ByVal DGV As DataGridView) As Boolean '查询 str---查询命令,DGV---DataGridView,用来显示数据的控件
Dim tb As New DataTable
Try
Dim ap As New OleDb.OleDbDataAdapter(str, cn)
ap.Fill(tb)
DGV.DataSource = tb
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
End Module
'以下是调用方法
DataModify(" insert into aa values ('1','2')")'-------这里是数据库更新操作
Search("select bb from aa",DataGridView1)'-----------这里是数据表查询操作
Imports System.Data
Imports System.IO
Imports System.Data.OleDb
Module Module1
Public cn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\mdb数据库名字.mdb") '定义连接
Public DataBaseRST As Integer '用来返回数据库执行结果
Public Function DataModify(ByVal str As String) As Boolean '进行数据库修改操作
Dim cmdinsert As New OleDbCommand
Try
cmdinsert.CommandText = str
cmdinsert.Connection = cn
If cn.State = ConnectionState.Closed Then cn.Open()
DataBaseRST = cmdinsert.ExecuteNonQuery() '用来返回执行的结果
cn.Close()
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
Public Function Search(ByVal str As String, ByVal DGV As DataGridView) As Boolean '查询 str---查询命令,DGV---DataGridView,用来显示数据的控件
Dim tb As New DataTable
Try
Dim ap As New OleDb.OleDbDataAdapter(str, cn)
ap.Fill(tb)
DGV.DataSource = tb
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
End Module
'以下是调用方法
DataModify(" insert into aa values ('1','2')")'-------这里是数据库更新操作
Search("select bb from aa",DataGridView1)'-----------这里是数据表查询操作
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询