的ExecuteNonQuery返回1,即使更新语句不影响任何行 [英] ExecuteNonQuery returns 1 even if update statement didn't affect any row

查看:138
本文介绍了的ExecuteNonQuery返回1,即使更新语句不影响任何行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里面临的一个很奇怪的问题。

我的DAL是使用 OdbcConnection 对象编写,并完美地工作。

不过,我必须尊重一些要求,因此必须将系统使用的MySqlConnection

不应该给任何问题,你会说。

不过,有一点误会,现在:当我执行更新命令,不输入任何新的细节(比方说我改变用户测试的用户名为...测试),在 command.ExecuteNonQuery()返回1​​无妨。

随着previous系统和放大器; 的OdbcCommand 的对象,它返回0,如果没有现场更改。

难道是两个系统之间只是一个基本的差别或有什么我已经在这里错过了吗?

只是一些code哪怕是最基本的:

 私人只读字符串_updateUserCommand =
            更新用户U+
            加盟同城C对c.Name =?市+
            SET`市ID` = c.Id,u.Username = username,其中u.Id = ID?;

//(...)

的MySqlCommand命令= NULL;
                尝试
                {
                    connection.Open();
                    //第一步:存储在表用户的用户
                    //创建实际的命令:
                    命令=新的MySqlCommand(_updateUserCommand,连接);
                    command.Parameters.AddWithValue(?城市,u.City);
                    command.Parameters.AddWithValue(,u.Name用户名?);
                    command.Parameters.AddWithValue(?ID,u.Id);

                    INT I = command.ExecuteNonQuery();
                    如果(我!= 0)返回true;
                    否则返回false;
                }
 

解决方案

你的解释并没有太大的意义。如果你给一个有效的身份证件,并做用户名的更新,即使你更新到相同的名称,你可以预期,1行会受到影响。即有一排与用户ID

I am facing a quite strange problem here.

My DAL was written using OdbcConnection objects and was perfectly working.

However I had to respect some requirements and therefore had to move the system to use MySqlConnection

Shouldn't give any problem, would you say.

However, there is a little misunderstanding now: when I execute an UPDATE command, without entering any new detail (let's say I change the user "test"'s username to... "test"), the command.ExecuteNonQuery() returns 1 anyway.

With the previous system & OdbcCommand objects, it returned 0 if no field changed.

Is it just a basic difference between the two systems or is there anything I've missed here?

Just some code even if it is very basic:

private readonly string _updateUserCommand =
            "UPDATE user u " +
            "JOIN city c ON c.Name=?City " +
            "SET `City Id`=c.Id, u.Username=?Username WHERE u.Id=?Id";

// (...)

MySqlCommand command = null;
                try
                {
                    connection.Open();
                    //First step: storing the user in table user
                    //Creating the actual command:
                    command = new MySqlCommand(_updateUserCommand, connection);
                    command.Parameters.AddWithValue("?City", u.City);
                    command.Parameters.AddWithValue("?Username", u.Name);
                    command.Parameters.AddWithValue("?Id", u.Id);

                    int i = command.ExecuteNonQuery(); 
                    if (i != 0) return true;
                    else return false;
                }

解决方案

Your explanation doesn't make much sense. If you give a valid id and do an update on the username even if you update to the same name you can expect that 1 row will be affected. i.e. there is one row with the userId

这篇关于的ExecuteNonQuery返回1,即使更新语句不影响任何行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆