ExecuteNonQuery update does not store change

ぐ巨炮叔叔 提交于 2021-01-29 02:08:42

问题


I am trying to update some fields in the database, using ExecuteNonQuery().

When trying to update, i receive no error or warning, and it returns me one affected row count.

The thing is, that i can only for the current request, see the change. If i reload the page or go through SMSS, the changes are not shown in the database.

Hes a little code.

    string Sql = "UPDATE AccessUser SET [" + Field + "] = '" + Database.SqlEscapeInjection(Value) + "' WHERE AccessUserID = " + User.ID;
    Response.Write("<br>" + Sql + "<br>");

    IDataReader Se = Database.CreateDataReader("SELECT * FROM AccessUser WHERE AccessUserID = " + User.ID);
    while (Se.Read())
    {
        Response.Write((string)Se[Field] + "<br>");
    }

    int affectedrows = Database.ExecuteNonQuery(Sql, Database.CreateConnection());

    Response.Write("<br>NUMMER: " + affectedrows + "<br>");
    IDataReader S = Database.CreateDataReader("SELECT * FROM AccessUser WHERE AccessUserID = " + User.ID);
   while (S.Read())
   {
       Response.Write((string)S[Field] + "<br>");
   }

As you see, i first output the current value, afterwards i run the update query, and afterwards again output the value. ( Must notify you, that i usually do use bound parameters, but for the sake of debugging i threw them away and made this. )

The result is as follow: Jonas NUMMER: 1 Mikkel

When i refresh the page, or look in the database, the row hasn't changed though. What to do? :D

EDIT: The documentation for the system "Dynamicweb-CMS" states that its not transactional: http://developer.dynamicweb-cms.com/api8/Dynamicweb~Dynamicweb.Database~ExecuteNonQuery(String,IDbConnection,IDbTransaction).html And that i should just be able to do, as i actually do. Weird stuff.

EDIT 2: To please those with a "bound parameters OCD"

    using (IDbConnection connection = Database.CreateConnection())
    {
        IDbCommand command = connection.CreateCommand();
        command.CommandText = "UPDATE AccessUser SET [" + Field + "] = @ParamValue WHERE AccessUserID = @UserId";
        Database.AddStringParam(command, "@ParamValue", Value);
        Database.AddInt32Param(command, "@UserId", User.ID);

        //Execute command
        command.ExecuteNonQuery();
        connection.Close();
    }

Same result as with the other query method.

Tried this as well. Nothing happens.

   using (SqlConnection cn = new SqlConnection(SQLString))
        {
            SqlTransaction Trans;
            try
            {

                cn.Open();
                Trans = cn.BeginTransaction();
                IDbCommand command = cn.CreateCommand();
                command.Transaction = Trans;
                command.CommandText = "UPDATE AccessUser SET [" + Field + "] = @ParamValue WHERE AccessUserID = @UserId";
                Database.AddStringParam(command, "@ParamValue", Value);
                Database.AddInt32Param(command, "@UserId", User.ID);

                //Execute command
                int affectedRecords = command.ExecuteNonQuery();
                Response.Write("<br>NUMMER: " + affectedRecords + "<br>");
                Response.Write(cn.ServerVersion );
                Trans.Commit();
                cn.Close();
            }
            catch (Exception e)
            {
                Response.Write("<span style='color:red;'>" + e.Message + "</span>");
            }
        };

I ran the profiler and see the update statement. After the update statement, the following is sent

Audit Login:
-- network protocol: LPC
set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read committed

Which, in my logic, tells me that the change is committed. Now, however, afterwards, i get this (Had to cut it down, Stackoverflow did not like it)

RPC:Completed:
exec sp_executesql N'UPDATE [AccessUser] SET [AccessUserUpdatedOn] = @p1 WHERE (([AccessUserID] = @p2) AND ((@p3 = 1 AND [AccessUserParentID] IS NULL) OR ([AccessUserParentID] = @p4)) AND ((@p5 = 1 AND [AccessUserUserName] IS NULL) OR ([AccessUserUserName] = @p6)) AND ((@p7 = 1 AND [AccessUserPassword] IS NULL) OR ([AccessUserPassword] = @p8)) AND ((@p9 = 1 AND [AccessUserName] IS NULL) OR ([AccessUserName] = @p10)) AND ((@p11 = 1 AND [AccessUserDepartment] IS NULL) OR ([AccessUserDepartment] = @p12)) AND ((@p13 = 1 AND [AccessUserEmail] IS NULL) OR ([AccessUserEmail] = @p14)) AND ((@p15 = 1 AND [AccessUserPhone] IS NULL) OR ([AccessUserPhone] = @p16)) AND ((@p17 = 1 AND [AccessUserFax] IS NULL) OR ([AccessUserFax] = @p18)) AND ((@p19 = 1 AND [AccessUserGroups] IS NULL) OR ([AccessUserGroups] = @p20)) AND ((@p21 = 1 AND [AccessUserType] IS NULL) OR ([AccessUserType] = @p22)) AND ((@p23 = 1 AND [AccessUserValidFrom] IS NULL) OR ([AccessUserValidFrom] = @p24)) AND ((@p25 = 1 AND [AccessUserValidTo] IS NULL) OR ([AccessUserValidTo] = @p26)) AND ((@p27 = 1 AND 

-and updates the content back to the original again. How would i check if this is some sort of transactional rollback? Or anything else. Pardon the English.


回答1:


You're probably using transactions. When you're using transactions, you have to commit the transaction when you're done with it, otherwise it will rollback automatically - so, in your case, you do the update, no errors. Then you check using the same SQL connection, and everything looks fine. Then at some point you leave the transaction scope, and the transaction gets rolled back automatically, reverting the change in the DB.



来源:https://stackoverflow.com/questions/21628242/executenonquery-update-does-not-store-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!