0

我正在创建具有 DataGrigView 来呈现表格的 winform 应用程序。我有一个负责使用 DB 的 DAL 类。

有一种方法可以加载表的数据:

 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

4

2 回答 2

1

好的,在 sallushan 的建议下,我得到了解决方案:DataAdapter 不更新数据库的原因是 DataTable 中更新的行的 RowState 值为“未更改”而不是“已修改”。

有两种基本方法可以解决此问题:

  1. 直接在 DataTable 中而不是在 DGV 中更新数据
  2. 在通过 DGV 进行更新后,调用 DataTable.Rows[indexOfUpdatedRowInDGV].EndEdit() 方法,如此处所述

感谢大家的帮助:-)

于 2014-06-02T13:39:53.373 回答
0

您确实意识到您运行的是 SELECT 命令而不是更新,对吗?我的猜测adapter.Update只是选择然后报告没有更新的行,因为没有更新。

于 2014-06-01T15:01:00.040 回答