[英]SqlDataAdapter.Update(dataset) method inserts new rows but does not updates existing rows
I am creating winform application that have DataGrigView to present a table. 我正在创建具有DataGrigView来呈现表的Winform应用程序。 I have a DAL class that is responsible to work with DB.
我有一个DAL类,负责与DB一起工作。
There are one method that loads data of the table: 有一种加载表数据的方法:
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;
}
}
And second method that is responsible to update the DB with the changes that were made in the table: 第二种方法负责使用表中所做的更改来更新数据库:
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;
}
}
The problem: If in the Items table new rows were aded or deleted - UpdateItemsByOrder add/delete the rows in the DB as expected. 问题:如果在Items表中添加或删除了新行-UpdateItemsByOrder按预期方式添加/删除了数据库中的行。 But updates in existing rows of the Items table does not updated in DB.
但是,Items表的现有行中的更新在DB中不会更新。
There are no error or exceptions. 没有错误或异常。 I have tryed to add builder.GetUpdateCommand() command = no result.
我试图添加builder.GetUpdateCommand()命令=没有结果。
I will be happy to get any help or advice. 我将很高兴获得任何帮助或建议。 Thanks
谢谢
P>S> I am using this MSDN LINK to learn how to work with SQLAdapter P> S>我正在使用此MSDN LINK学习如何使用SQLAdapter
Ok, with the advice of sallushan I got the solution: The reason why DataAdapter doesn't updated the DB is that updated rows in DataTable has RowState value "Unchanged" instead "Modified". 好的,在sallushan的建议下,我得到了解决方案:DataAdapter不更新DB的原因是,DataTable中的更新行的RowState值为“ Unchanged”而不是“ Modified”。
There is 2 basic ways to resolve this problem: 有两种解决此问题的基本方法:
Thanks to all for a help :-) 感谢所有人的帮助:-)
You do realize that you run a SELECT command instead of update right? 您确实意识到您运行的是SELECT命令而不是更新吗? My guess is
adapter.Update
just does select and then reports that no lines where updated since none were. 我的猜测是
adapter.Update
只是选择,然后报告没有行更新,因为没有行被更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.