楼上的说对了,你的SQL语句打错了! try myCommand.CommandText="Update T_Travel_Info set VCHA_City='大连' where VCHA_InfoNum='U1' "; i=myCommand.ExecuteNonQuery(); ////////// i=1;
you can try: myCommand.CommandText="Update T_Travel_Info set VCHA_City='大连' where VCHA_InfoNum='U1' "; try { int i=myCommand.ExecuteNonQuery(); MessageBox.Show(i.ToString(),"影响数据行");} catch(Exception ex) { MessageBox.Show(ex.Message); } ///////////// 如果仍为5的话,说明你的数据库有多行相同条件数据行!
楼上的说的不对吧 You can use the ExecuteNonQuery to perform catalog operations (for example, querying the structure of a database or creating database objects such as tables), or to change the data in a database without using a DataSet by executing UPDATE, INSERT, or DELETE statements.Although the ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are populated with data.For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1.from MSDN
影响一条记录。可在程序中返回是5。不知道为什么。怎么能知道影响几条呢?有没有高手了?
try
myCommand.CommandText="Update T_Travel_Info set VCHA_City='大连' where VCHA_InfoNum='U1' ";
i=myCommand.ExecuteNonQuery();
//////////
i=1;
myCommand.CommandText="Update T_Travel_Info set VCHA_City='大连' where VCHA_InfoNum='U1' ";
try
{
int i=myCommand.ExecuteNonQuery();
MessageBox.Show(i.ToString(),"影响数据行");}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
/////////////
如果仍为5的话,说明你的数据库有多行相同条件数据行!
SqlCommand.ExecuteNonQuery()只能对两种sql语句返回正确的值:
insert、delete,而对于update不能返回正确的值。
You can use the ExecuteNonQuery to perform catalog operations (for example, querying the structure of a database or creating database objects such as tables), or to change the data in a database without using a DataSet by executing UPDATE, INSERT, or DELETE statements.Although the ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are populated with data.For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1.from MSDN
是的,我说错了,对不起,在这里误导了。
我刚才试了一下,发现可以返回值。我想楼主,你的这个sql语句
肯定就是返回5条记录的,你肯定是弄错了,我刚才试了,可以返回
1条和多条。
希望以后,大家都我的错误多多指正,因为我这也是听别人说的,
今天试了一下,发现竟然可以返回,我以前都是用select count
,ExcuteScalar进行返回值判断的。
我的语句也有返回。但返回的数=总记录数+UPDATE影响的记录数,不知道为什么?
我的表里数据如下:
字段名: auto_ID date message
数 据: 1 2003-1-27 OK1
2 2003-1-27 OK2
3 2003-1-27 OK3
4 2003-1-27 OK4
我的第一条sql是:
update table_test set message='OK' where auto_ID=1
测试返回值为:受影响的行数:1
此时,数据库的数据是:
字段名: auto_ID date message
数 据: 1 2003-1-27 OK
2 2003-1-27 OK2
3 2003-1-27 OK3
4 2003-1-27 OK4第二条sql是:
update table_test set message='OK'
测试返回值为:受影响的行数:4
此时,数据库的数据是:
字段名: auto_ID date message
数 据: 1 2003-1-27 OK
2 2003-1-27 OK
3 2003-1-27 OK
4 2003-1-27 OK