SqlCommand.ExecuteNonQuery() 方法仅对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于所有其他类型的语句,返回值为 -1。如果发生回滚,返回值也为 -1

SqlCeCommand.ExecuteScalar ()方法执行查询,并返回查询所返回的结果集中第一行的第一列,忽略额外的列或行。如果为聚合查询则返回一个聚合值。

所以在查询表中是否有(某条)数据的时候,一定不能用 cmd.ExecuteNonQuery()通过返回值是否大于0来判断。

解决方案:

1. SqlCeCommand.ExecuteScalar ()方法 中使用 聚合查询

cmd.CommandText = " select count(*) from users where id = sa and password=123;"

int count = (int)cmd.ExecuteScalar() ;

If(count>=1)

       return true;

else

      return false;

2. 用ExcuteReader()方法返回一个reader

cmd.CommandText = " select count(*) from users where id = sa and password=123;"

SqlDataReader dreader = Cmd.ExecuteReader();

if(dreader.Read()==false)

      return false ;

else

     return true ;

相关文章:

  • 2021-07-31
  • 2021-10-29
  • 2021-09-02
  • 2022-02-05
  • 2022-12-23
  • 2021-11-20
  • 2021-11-16
  • 2021-05-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-27
  • 2022-12-23
  • 2021-11-27
  • 2021-06-26
相关资源
相似解决方案