TableAdapter插入不保留数据 [英] TableAdapter Insert not persisting data

查看:108
本文介绍了TableAdapter插入不保留数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Visual Studio 2013 Express版创建了基于服务的本地数据库..连接字符串,数据集和TableAdapter已自动添加.

I created a service-based local db via Visual Studio 2013 Express edition.. The connection string, dataset and TableAdapter were added automatically.

单击按钮后,我试图通过调用TableAdapter.Insert插入一些数据 因为已经有一个dataGridView的数据源绑定到该数据集,所以我立即看到该数据在运行时已正确地插入到表中,但是当我关闭该应用程序时,实际的数据库并不包含该数据.因此,实际上并没有将数据插入数据库.

On click of a button, I am trying to insert some data by calling TableAdapter.Insert As it is I already have a dataGridView's datasource bound to the dataset, So I immediately see that the data was inserted in table properly at run time, But when I close the application, The actual DB dosn't contain the data. Therefore, The data isn't actually inserted in DB.

根据 http://msdn. microsoft.com/en-us/library/ms233812%28v=vs.110%29.aspx

对于插入,您只需要调用插入,但是为了安全起见,我在表上调用Update和AcceptChanges,所以我尝试了链接中显示的第一种方法(即创建一行并将其添加到数据集然后调用update),如下所示:很好,但是似乎根本没有将数据插入数据库中.

With insert, you have to only call insert, yet I am calling Update and AcceptChanges on table, for safety, well I tried the first way shown in link (i.e. creating a row and adding it to dataset then calling update) as well, but it seems the data isn't being inserted in DB at all.

最后,插入代码rds是DataSet,ta是TableAdapter

Finally, the insert code, rds is DataSet and ta is TableAdapter

private void AddBtn_Click(object sender, EventArgs e)
{
     ta.Insert("foo", "bar", 2, "zing", "tada");
     ta.Fill(rds.reminders);
     rds.reminders.AcceptChanges();
     ta.Update(rds.reminders);  
}

推荐答案

事实证明,由于每次mdf数据库被覆盖时我都使用VS编译的应用程序,因此我所做的更改被完全擦除了,

It turned out that, as I was using the VS compiled application each time the mdf database was being overwritten, hence the changes I made were completely erased,

可能的解决方案可能是以下其中一种

The possible solution could be one of following

1)更改连接字符串以指向Debug文件夹中的数据库,每次编译和运行应用程序时该数据库都不会被覆盖

1)Change the connectionstring to point to database that is in Debug folder, which wont be overwritten each time you compile and run the application

2)您可以简单地保持连接字符串不变,并通过分离的编译器模式对其进行测试.

2)You could simply let the connectionstring be as it is and just test it through detached compiler mode.

由于下面的stackoverflow链接,我在两天后突然迷失了自己,所以我能够弄清这一点.

I was able to figure this out due to the following stackoverflow link I suddenly sumbled upon after 2 days.

数据库更改不会持续存在在调用ObjectContext.SaveChanges()之后

这篇关于TableAdapter插入不保留数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆