[英]C# MySql Timeout expired
我在C#和INSERT查询上使用MySql.Data引发异常:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)
at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
at MySql.Data.MySqlClient.MySqlConnection.HandleTimeoutOrThreadAbort(Exception ex)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at WarehouseMgr.SyncServiceLib.RequestCreator.CreateRequest(RequestPackage package, MySqlConnection db, ILog log, ConfigFile config, String log_prefix) in z:\WORKSPACE\Web\W_WarehouseMgr\WebApp\SyncService_Library\RequestCreator.cs:line 229
at WarehouseMgr.SyncServiceLib.InputWork.InputWorker.RunWorkOnce() in z:\WORKSPACE\Web\W_WarehouseMgr\WebApp\SyncService_Library\InputWork\InputWorker.cs:line 152
有任何想法吗?
编辑:
插入查询的代码:
var reqId = 0;
var reqSql =
"INSERT INTO `requests` (`uid`, `label_id`, `corridor_id`, `status`) VALUES (@p_uid, @p_label_id, @p_corridor_id, 'waiting');" +
"SELECT `id` FROM `requests` WHERE uid = @p_uid LIMIT 1;";
using (var cmd = new MySqlCommand(reqSql, db)) {
cmd.Parameters.AddWithValue("@p_uid", package.RequestUid);
cmd.Parameters.AddWithValue("@p_label_id", labelId);
cmd.Parameters.AddWithValue("@p_corridor_id", corridorId);
using (var reader = cmd.ExecuteReader()) {
if (reader.Read()) {
reqId = reader.GetInt32("id");
}
}
}
if (reqId <= 0) {
log.E(TAG, log_prefix + "Request creation failed");
throw new Exception("Request creation failed");
}
发出命令时,请设置命令超时。 所以只需添加
cmd.CommandTimeout = xxx;
以秒为单位,您可以根据需要进行设置。 0 =无限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.