0

我有以下代码:

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 的更新方法。SqlCommand 可以是使用 adapter.Update() 方法执行的 INSERT、UPDATE 或 DELETE。

该命令成功执行,但 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)));
4

1 回答 1

1

利用

adapter.RowUpdating += new SqlRowUpdatingEventHandler(AdapterOnRowUpdating); 
adapter.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);

代替

adapter.RowUpdated += OnRowUpdated;
adapter.RowUpdating += AdapterOnRowUpdating;
于 2012-06-01T07:59:39.547 回答