'创建连接对象
set oCon = createObject("adodb.connection")
'连接字符串
oCon.connectionString = "driver={sql server}; server=.;uid=sa;pwd=sa;dataBase=testdb"
'设置游标类型 其它类型会导致结果ors.recordcount返回-1
ocon.CursorLocation =3
'打开连接
oCon.open
'查询语句,可替换为插入 删除 更新语句
strSQL="select * from person"
'执行语句
Set oRs = ocon.Execute(strsql)
'结果执行行数
MsgBox(ors.recordcount)
'循环输出查询结果 非查询可不需要
If oRs.RecordCount > 0 Then
Do While (Not oRs.EOF)
str=""
for each field in ors.fields
str=str&" "&field
Next
MsgBox(str)
oRs.MoveNext
Loop
End If
'关闭查询结果
oRs.Close
Set oRs = Nothing
'关闭连接
oCon.close
MsgBox("close")
VBS 连接sqlserver
于 2023-09-14 11:09:04 首次发布