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

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"

cnn.CursorLocation = adUseClient

cnn.Open()
rs.Open("Select A.社员CD,A.社员カナ,A.社员名,B.部门名 From 社员 A,部门 B where b.部门cd = a.部门cd", cnn, 3, 3)
Me.DGV社员Show.DataSource = rs
Me.DGV社员Show.Refresh()

为什么就是显示不了数据呢??
最新回答
咸鱼味的猫

2024-06-24 03:33:44

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
夏迟归

2024-06-24 01:57:44

添加一个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