久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

重用帶有事務的 SqlCommand 時,我應該調用 Parame

Should I call Parameters.Clear when reusing a SqlCommand with a transation?(重用帶有事務的 SqlCommand 時,我應該調用 Parameters.Clear 嗎?)
本文介紹了重用帶有事務的 SqlCommand 時,我應該調用 Parameters.Clear 嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在 ADO.NET 中手動編寫事務代碼.我正在使用的示例重用了 SqlCommand,這看起來是個好主意.

I'm coding a transaction manually in ADO.NET. The example I'm working from reuses the SqlCommand which seem like a fine idea.

但是,我在命令中添加了參數.

However, I have added parameters to my command.

我的問題是:在下面的代碼中,command.Parameters.Clear() 是否正確?還是我做錯了?

My question is: in the following code, is command.Parameters.Clear() correct? Or am I doing it wrong?

using (var connection = new SqlConnection(EomAppCommon.EomAppSettings.ConnStr))
{
    connection.Open();
    SqlTransaction transaction = connection.BeginTransaction();
    SqlCommand command = connection.CreateCommand();
    command.Transaction = transaction;
    try
    {
        foreach (var itemIDs in this.SelectedItemIds)
        {
            command.CommandText = "UPDATE Item SET payment_method_id = @batchID WHERE id in (@itemIDs)";
            // IS THE FOLLOWING CORRECT?
            command.Parameters.Clear();

            command.Parameters.Add(new SqlParameter("@batchID", batchID));
            command.Parameters.Add(new SqlParameter("@itemIDs", itemIDs));
            command.ExecuteNonQuery();
        }
        transaction.Commit();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Failed to update payment batches, rolling back." + ex.Message);
        try
        {
            transaction.Rollback();
        }
        catch (Exception exRollback)
        {
            if (!(exRollback is InvalidOperationException)) // connection closed or transaction already rolled back on the server.
            {
                MessageBox.Show("Failed to roll back. " + exRollback.Message);
            }
        }
    }
}

推薦答案

由于您重復執行相同的查詢,因此沒有必要清除它們 - 您可以將參數添加到循環外,只需將它們填充到內即可.

Since you're repeatedly executing the same query, it's unnecessary to clear them - you can add the parameters outside the loop and just fill them inside.

try
{
    command.CommandText = "UPDATE Item SET payment_method_id = @batchID WHERE id in (@itemIDs)";
    command.Parameters.Add(new SqlParameter("@batchID", 0));
    command.Parameters.Add(new SqlParameter("@itemIDs", ""));

    foreach (var itemIDs in this.SelectedItemIds)
    {
        command.Parameters["@batchID"].Value = batchID;
        command.Parameters["@itemIDs"].Value = itemIDs;
        command.ExecuteNonQuery();
    }
    transaction.Commit();
}

注意 - 您不能在此處使用帶有 IN 的參數 - 它不會工作.

Note - you can't use parameters with IN as you've got here - it won't work.

這篇關于重用帶有事務的 SqlCommand 時,我應該調用 Parameters.Clear 嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

LINQ to SQL and Concurrency Issues(LINQ to SQL 和并發問題)
Yield return from a try/catch block(try/catch 塊的收益回報)
Does SqlTransaction need to have Dispose called?(SqlTransaction 是否需要調用 Dispose?)
Reason for System.Transactions.TransactionInDoubtException(System.Transactions.TransactionInDoubtException 的原因)
How do I use TransactionScope with MySql and Entity Framework? (getting Multiple simultaneous connections...are not currently supported error)(如何將 TransactionScope 與 MySql 和實體框架一起使用?(獲取多個同時連接...目前不
what does a using statement without variable do when disposing?(處理時不帶變量的 using 語句有什么作用?)
主站蜘蛛池模板: 免费在线看a| 四虎久久| 日韩特级片 | 97在线免费 | 极品美女一区二区三区 | 免费av网址在线观看 | 国产一区二区三区在线视频 | 91成人在线| 一本伊人| 日韩在线视频免费 | 日日干夜夜干 | 欧美黄色片在线观看 | 天天操天天看 | 91欧美激情一区二区三区成人 | 亚洲欧美专区 | 精品国产网站 | 糖心vlog精品一区二区 | 国产成人在线观看免费网站 | 日韩网站免费观看 | 久久久亚洲一区 | 四虎永久网址 | 欧美日韩黄色片 | 激情五月婷婷丁香 | 黄色小视频免费 | 国内精品国产成人国产三级 | 成人网址在线观看 | 午夜久久久久久久 | 久操视频在线观看 | 中文在线字幕免费观看 | 91蜜桃在线观看 | 四虎在线观看 | 国产一区不卡 | 放几个免费的毛片出来看 | 日韩久久久| 黄色免费大片 | 精品影院 | 日韩专区在线观看 | 99久久婷婷国产综合精品草原 | 国产精品毛片一区二区在线看 | 曰本毛茸茸性生活 | 亚洲深夜福利 |