代码之家  ›  专栏  ›  技术社区  ›  Scott Chamberlain

在使用SQLDataAdapter.Update()之前是否需要调用SqlCommandBuilder.GetUpdateCommand()?

  •  5
  • Scott Chamberlain  · 技术社区  · 15 年前

    当我使用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 正确的程序是什么。我知道没有它也能用,但我不知道它在幕后是否有什么特别之处。这是必要的还是货物邪教编程?

    1 回复  |  直到 15 年前
        1
  •  5
  •   jsmith    13 年前

    我在我的测试数据库上做了一些测试,看看如果我不调用 GetInsertCommand()

    由于某些原因,在更新我的第四个表时,它无法正确插入。这让我很困扰,所以我决定做更多的研究,这使我 here

    这告诉我,它可以工作而不调用它,但它更好地调用它。我试着找出原因,解释为什么有时有效,有时无效。但我还没有完全弄清楚。