Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未将更改保存回数据库_C#_Asp.net - Fatal编程技术网

C# 未将更改保存回数据库

C# 未将更改保存回数据库,c#,asp.net,C#,Asp.net,我正在尝试连接到我的NorthwindDataSet,当用户单击按钮时,我希望数据保存回数据库。当我运行下面的代码时,我没有收到任何错误,但是数据没有保存。我认为我正确地连接到了数据集,不确定为什么这不能保存回来 NorthwindDataSetTableAdapters.CustomersTableAdapter north = new NorthwindDataSetTableAdapters.CustomersTableAdapter(); NorthwindDataSet.Custome

我正在尝试连接到我的NorthwindDataSet,当用户单击按钮时,我希望数据保存回数据库。当我运行下面的代码时,我没有收到任何错误,但是数据没有保存。我认为我正确地连接到了数据集,不确定为什么这不能保存回来

NorthwindDataSetTableAdapters.CustomersTableAdapter north = new NorthwindDataSetTableAdapters.CustomersTableAdapter();
NorthwindDataSet.CustomersDataTable northtable = north.GetData();

NorthwindDataSet northwindDataSet1 = new NorthwindDataSet();
NorthwindDataSet.CustomersRow newCustomersRow =
northwindDataSet1.Customers.NewCustomersRow();

newCustomersRow.CustomerID = "5";
newCustomersRow.CompanyName = "Alfreds Futterkiste";

northwindDataSet1.Customers.Rows.Add(newCustomersRow);

northwindDataSet1.Customers.AcceptChanges();

您不仅需要通过AcceptChanges方法提交更改,还需要在表适配器上使用Update方法

在您的情况下,它将如下所示:

north.update(northwindDataSet1.Customers);
northwindDataSet1.Customers.AcceptChanges();

接受更改不向数据库提交数据。更新有。

我已经有一段时间没有使用这些东西了,但是您的数据集和DataAdapter之间有什么联系?我希望CustomersTableAdapter实例通过update()或自定义方法进行更新。您不需要在TableAdapter上调用update方法吗?看起来您正在尝试设置主键,因此默认情况下框架会阻止您这样做(因为PK应该自动生成)。我认为您必须更改INSERT查询和参数。当然,考虑到DB中没有定义标识字段,这将再次阻止您在DB级别更新PK列。