我有以下代码:
DataSet dataSet = new DataSet();
bool result;
using (SqlConnection connection = new SqlConnection(command.Connection.ConnectionString))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(command);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.RowUpdated += OnRowUpdated;
adapter.RowUpdating += AdapterOnRowUpdating;
adapter.Fill(dataSet);
// Code to modify data in the DataSet here.
// Without the SqlCommandBuilder, this line would fail.
adapter.UpdateCommand = builder.GetUpdateCommand();
int i = adapter.Update(dataSet);
result = i > 0;
}
return result;
此代码假设采用我的SqlCommand对象并将其提交给SqlDataAdapter的update方法。 SqlCommand可以是INSERT,UPDATE或DELETE,它使用adapter.Update()方法执行。
该命令成功执行,但adapter.Update()方法返回0行。我检查了数据库,我可以看到它成功了。 RowUpdated和RowsUpdating事件也没有触发。我不知道最近发生了什么。请帮忙。
提前谢谢你。
修改
public void OnRowUpdated(object sender, SqlRowUpdatedEventArgs sqlRowUpdatedEventArgs)
{
switch (sqlRowUpdatedEventArgs.StatementType)
{
case StatementType.Insert:
case StatementType.Update:
case StatementType.Delete:
case StatementType.Batch:
if (sqlRowUpdatedEventArgs.Status != UpdateStatus.ErrorsOccurred)
SqlReporting.LogSqlCommand(sqlRowUpdatedEventArgs.Command);
break;
}
}
修改 我使用以下查询插入:
INSERT INTO Users
(Username, Firstname, Lastname, RoleId, ReceiveRoleAlerts, StatusId, Password, Fingerprint, AuthenticityCheck, CreatedByUserId, CreationDate, ModifiedByUserId,
ModificationDate)
SELECT @Username AS Expr1, @Firstname AS Expr2, @Lastname AS Expr3, @RoleId AS Expr4, @ReceiveRoleAlerts AS Expr5, @StatusId AS Expr6, @Password AS Expr7,
@Fingerprint AS Expr8, @AuthenticityCheck AS Expr9, @CreatedByUserId AS Expr10, @CreationDate AS Expr11, @ModifiedByUserId AS Expr12,
@ModificationDate AS Expr13
WHERE (NOT EXISTS
(SELECT Username, Firstname, Lastname, RoleId, ReceiveRoleAlerts, StatusId, Password, Fingerprint, AuthenticityCheck, CreatedByUserId, CreationDate,
ModifiedByUserId, ModificationDate
FROM Users AS Users_1
WHERE (Username = @Username)));
答案 0 :(得分:1)
使用
adapter.RowUpdating += new SqlRowUpdatingEventHandler(AdapterOnRowUpdating);
adapter.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);
而不是
adapter.RowUpdated += OnRowUpdated;
adapter.RowUpdating += AdapterOnRowUpdating;