为什么DataAdapter.Update什么都不更新? [英] Why does DataAdapter.Update update nothing?

查看:139
本文介绍了为什么DataAdapter.Update什么都不更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



在下面的代码中,我在DataTable中添加一行并尝试更新数据库。结果,元素DataTable有添加的行,但数据库中的表不会改变。



你能帮我修复问题吗? 。



Hi!

In the code below I add a row into my DataTable and try to update the database. In result, the element DataTable has the added row, but the table in the database doesn't change.

Would you be so kind to help me with fixing the problem.

string connstr = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|db.mdf;Integrated Security=True";
string command = "SELECT * FROM myTable";
var adapter = new SqlDataAdapter(command, connstr);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
var ds = new DataSet();
try
{
    adapter.Fill(ds);
    DataTable tab = ds.Tables[0];

    var r = tab.NewRow();
    r["name"] = "TestName";
    r["length"] = 1;
    r["Comment"] = "SomeComment";
    tab.Rows.Add(r);              

    adapter.Update(tab);
}
catch (SqlException ex)
{
    Console.WriteLine(ex.Message);
}

Console.Read();





而不是adapter.Update(tab)我也尝试过adapter.Update(ds),但没有什么新东西。



这是我的表。

< img src = http://i.imgur.com/VZ7HYK6.png\">



Instead of adapter.Update(tab) I also tried adapter.Update(ds), but there was nothing new.

Here is my Table.
<img src="http://i.imgur.com/VZ7HYK6.png">

推荐答案

解决方案是(来自StackOverflow论坛的Steve):



这是一种非常常见的情况。您在项目项之间列出了数据库文件,并且它具有属性Copy To output Directory设置为Copy Always。

这意味着每次启动调试会话时,Visual Studio都会复制MDF文件从它的项目位置到BIN \DEBUG文件夹覆盖那里的文件。

当然这个副本没有在上一次运行中添加的最新记录。



所以你的代码工作正常,但你的项目配置会对你的测试造成严重破坏



只需将你的财产改为不复制或复制如果更新
The solution was (by Steve from StackOverflow forum):

This is a pretty common scenario. You have your database file listed between your project items and it has the property Copy To output Directory set to Copy Always.
This means that every time you start a debug session, the Visual Studio copy your MDF file from its project location to the BIN\DEBUG folder overwriting the file present there.
Of course this copy doesn't have the newest record added in the previous run.

So your code is working correctly but the configuration of your project wreak havoc with your tests

Simply change your property to Do Not Copy or Copy if Newer


更新命令只能在你更新数据时才能更新你的数据

1.有更新命令

2.有一些主要的你的桌子上的钥匙 - 在这种情况下自动生成更新命令

检查这些问题...
Update command can update your data only if you
1. have an update command
2. have some primary key on your table - in which case update command generated automatically
Check these issues...


它有权利。

INSERT,UPDATE和DELETE命令由SqlComm提供andBuilder在我的代码中。为了检查其正确性,我调用了构建器的属性:

There is all right with it.
INSERT, UPDATE and DELETE commands are provided by SqlCommandBuilder in my code. To check ones' correctness, I called builder's properties:
builder.GetInsertCommand().CommandText;
builder.GetUpdateCommand().CommandText;
builder.GetDeleteCommand().CommandText;



有值


There were values

INSERT INTO [myTable] ([name], [length], [comment]) VALUES (@p1, @p2, @p3)




UPDATE [myTable] SET [name] = @p1, [length] = @p2, [comment] = @p3 WHERE (([Id] = @p4) AND ([name] = @p5) AND ([length] = @p6) AND ((@p7 = 1 AND [comment] IS NULL) OR ([comment] = @p8)))




DELETE FROM [myTable] WHERE (([Id] = @p1) AND ([name] = @p2) AND ([length] = @p3) AND ((@p4 = 1 AND [comment] IS NULL) OR ([comment] = @p5)))





据我所知,在我的情况下,查询INSERT发生。在我的意见中,它没有任何权利。



至于主键,我的表中有一个。



As fas as I understand, in my case the query INSERT takes place. In my oppinion, there is all right with it.

As for primary key, there is one in my table.


这篇关于为什么DataAdapter.Update什么都不更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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