我是Excel VBA的新手。我已经为IP地址为199.63.106.70的机器编写了现成的代码。现在我希望为另一台机器199.56.122.155运行相同的代码。我在新机器上安装了MS SQL server 2008 R2。我还使用数据连接向导检查连接。数据已被提取。
但是,当我尝试通过单击按钮获取数据时,它会显示错误消息“正在处理错误”。
控制器从oCon.Open
跳出
如何解决这个错误?连接字符串的格式是否正确?
用户ID和密码是用于字符串的Windows登录凭据。
Dim oCon As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim rowcounter As Long
On Error GoTo errhandler
rowcounter = 2
Set oCon = New ADODB.Connection
oCon.connectionstring = "Driver={SQL Server};Server=199.63.106.70;Database=dashboard;User Id=dashboardadmin;Password=passwrd;"
oCon.Open
Set oRS = New ADODB.Recordset
oRS.ActiveConnection = oCon
oRS.Source = "SELECT HourlyReadingTimestamp, Hourlyreading,cost FROM MeterConsumptioNDetail where meterid=" & Range("J5").Value & " and HourlyreadingTimestamp between '" & Range("K5").Value & "' and '" & Range("L5").Value & " 23:59:59' order by HourlyreadingTimestamp"
oRS.Open
While Not oRS.EOF
Range("A" & rowcounter).Value = oRS.Fields(0).Value
Range("B" & rowcounter).Value = oRS.Fields(1).Value
Range("C" & rowcounter).Value = oRS.Fields(2).Value
rowcounter = rowcounter + 1
oRS.MoveNext
Wend
oCon.Close
If Not oRS Is Nothing Then Set oRS = Nothing
If Not oCon Is Nothing Then Set oCon = Nothing
MsgBox ("Data fetched successfully")
Exit Sub
errhandler:
MsgBox ("Error in process!")
End Sub
答案 0 :(得分:0)
如果要使用集成安全性(Windows登录),则连接字符串不正确:
Driver={SQL Server};Server=199.63.106.70;Database=dashboard;Trusted_Connection=Yes;
驱动程序将根据运行该进程的用户处理身份验证。