首页 > 解决方案 > 通过Excel ADODB在sql server中随机“登录超时”

问题描述

用户从 Excel 向 SQL Server 数据库发送数据。现在随机用户收到“登录超时过期”消息。与上周相比,我没有太大变化。它在连接步骤上失败。

我的通用连接代码:

Public Function MyConn() As ADODB.Connection
'Make connection to the DB. Can be called from all macro's
On Error GoTo Message2
Static cnn As ADODB.Connection
Run "BP_Procedures.BP_setparameters"

   Set cnn = New ADODB.Connection
cnn.Open "Provider=SQLOLEDB;Data Source= " & Data_source & _
                 "Initial Catalog= " & Initial_catalog & _
               "Integrated Security=SSPI;"
cnn.CursorLocation = adUseClient


Set MyConn = cnn
Exit Function

Message2:
noVPN = True
Exit Function


End Function

在执行 DB 命令的宏中,我有以下代码:

 'Make connection
Set cn = MyConn()
cn.CommandTimeout = 0

'insert new lines
insertsql = "INSERT INTO [tbl_Proc_Division_Prod] ([Prdl_ID],[Opp_ID],[Product],[Product_pct]) VALUES ('" & ws.Cells(r, 7) & "'," & ws.Cells(r, 8) & ",'" & ws.Cells(r, 9) & "'," & ws.Cells(r, 10) & ")"
cn.Execute (insertsql)

'Close connection to DB
 cn.Close
    Set cn = Nothing
Set cn = CloseConn()

关闭连接vba:

Public Function CloseConn() As ADODB.Connection
'This macro is to close the connection to the DB. Can be called from all macro's
Static cnn As ADODB.Connection


Set cnn = Nothing
Set CloseConn = cnn

End Function

同样在 SQL Server 级别(工具 > 选项 > 查询执行),我已将查询执行超时设置为 0。在 SQL Server Management Studio 中,我看到数据库很好,我可以在其上运行查询。

我还能做什么?这些超时开始的原因是什么?

标签: sql-serverexcelvbasql-server-2016adodb

解决方案


推荐阅读