首页 > 解决方案 > 选择查询有效,但更新查询在使用 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();
                }

标签: c#asp.netpostgresql

解决方案


推荐阅读