线程和SQL插入删除奇怪的问题

时间:2018-12-19 20:05:22

标签: c# sql multithreading

我有一个Windows应用程序,该应用程序调用服务,然后写入数据库。在写入结果之前,我先删除旧数据。

Windows应用程序使用每5分钟运行一次的计时器来调用该服务。

问题是:即使我使用的是删除,代码也不会删除记录。如果我不使用计时器就可以调用它。

我使用了交易,使用EF无效。它仍然不会删除记录。

 static void Main(string[] args)
 {
        //////Start Timer
        Timer timer = new Timer();
        timer.Interval = 5 * 60 * 1000; // converts ms to minutes
        timer.Elapsed += new ElapsedEventHandler(InsertRecords);
        timer.Enabled = true;
        timerFired.WaitOne();        
 }

 public void InsertRecords()
 { 
     using (SqlConnection connection = new SqlConnection(connectionstr))
     {
         connection.Open();

         // Delete old entries
         SqlTransaction trans = connection.BeginTransaction();
         string sql = "Delete  from PhilaMethod ";

         SqlCommand cmd = new SqlCommand(sql, connection, trans);
         trans.Commit();
         connection.Close();
     }

     var conn = connectionstr;

     string SQL1 = "";

     foreach (PhilaMethod phila in phila2)
     {
         SQL1 += "INSERT INTO PhilaMethod(Name,PS1,PS2,RSI1,) values ('" + phila.Name + "','"+phila.PS1+"','"+phila.PS2+"','"+phila.RSI1+"','"+phila.RSI2+"'); ";
     }

     string SQL2 = "Delete  from philamethod";

     using (SqlConnection connection = new SqlConnection(conn))
     {
            connection.Open();
            SqlTransaction sqlTran = connection.BeginTransaction();
            SqlCommand command = connection.CreateCommand();
            command.Transaction = sqlTran;

            try
            {
                command.CommandText = SQL2;

                int rowsAffected = command.ExecuteNonQuery();

                if(rowsAffected >= 0)
                {
                    command.CommandText = SQL2;
                    int rowsAffected2 = command.ExecuteNonQuery();

                    if(rowsAffected2 == 0)
                    {
                        command.CommandText = SQL1;
                        rowsAffected += command.ExecuteNonQuery();
                    }
                }

                sqlTran.Commit();
            }
            catch (Exception ex1)
            {
                // Attempt to roll back the transaction.
                try
                {
                    sqlTran.Rollback();
                }
                catch (Exception ex2)
                {
                    // This catch block will handle any errors that may have occurred
                    // on the server that would cause the rollback to fail, such as
                    // a closed connection.
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您需要执行命令。

using (SqlConnection connection = new SqlConnection(connectionstr))
    {
        connection.Open();
        // Delete old entries
        SqlTransaction trans = connection.BeginTransaction();
        string sql = "Delete  from PhilaMethod ";

        SqlCommand cmd = new SqlCommand(sql, connection, trans);
        cmd.ExecuteNonQuery(); // <--- added this
        trans.Commit();
        connection.Close();
    }

答案 1 :(得分:0)

您的代码存在一些问题。您应始终丢弃一次性物品。可以通过调用Dispose()或使用using {}块来实现。

1。)您没有处置SqlCommand对象 您应该将它们包装在using语句中,以避免此错误和可能的内存泄漏。

2。)您没有处置SqlTransaction对象 您应该始终将其包装在using语句中,以避免可能的内存泄漏。

3。)像其他已经写过的一样,您必须执行命令。 ;)

 using (SqlConnection connection = new SqlConnection(connectionstr))
 {
     connection.Open();

     // Delete old entries
     SqlTransaction trans = connection.BeginTransaction();
     string sql = "Delete  from PhilaMethod ";

     SqlCommand cmd = new SqlCommand(sql, connection, trans);
     trans.Commit();
     connection.Close();
 }

此代码中缺少执行。 ;) 因此,您的代码应如下所示。 (未测试)

 using (SqlConnection connection = new SqlConnection(connectionstr))
 {
     connection.Open();
     // Delete old entries
     using (SqlTransaction trans = connection.BeginTransaction())
     {
       string sql = "Delete  from PhilaMethod ";
       using (SqlCommand cmd = new SqlCommand(sql, connection, trans))
       {
         cmd.ExecuteNonQuery(); // or whatever method you need
       }
       trans.Commit();
     }
     connection.Close();         
 }