VB中查询Access数据库让结果显示在Datagridview中

2024-12-12 09:53:13
推荐回答(2个)
回答1:

Private myCon As New OleDbConnection
Private mycmd As New OleDbCommand
Private myada As New OleDbDataAdapter
myCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurDir() & "\Jet 4.0.mdb"
myCon.Open()
mycmd.CommandText ="Select A.社员CD,A.社员カナ,A.社员名,B.部门名 From 社员 A,部门 B where b.部门cd = a.部门cd"
mycmd.Connection = myCon
myada.SelectCommand = mycmd
myds.Clear()
myada.Fill(myds, "表名")
Me.DGV社员Show.DataSource = myada
Me.DGV社员Show.Refresh()

我是做C# 用这个VB 很是不习惯,代码条理不清晰,但总的思想是这样的,你自己稍为看下。给点建议:
在控件命名时不要使用中文
数据库尽量不要使用绝对路径OVER

回答2:

添加一个adodc控件
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\qn\My Documents\Visual Studio 2005\Projects\vbexercise\vbexercise\Jet 4.0.mdb"

Adodc1.ConnectionString = CnnStr
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "select A.社员CD,A.社员カナ,A.社员名,B.部门名 From 社员 A,部门 B where b.部门cd = a.部门cd"
adodc1.refresh
Set DataGrid1.DataSource = Adodc1
DataGrid1.Refresh