Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在命令启动后更改sql超时_C#_Mysql_Sql_Multithreading_Executenonquery - Fatal编程技术网

C# 如何在命令启动后更改sql超时

C# 如何在命令启动后更改sql超时,c#,mysql,sql,multithreading,executenonquery,C#,Mysql,Sql,Multithreading,Executenonquery,问题: 有没有办法在命令完成之前停止该命令。ExecuteNonQuery(),而不使用超时 我创建了一个多线程MySQL程序,用于存储和运行sql语句。允许一组事务同时运行(因为它们不修改相同的表)。 我不得不禁用超时(将其设置为0),因为某些SQL语句可能需要几分钟才能运行。 当我想停止查询时,问题就出现了。现在我必须等到当前的SQL语句完成(就像我说的,可能需要几分钟) 根据我现有的知识(帮助他人),以下是有效的代码: MySqlConnectionStringBuilder Connec

问题: 有没有办法在
命令完成之前停止该命令。ExecuteNonQuery()
,而不使用超时

我创建了一个多线程MySQL程序,用于存储和运行sql语句。允许一组事务同时运行(因为它们不修改相同的表)。 我不得不禁用超时(将其设置为0),因为某些SQL语句可能需要几分钟才能运行。
当我想停止查询时,问题就出现了。现在我必须等到当前的SQL语句完成(就像我说的,可能需要几分钟)

根据我现有的知识(帮助他人),以下是有效的代码:

MySqlConnectionStringBuilder ConnectionString=new MySqlConnectionStringBuilder();
ConnectionString.Server=ServerName;//ServerName是用户定义的字符串
ConnectionString.Database=DatabaseName;//DatabaseName是用户定义的字符串
ConnectionString.UserID=UserName;//用户名是用户定义的字符串
如果(!Password.Equals(string.Empty))//密码是用户定义的字符串
{ConnectionString.Password=Password;}//如果密码字符串不是空的,则添加它。
ConnectionString.AllowUserVariables=true;
使用(MySqlConnection=MySqlConnection(ConnectionString))
{
尝试
{
connection.Open();
DBTransaction=connection.BeginTransaction();
使用(MySqlCommand=connection.CreateCommand())
{
foreach(字符串SQLCommandString in SQLCommands)//SQLCommands是用户定义的列表
{
尝试
{
Command.CommandText=SQLCommandString;//SQLCommandString是用户定义的字符串,例如“updatemytable SET MyVar=3,其中id=3;”
NumofRecordInfected=Command.ExecuteOnQuery();
}
catch(MySql.Data.MySqlClient.MySqlException-ex)
{
Trans.RollBack();
//如果代码到达此处,则执行SQLCommandString时出现问题。
掷骰子;
}
捕获(例外情况除外)
{
//除了SQLCommandString之外,还有一个问题。
掷骰子;
}
}
}
Trans.Commit();
}

在单线程应用程序中无法执行此操作,因为在a)查询完成执行或b)异常强制控件返回之前,控件不会返回


相反,您可以启动和执行工作线程中的所有事务,并在需要时关闭与原始线程的连接。调用将回滚所有挂起的事务。

那么此解决方案对您来说应该没有问题:)我想我已经意识到您所说的sI正在返回。请给另一个线程上运行的对象一个引用MySqlConnection。请让该线程关闭连接。我明天上班时必须尝试。谢谢。:@AmeliaCox:差不多,我说的是相反的情况。通过传入connection对象,让事务在另一个工作线程中执行。然后,如果需要手动停止执行,请关闭t从主线程创建连接(它将回滚任何正在运行的事务),并终止工作线程。我发现有时这种方法会起作用。如果(connection.State!=ConnectionState.Closed)connection.CancelQuery(1);connection.Close();我必须运行CancelQuery(1)因此它会提前停止sql。否则,Connection.Close()将等待sql语句完成。(问题-CancelQuery(1)有时只会偶尔起作用。这更好,但仍然是个问题。)我不知道CancelQuery(int)是如何工作的以前工作过,它在智能感知上听起来是正确的。但我确实发现了这可能解释它的原因,不知道它是否正确。
MySqlConnectionStringBuilder ConnectionString = new MySqlConnectionStringBuilder();
ConnectionString.Server = ServerName;  // ServerName is a user defined string
ConnectionString.Database = DatabaseName; // DatabaseName is a user defined string
ConnectionString.UserID = UserName; // UserName is a user defined string
if (!Password.Equals(string.Empty)) // Password is a user defined string
{ ConnectionString.Password = Password; } // If Password string is not empty, then add it.
ConnectionString.AllowUserVariables = true; 

using (MySqlConnection connection = MySqlConnection(ConnectionString))
{
   try
   {
       connection.Open();
      DBTransaction Trans = connection.BeginTransaction();

       using (MySqlCommand Command = connection.CreateCommand())
       {
              foreach(String SQLCommandString in SQLCommands) // SQLCommands is user defined List<String>
              {
                   try
                   {
                          Command.CommandText = SQLCommandString; // SQLCommandString is a user defined string ex "UPDATE MyTable SET MyVar = 3 WHERE id = 3;"
                         NumOfRecordAffected = Command.ExecuteNonQuery();
                   }
                   catch (MySql.Data.MySqlClient.MySqlException ex)
                   {
                         Trans.RollBack();
                         // If code reaches here then there was a problem with the SQLCommandString executing.
                         throw ex;
                   }
                   catch (Exception ex)
                   {
                           // There was a problem other than with the SQLCommandString.
                           throw ex;
                   }
              }
       }
       Trans.Commit();
 }