c# - 选择查询有效,但更新查询在使用 NpgsqlCommand 的点网代码中的 Postgresql 中无效
问题描述
连接 Postgres SQL 后,我正在尝试从 MVC .Net C# 更新用户名。我能够建立连接,如果我运行 select 命令,我就能得到结果。但是当我尝试更新记录时,没有错误出现,但更新的计数为 0。数据库中可用的记录。您能否建议可能是什么原因。
using (NpgsqlConnection con = new NpgsqlConnection(connectionString))
{
string query = string.Format("UPDATE um_user SET um_user_name='{0}' WHERE um_user_name='{1}'", updatedUser, userNameToBeUpdated);
con.Open();
NpgsqlCommand comn = new NpgsqlCommand(query, con);
comn.Connection = con;
updatedRows = comn.ExecuteNonQuery();
comn.Dispose();
con.Close();
}
我还使用以下代码添加了 using 参数,但仍然获得 0 个更新的行。
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = "update um_user set um_user_name=@newName where um_user_name=@oldName";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new NpgsqlParameter("@newName", updatedUser));
cmd.Parameters.Add(new NpgsqlParameter("@oldName", userNameToBeUpdated));
updatedRows = cmd.ExecuteNonQuery();
cmd.Dispose();
connection.Close();
}
解决方案
推荐阅读
- excel - 发送电子邮件时如何显示 MsgBox?Excel VBA
- excel - 如何在工作表中显示即时窗口
- arrays - 如何从对象数组中提取键值
- javascript - React JS中验证不正确时如何更改文本框的颜色
- python-3.x - 在日期之间选择熊猫数据框中的行,无论年份如何
- reactjs - 添加设置状态更改会导致 React 中的无限循环
- java - JniExport 因声明字符串而失败
- java - 在 Quarkus 中创建托管 bean 在构建时失败,但在手动创建时成功
- typescript - 类型安全的深度对象属性访问
- javascript - ::after css元素上的vue @click函数-关闭btn