我已经看过关于这个主题的其他帖子。然而,到目前为止,还没有解决方案。我正在使用C#中的Visual Studio2013。
我有一个数据库"Database1.mdf“,其中有一个名为Customers的表,其中只有两条记录。我创建了一个名为CustomersDataSet的项目(菜单: DataSet,Add New Data Source...)基于这个数据库。
这是我的代码。
CustomersDataSetTableAdapters.CustomersTableAdapter cta = new CustomersDataSetTableAdapters.CustomersTableAdapter();
CustomersDataSet ds = new CustomersDataSet();
// Fill our customersDataSetTable with the data from customers adapter
cta.Fill(ds.Customers);
Console.WriteLine("BEFORE");
foreach (CustomersDataSet.CustomersRow customer in ds.Customers.Rows)
{
Console.WriteLine(customer.FirstName + " " + customer.LastName);
}
Console.WriteLine("\nMaking changes now...");
// Insert a new record
CustomersDataSet.CustomersRow newCustomer = ds.Customers.NewCustomersRow();
newCustomer.FirstName = "Brian";
newCustomer.LastName = "Faley";
newCustomer.City = "Denver";
newCustomer.State = "CO";
ds.Customers.AddCustomersRow(newCustomer);
// Update a record, [0] = gets access to the first row of the customers table
ds.Customers[0].FirstName = "Robert";
// Delete a record
ds.Customers[1].Delete();
// Update the dataset ds. Commit changes to the database
cta.Update(ds);
Console.WriteLine("\nAFTER");
foreach (CustomersDataSet.CustomersRow customer in ds.Customers.Rows)
{
Console.WriteLine(customer.FirstName + " " + customer.LastName);
}它是有效的,因为我确实看到了在" after“之后对数据集所做的更改。
然而,我可以随心所欲地运行它-永远不会将更改写入底层数据库。更新应该做到这一点,但它没有。我的代码中没有AcceptChanges()。我已经跟进了所有这些建议-它们不会有任何结果。
有人会有个主意吗?
我到处搜索,所有关于这个问题的帖子都没有解决。
发布于 2016-08-17 03:19:10
当您调试应用程序时,mdf文件被复制到bin\debug文件夹,并且您的更改被提交到那里的数据库。
每次启动项目时,调试文件夹中的mdf都会被原始数据库覆盖。
您可以通过转到解决方案中的数据库设置来停止此行为,并将数据库设置为仅当您的版本更新时才进行复制。
你的代码很有可能一直都在工作。
希望这能有所帮助。
https://stackoverflow.com/questions/32008097
复制相似问题