我正在尝试使用 ExecuteNonQuery() 更新数据库中的某些字段。
尝试更新时,我没有收到任何错误或警告,它返回一个受影响的行数。
问题是,我只能针对当前请求,查看更改。如果我重新加载页面或通过 SMSS,数据库中不会显示更改。
他是一个小代码。
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>");
}
如您所见,我首先输出当前值,然后运行更新查询,然后再次输出该值。(必须通知你,我通常确实使用绑定参数,但为了调试,我把它们扔掉并做了这个。)
结果如下: Jonas NUMMER: 1 Mikkel
当我刷新页面或查看数据库时,该行并没有改变。该怎么办?:D
编辑:系统“Dynamicweb-CMS”的文档指出它不是事务性的:http: //developer.dynamicweb-cms.com/api8/Dynamicweb~Dynamicweb.Database~ExecuteNonQuery (String , IDbConnection,IDbTransaction).html我应该能够做到,就像我实际上所做的那样。奇怪的东西。
编辑2:为了取悦那些有“绑定参数强迫症”的人
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();
}
与其他查询方法的结果相同。
这个也试过了。没发生什么事。
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>");
}
};
我运行分析器并查看更新语句。更新语句后,发送如下
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
在我的逻辑中,这告诉我更改已提交。现在,但是,之后,我得到了这个(不得不削减它,Stackoverflow 不喜欢它)
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
- 并再次将内容更新回原始内容。我将如何检查这是否是某种事务回滚?或者别的什么。原谅英语。