MS SQL + Python (IronPython) timing out
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 (0x80开发者_StackOverflow社区131904): 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?
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.
精彩评论