我正在使用多线程应用程序从数据库中提取数据,运行几分钟后,我开始收到此异常或 sql 超时异常。我的代码如下:
public static List<FlightInfo> FlightInfoAllPricesPerDayForRoute(string From, string To)
{ // Create the object array from the datareader
List<FlightInfo> arr = new List<FlightInfo>();
using (var conn = new SqlConnection(sConnectionString))
{
using (var command = new SqlCommand("FlightInfoAllPricesPerDayForRoute", conn)
{
CommandType = CommandType.StoredProcedure
})
{
conn.Open(); command.CommandTimeout = 150;
command.Parameters.AddWithValue("AirportFrom", From);
command.Parameters.AddWithValue("AirportTo", To);
// command.Parameters.AddWithValue("Date", dt);
//execute the stored procedure
using (IDataReader dr = command.ExecuteReader())
{
FlightInfo obj;
while (dr.Read())
{
obj = new FlightInfo();
obj.flightInfoID = (int)dr["FlightInfoID"];
obj.airportFrom = (string)dr["AirportFrom"];
obj.airportTo = (string)dr["AirportTo"];
obj.timeDeparture = DateTime.Parse(dr["TimeDeparture"].ToString());
obj.timeArrival = (DateTime)dr["TimeArrival"];
obj.price = (short)Int16.Parse(dr["Price"].ToString());
obj.dateAdded = (DateTime)dr["DateAdded"];
obj.carrier = (string)dr["Carrier"];
arr.Add(obj);
}
dr.Close(); dr.Dispose(); conn.Close(); conn.Dispose(); command.Dispose();
}
}
} return arr;
}
我在连接字符串中添加了以下内容以改善超时问题,但仍然出现错误:
Min Pool Size=200;Max Pool Size=32767;Asynchronous Processing=true; Connect Timeout=150;
存储过程非常简单,所以我认为那里没有问题:
select *, DATEADD(dd, 0, DATEDIFF(dd, 0, TimeDeparture)) as FlightDate
from FlightInfo
where AirportFrom = @AirportFrom and AirportTo = @AirportTo
AND TimeDeparture > GETDATE() -- Only get future flights
Order by FlightDate ASC, Price asc
我也尝试运行它来检查 sql 连接,但我得到一个“用户没有执行此操作的权限。” 错误
SELECT * FROM sys.dm_exec_connections
任何建议,将不胜感激!