gpt4 book ai didi

c# - 在使用 SqlDataAdpater.Update() 之前是否需要调用 SqlCommandBuilder.GetUpdateCommand()?

转载 作者:太空狗 更新时间:2023-10-29 23:19:01 26 4
gpt4 key购买 nike

当我使用 SqlCommandBuilder 将更新/插入/删除推送到服务器时,我是否需要调用 .GetUpdateCommand().GetInsertCommand().GetDeleteCommand()?

using (var adapter = new SqlDataAdapter("select * from MyTable", _connection))
using (var builder = new SqlCommandBuilder(adapter))
{
adapter.Fill(dt);

//Magic happens

builder.GetUpdateCommand(); //is this line necessary
builder.GetInsertCommand(); //is this line necessary
adapter.Update(dt);
}

我看过conflicting examples什么是正确的程序。我知道没有它它也能工作,但我不知道它是否在幕后做了一些特别的事情。这是必要的还是 cargo 崇拜编程?

最佳答案

我对我的测试数据库做了一些测试,看看如果我不调用 GetInsertCommand() 是否会出现错误的场景。事实证明它可能会发生,就我而言,它发生在我更新数据库中的几个不同表时。

由于某种原因,在更新我的第 4 个表时,它无法正确插入。这让我很困扰,所以我决定做更多的研究,这给了我 here .密切关注:

After the Transact-SQL statement is first generated, the application must explicitly call RefreshSchema if it changes the statement in any way. Otherwise, the GetInsertCommand will still be using information from the previous statement, which might not be correct.

这告诉我它可以在不调用它的情况下工作,但最好调用它。我试图找出为什么它有时有效,有时却无效的原因。但我一直没能完全弄明白。

关于c# - 在使用 SqlDataAdpater.Update() 之前是否需要调用 SqlCommandBuilder.GetUpdateCommand()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3119253/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com