在我的程序中,我需要使用该if语句检查表中是否已存在数据库中的记录.使用c#我试图通过SQL连接来做到这一点.因为我认为该ExecuteNonQuery();命令返回一个整数值,如果我的假设是真的,我想知道什么值是真的知道表中是否存在某条记录.这是我的代码示例:
using (SqlConnection sqlConnection = dbUtil.GetSqlConnection(dbUtil.GetConnectionStringByName("NonConnectionString")))
{
using (SqlCommand sqlCommand = new SqlCommand("SELECT * from users where user_name like 'Adam' AND password like '123456'", sqlConnection))
{
sqlresult = sqlCommand.ExecuteNonQuery();
}
}
Run Code Online (Sandbox Code Playgroud)
考虑到sqlresult之前已经在main中进行了初始化,int sqlresult;
因此我想知道,如果这个用户'Adam'存在于数据库中,或者不存在.如果他存在,那么我想继续进行'if'语句,例如:
if(sqlresult == 0)
{
MessageBox.Show("Adam exists!");
}
Run Code Online (Sandbox Code Playgroud)
所以我只是不知道它应该返回的整数,我要么不确定这是正确的方法来做到这一点.
谢谢.