簡體   English   中英

SqlDataAdapter.Update(dataset)方法插入新行,但不更新現有行

[英]SqlDataAdapter.Update(dataset) method inserts new rows but does not updates existing rows

我正在創建具有DataGrigView來呈現表的Winform應用程序。 我有一個DAL類,負責與DB一起工作。

有一種加載表數據的方法:

 public static void GetItemsByOrder(int orderId, ref DataSet dataSet)
    {
        string queryString = @"Select Id,OrderId as [מס' הזמנה],ItemCode as[מק""ט], ItemName as [שם פריט], ReceiptDate as [ת. הספקה],
                WarrantyExpDate as [באחריות עד],SuppliersItemCode as [מק""ט ספק], Supplier as [ספק], Count as[כמות], Active 
                FROM OrdersManager_Items where OrderId = @param";

        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Parameters.AddWithValue("@param", orderId);

        SqlDataAdapter adapter = new SqlDataAdapter(command);


        try
        {
            lock (myLock)
            {
                adapter.Fill(dataSet,"Items");
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLogEntry(LogWriter.LogType.ERROR, string.Format("Failed to get Items by OrderId code from DB."+
                "This is due to exception: {0},\n StackTrace: {1}. ", ex.Message, ex.StackTrace));

            dataSet = null;
        }
    }

第二種方法負責使用表中所做的更改來更新數據庫:

public static bool UpdateItemsByOrder(int orderId, DataSet data)
    {

        string queryString = @"Select Id,OrderId as [מס' הזמנה],ItemCode as[מק""ט], ItemName as [שם פריט], ReceiptDate as [ת. הספקה],
                WarrantyExpDate as [באחריות עד],SuppliersItemCode as [מק""ט ספק], Supplier as [ספק], Count as[כמות], Active 
                FROM OrdersManager_Items where OrderId = @param";


        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Parameters.AddWithValue("@param", orderId);

        SqlDataAdapter adapter = new SqlDataAdapter(command);


        try
        {
            lock (myLock)
            {

                SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
                int rowsUpdated = adapter.Update(data,"Items");

                return true;
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLogEntry(LogWriter.LogType.ERROR, string.Format("Failed to update Items table in DB. This is due to exception: {0},\n StackTrace: {1}. ", ex.Message, ex.StackTrace));

            return false;
        }
    }

問題:如果在Items表中添加或刪除了新行-UpdateItemsByOrder按預期方式添加/刪除了數據庫中的行。 但是,Items表的現有行中的更新在DB中不會更新。

沒有錯誤或異常。 我試圖添加builder.GetUpdateCommand()命令=沒有結果。

我將很高興獲得任何幫助或建議。 謝謝

P> S>我正在使用此MSDN LINK學習如何使用SQLAdapter

好的,在sallushan的建議下,我得到了解決方案:DataAdapter不更新DB的原因是,DataTable中的更新行的RowState值為“ Unchanged”而不是“ Modified”。

有兩種解決此問題的基本方法:

  1. 直接在DataTable中而不是DGV中更新數據
  2. 調用DataTable.Rows [indexOfUpdatedRowInDGV] .EndEdit()方法,通過DGV進行更新后,如所描述的在這里

感謝所有人的幫助:-)

您確實意識到您運行的是SELECT命令而不是更新嗎? 我的猜測是adapter.Update只是選擇,然后報告沒有行更新,因為沒有行被更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM