VB通过ADO连接SQL后,如何将查询后数据显示到DataGrid中
在图中缺席天数中输出数值查找SQL数据,连接已好,就是怎么将查询的结果输出到名为“DgdShow”的datagrid中,不使用ADODC插件已有源码:DimconnStr...
在图中缺席天数中输出数值查找SQL数据,连接已好,就是怎么将查询的结果输出到名为“DgdShow”的datagrid中,不使用ADODC插件
已有源码:
Dim connString As String
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
------------------------------------------------------------------------------------------------------------------
Private Sub ConnectDB()
connString = "driver={SQL Server};" & "server=localhost;uid=sa;pwd=sa;database=vb"
If conn.State <> 1 Then
conn.Open (connString)
End If
End Sub
------------------------------------------------------------------------------------------------------------------
Private Sub CmdFind_Click()
sql = "Select * From wage where absent=" & txtAbsent.Text
Set rs = conn.Execute(sql)
rs.MoveFirst
End Sub
------------------------------------------------------------------------------------------------------------------
Private Sub Form_Load()
ConnectDB
End Sub
------------------------------------------------------------------------------------------------------------------
Private Sub form_Unload(Cancel As Integer)
conn.Close
End Sub 展开
已有源码:
Dim connString As String
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
------------------------------------------------------------------------------------------------------------------
Private Sub ConnectDB()
connString = "driver={SQL Server};" & "server=localhost;uid=sa;pwd=sa;database=vb"
If conn.State <> 1 Then
conn.Open (connString)
End If
End Sub
------------------------------------------------------------------------------------------------------------------
Private Sub CmdFind_Click()
sql = "Select * From wage where absent=" & txtAbsent.Text
Set rs = conn.Execute(sql)
rs.MoveFirst
End Sub
------------------------------------------------------------------------------------------------------------------
Private Sub Form_Load()
ConnectDB
End Sub
------------------------------------------------------------------------------------------------------------------
Private Sub form_Unload(Cancel As Integer)
conn.Close
End Sub 展开
1个回答
展开全部
Set DgdShow.DataSource = Rs '设置绑定控件
Dim n As Integer, m As Integer
n = Rs.Fields.Count
m = DgdShow.Columns.Count
For i = m + 1 To n '添加行补足数据库列
DgdShow.Columns.Add i
Next
'绑定列名
For i = 1 To DgdShow.Columns.Count
DgdShow.Columns(i).DataField = Rs.Fields(i).Name
Next
Dim n As Integer, m As Integer
n = Rs.Fields.Count
m = DgdShow.Columns.Count
For i = m + 1 To n '添加行补足数据库列
DgdShow.Columns.Add i
Next
'绑定列名
For i = 1 To DgdShow.Columns.Count
DgdShow.Columns(i).DataField = Rs.Fields(i).Name
Next
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询