我正在尝试学习如何从excel数据库连接到sqlserverdb。我试着从一开始就把代码简化为非常简单的代码。我已经看了几个相关问题的答案,但是,我不明白为什么这不起作用。它一直执行(此处显示的代码有点匿名。)
查询将查找数据库,因为如果表名无效,它将抛出一个错误。但是,它总是返回record count=-1。我可以在mssms中查看表格,它有数据。db中其他表的结果相同。
Public Sub ADOtest1()
Dim Conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConnString As String
strConnString = "Provider='SQLOLEDB'" & ";" & _
"Data Source='XXX-XPS\SQLEXPRESS'" & ";" & _
"Initial Catalog='XXXXX'" & ";" & _
"Integrated Security='SSPI'"
Set Conn = New ADODB.Connection
Conn.Open strConnString
' the query finds the DB, because if the table name is incorrect, it throws an error
strSQLString = "SELECT * from t300_XXXX"
Set rs = Conn.Execute(strSQLString)
wrkRecordCount = rs.RecordCount
'--- just some test breakpoints
If wrkRecordCount = -1 Then
a = "" '--- code keeps arriving here
Else
a = ""
End If
rs.Close
Conn.Close
End Sub
斯里尼卡的回答如下:
Set rs = Conn.Execute(strSQLString)
rs.Close
rs.CursorLocation = adUseClient
rs.Open
1条答案
按热度按时间b5buobof1#
我将张贴两个例子,所以请参考。
第一个例子
第二个例子