第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

線程和SQL插入刪除奇怪的問題

線程和SQL插入刪除奇怪的問題

C#
回首憶惘然 2022-06-19 16:50:02
我有一個 Windows 應(yīng)用程序,它調(diào)用服務(wù)然后寫入數(shù)據(jù)庫。在寫結(jié)果之前,我刪除了舊數(shù)據(jù)。Windows 應(yīng)用程序使用每 5 分鐘運(yùn)行一次的計(jì)時器調(diào)用該服務(wù)。問題是:即使我使用刪除,代碼也不會刪除記錄。如果我不使用計(jì)時器調(diào)用它,它就可以工作。我使用事務(wù),使用 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();                    }                }      
查看完整描述

2 回答

?
慕容708150

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個贊

您需要執(zhí)行命令。


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();

    }


查看完整回答
反對 回復(fù) 2022-06-19
?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個贊

您的代碼存在一些問題。您應(yīng)該始終處理一次性物品。通過調(diào)用Dispose()或使用using {}塊,這是首選方式。


1.)您沒有處理您的 SqlCommand 對象您應(yīng)該將它們包裝在 using 語句中以避免此錯誤和可能的內(nèi)存泄漏。


2.) 您沒有處理您的 SqlTransaction 對象您應(yīng)該始終將其包裝在 using 語句中以避免可能的內(nèi)存泄漏。


3.)像其他人已經(jīng)寫過你必須執(zhí)行一個命令。;)


 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();

 }

在此代碼中,缺少執(zhí)行。;) 所以你的代碼應(yīng)該如下所示。(未測試)


 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();         

 }


查看完整回答
反對 回復(fù) 2022-06-19
  • 2 回答
  • 0 關(guān)注
  • 150 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號