VBA ADO connection timeout in different version of libraries

梦想的初衷 提交于 2019-12-13 05:18:41

问题


I'm trying to connect with views on SQL Server 2012 using VBA (Excel 2007) and ADO connection object. Everything works perfect except one view, for which Runtime error occours even for simplest queries. To set maximum time out of connection I use command YourConnection.ConnectionTimeout = 0. Does different versions of Microsoft ActiveX Data Objects x.x Library (2.0, 2.1,...,2.8,6.1,6.2) have different maximum timeouts, or all have 30 secunds maximum ? In addition - what are difference between 2.0,...,2.8 versions of that library - where could I find it out ?


回答1:


ConnectionTimeout only applies to connection establishing. If your queries timeout during execution, you should use Command object with CommandTimeout instead. Beware, you have to set it on each Command object as it is not inherited from Connection object:

Dim cnn As New ADODB.Connection
cnn.ConnectionString = "Provider=SQLOLEDB;Data Source=xxx;Catalog=xxx;User ID=xxx;Password=xxx;"
cnn.Open

Dim cmd As New ADODB.Command
Set cmd.ActiveConnection = cnn
cmd.CommandTimeout = 120
cmd.CommandText = "SELECT * FROM [Table]"

Dim rs As New ADODB.RecordSet
rs.Open cmd, , adOpenStatic, adLockOptimistic

Check MSDN for more info and MDAC/ADO history.



来源:https://stackoverflow.com/questions/24142139/vba-ado-connection-timeout-in-different-version-of-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!