前端之家收集整理的这篇文章主要介绍了
vb 查询,连接数据库,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Dim Conn As ADODB.Connection Dim Rs As ADODB.Recordset Set Conn = New ADODB.Connection On Error GoTo ErrorSub Conn.ConnectionString = "Driver={
sql Server};Server=192.168.1.1;Database=test;User ID=sa;Password= 123456" Conn.Open On Error GoTo 0 Dim
sqlcount As String
sqlcount = "select * from test where ID ='" & Sheet1.Range("B2").Value & "'" On Error GoTo ErrorSub Set Rs = Conn.Execute(
sqlcount) On Error GoTo 0 Dim i,j As Integer i = 2 Sheet2.Range("A1").Value = "a" Sheet2.Range("B1").Value = "b" Sheet2.Range("C1").Value = "c" Do Until Rs.EOF Sheet2.Range("A" & i).Value = Rs.Fields("ID") Sheet2.Range("B" & i).Value = Rs.Fields("username") Sheet2.Range("C" & i).Value = Rs.Fields("password") i = i + 1 Rs.MoveNext Loop Sheet2.Columns("A:A").EntireColumn.AutoFit Sheet2.Columns("B:B").EntireColumn.AutoFit Sheet2.Columns("C:C").EntireColumn.AutoFit Set Rs = Nothing Conn.Close Exit Sub ErrorSub: Call f_ErrorInfo(Conn) End Sub