啓用TCP/IP
On Error Resume Next
'連接對象
Dim oCon
'數據庫連接字符串
Dim strCon
'結果集對象
Dim oRs
'執行對象
Dim oCmd
'查詢語句
Dim strSQL
'結果集行數
Dim RsCount,IP,Port,PWD,usr,DBName
Set oCon = CreateObject("ADODB.Connection")
strCon = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;PassWord=123456;Initial Catalog=TMP;Data Source=192.168.1.73,1433"
oCon.ConnectionString = strCon
oCon.CursorLocation = 3
oCon.Open
Set oRs = CreateObject("ADODB.Recordset")
Set oCmd = CreateObject("ADODB.Command")
oCmd.CommandType = 1
strSQL = "select * from UserInfo"
With oCmd
.ActiveConnection = oCon
.CommandText = strSQL
End With
'結果集
Set oRs = oCmd.Execute
'獲取查詢條件的結果集
RsCount = oRs.RecordCount
MsgBox RsCount
'斷開結果集
oRs.Close
'清空結果集
Set oRs = Nothing
'斷開數據庫連接
oCon.Close
'清空連接對象
Set oCon = Nothing'斷開結果集