SqlDataAdapter RowUpdating事件似乎突变更新命令

时间:2015-06-29 21:33:10

标签: .net datatable sqldataadapter sqlcommandbuilder

我跟着a tutorial使用.Net SqlDataAdapater.Update方法生成表更新脚本。但是,看起来SqlCommands在某种程度上会以语法错误的方式进行变异。对我来说更奇怪的是,这个问题似乎突然冒出来了。

...
dataAdapter.RowUpdating += (sender, updatingEvent) => AppendToTableDataString(updatingEvent, tableDataStringBuilder);
var cmdBuilder = new SqlCommandBuilder(dataAdapter);
dataAdapter.DeleteCommand = cmdBuilder.GetDeleteCommand();
dataAdapter.InsertCommand = cmdBuilder.GetInsertCommand();
dataAdapter.UpdateCommand = cmdBuilder.GetUpdateCommand();
Console.WriteLine("Before call to Update: " + dataAdapter.UpdateCommand.CommandText);
dataAdapter.Update(DataTable);
Console.WriteLine("After call to Update: " + dataAdapter.UpdateCommand.CommandText);
...

private static void AppendToTableDataString(SqlRowUpdatingEventArgs updatingEvent, StringBuilder tableDataBuilder)
{
  if (updatingEvent.Command == null)
  {
    return;
  }

  Console.WriteLine("Before appending to StringBuilder: " + updatingEvent.Command.CommandText);
            tableDataBuilder.Append(updatingEvent.Command.Parameters.Cast<SqlParameter>()
                                    .Aggregate(updatingEvent.Command.CommandText, (current, aParameter) =>
                                               current.Replace(aParameter.ParameterName, aParameter.Value.ToString())) + Environment.NewLine);
  Console.WriteLine("After appending to StringBuilder: " + updatingEvent.Command.CommandText);
}

输出:

Before call to Update: UPDATE [schema].[table] SET [Id] = @p1, [Num] = @p2 WHERE (([Id] = @p3) AND ((@p4 = 1 AND [Num] IS NULL) OR ([Num] = @p5)))
Before appending to StringBuilder: UPDATE [schema].[table] SET [Id] = @p1, [Num] = @p2 WHERE (([Id] = @p3) AND ((@p4 = 1 AND [Num] IS NULL) OR ([Num] = @p5)))
After appending to StringBuilder: UPDATE [schema].[table] SET [Id] = @p1, [Num] = @p2 WHERE (([Id] = @p3) AND ((@p4 = 1 AND [Num] IS NULL) OR ([Num] = @p5)))
Before appending to StringBuilder: UPDATE [schema].[table] WHERE (([Id] = @p1) AND ((@p2 = 1 AND [Num] IS NULL) OR ([Num] = @p3)))
After appending to StringBuilder: UPDATE [schema].[table] WHERE (([Id] = @p1) AND ((@p2 = 1 AND [Num] IS NULL) OR ([Num] = @p3)))
Before appending to StringBuilder: UPDATE [schema].[table] WHERE (([Id] = @p1) AND ((@p2 = 1 AND [Num] IS NULL) OR ([Num] = @p3)))
After appending to StringBuilder: UPDATE [schema].[table] WHERE (([Id] = @p1) AND ((@p2 = 1 AND [Num] IS NULL) OR ([Num] = @p3)))
After call to Update: UPDATE [schema].[table] WHERE (([Id] = @p1) AND ((@p2 = 1 AND [Num] IS NULL) OR ([Num] = @p3)))

因此,您可以看到问题在于生成了错误的脚本:

UPDATE [schema].[table] WHERE (([Id] = @p1) AND ((@p2 = 1 AND [Num] IS NULL) OR ([Num] = @p3)))

输出使得看起来命令在第一行更新完成后某处变异,但之前第二行更新,但是,我不知道会导致这种情况发生的原因。任何人都可以提供任何线索或发现我做错了什么吗?正如我在上面指出的那样,这样做得很好,然后看起来似乎没有理由。

更新

我完全删除了StringBuilder操作(RowUpdating事件仅包含 打印出更新命令),我仍然看到了奇怪的行为。

然而,我相信我能够通过稍微改变我的代码来缓解这个问题。除此之外,我在设置适配器上的各种命令之后设置了RowUpdating事件。我想我不确定为什么这会影响问题(因此,我不会将此列为答案),但它似乎至少要解决问题。

0 个答案:

没有答案