【问题标题】:MS SQL + Python (IronPython) timing outMS SQL + Python (IronPython) 超时
【发布时间】:2010-06-18 20:11:42
【问题描述】:

我正在使用来自http://www.ironpython.info/index.php/Accessing_SQL_Server 的源代码使用python 查询MS SQL:

import clr
clr.AddReference('System.Data')
from System.Data import *

TheConnection = SqlClient.SqlConnection
("server=yourserver;database=News;uid=sa;password=password;timeout=0")
TheConnection.Open()

MyAction = SqlClient.SqlCommand("Select Headline from News", TheConnection)
MyReader = MyAction.ExecuteReader()

while MyReader.Read():
    print MyReader[0]

MyReader.Close()
TheConnection.Close()

我刚刚添加了timeout=0,但还是得到了:

EnvironmentError: System.Data.SqlClient.SqlException (0x80131904): Timeout 
expired.  The timeout period elapsed prior to completion of the operation 
or the server is not responding.

我用timeout=1000000 尝试过,但仍然遇到同样的错误。

如果我使用 MSSQL 客户端在同一台机器上运行相同的 SQL,那就完全没问题了。你知道如何避免这个超时异常吗?

【问题讨论】:

    标签: python sql-server ironpython pymssql


    【解决方案1】:

    尝试增加 SqlCommand 上的 CommandTimeout 属性,如下所述: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout(v=vs.110).aspx

    连接字符串中的超时值仅控制初始连接到数据库的超时时间。如果您的 SQL 查询需要很长时间才能执行,这将无济于事,因此您需要改用 CommandTimeout。

    【讨论】:

      猜你喜欢
      • 2014-04-14
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多