如何正确设置QueryAsync的OdbcConnection超时?



我试图执行查询ODBC数据库,但当有很多用户试图执行查询时,它返回错误:

ERROR [08003] [Sybase][ODBC Driver]Connection not open: not connected to SQL database
ERROR [08001] [Sybase][ODBC Driver]Unable to connect to database server

下面是查询的例子:

using (OdbcConnection db = new OdbcConnection(DSNs["kxp"]))
{
db.ConnectionTimeout = dbConnectionTimeout;

IEnumerable<KxpWarehouse> productions = (await db.QueryAsync<KXPWarehous>(stmt, new { queryDate }))
.Select(idl => new KxpWarehouse
{
name = idl.name,
stamp = idl.stamp,
queue1 = idl.queue1,
queue2 = idl.queue2,
arrived = idl.arrived,
remains = idl.remains,
total = idl.total,
}).ToList();
List<KxpWarehouse> kxpWarehouseInfo = new List<KxpWarehouse>();
foreach (var item in productions)
{
byte[] bytes = Encoding.GetEncoding(1251).GetBytes(item.name);
item.name = Encoding.GetEncoding(866).GetString(bytes);
bytes = Encoding.GetEncoding(1251).GetBytes(item.stamp);
item.stamp = Encoding.GetEncoding(866).GetString(bytes);
kxpWarehouseInfo.Add(item);
}
return kxpWarehouseInfo;
}

我试图设置OdbcConnection.ConnectionTimeout,但它似乎不是在我的情况下工作,我想我需要设置查询超时

  • OdbcConnection.ConnectionTimeout不影响执行查询的超时时间-仅设置与数据库服务器建立连接的超时时间

增加OdbcCommand对象的CommandTimeout用于执行查询:

using (OdbcConnection db = new OdbcConnection(DSNs["kxp"]))
{
db.ConnectionTimeout = dbConnectionTimeout;
using (OdbcCommand cmd = new OdbcCommand(stmt, db)) // creating the object
{
cmd.CommandTimeout = queryTimeout; // setting query timeout
cmd.Parameters.AddWithValue("queryDate", queryDate); // add query parameters
// Execute asynchronously + fetch results
IEnumerable<KxpWarehouse> productions = (await cmd.ExecuteReaderAsync())
.Select(idl => new KxpWarehouse
{
name = idl.name,
stamp = idl.stamp,
queue1 = idl.queue1,
queue2 = idl.queue2,
arrived = idl.arrived,
remains = idl.remains,
total = idl.total,
}).ToList();
List<KxpWarehouse> kxpWarehouseInfo = new List<KxpWarehouse>();
foreach (var item in productions)
{
byte[] bytes = Encoding.GetEncoding(1251).GetBytes(item.name);
item.name = Encoding.GetEncoding(866).GetString(bytes);
bytes = Encoding.GetEncoding(1251).GetBytes(item.stamp);
item.stamp = Encoding.GetEncoding(866).GetString(bytes);
kxpWarehouseInfo.Add(item);
}
return kxpWarehouseInfo;
}
}

queryTimeout=超时时间

相关内容

  • 没有找到相关文章

最新更新