MS SQL + Python (IronPython) 超时
我正在使用 python 查询 MS SQL,使用 http://www.ironpython.info 的源代码/index.php/Accessing_SQL_Server:
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,那就完全没问题。你知道如何避免这种超时异常吗?
I'm querying MS SQL using python using the source code from http://www.ironpython.info/index.php/Accessing_SQL_Server:
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()
I just added timeout=0
, but still I got :
EnvironmentError: System.Data.SqlClient.SqlException (0x80131904): Timeout
expired. The timeout period elapsed prior to completion of the operation
or the server is not responding.
I tried it with timeout=1000000
, but still got the same error.
If I run the same SQL in the same machine using the MSSQL Client, it's totally fine. Do you know how to avoid this timeout exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试增加 SqlCommand 上的 CommandTimeout 属性,如下所述:
https:// msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout(v=vs.110).aspx
连接字符串中的超时值仅控制初始连接的超时时间。数据库。如果您的 SQL 查询需要很长时间才能执行,那么这将无济于事,因此您需要使用 CommandTimeout。
Try increasing the CommandTimeout property on the SqlCommand as described here:
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout(v=vs.110).aspx
The timeout value in the connection string only controls the timeout for the initial connection to the database. That will not help if your SQL query takes a long time to execute so you need to use CommandTimeout instead.