Check if a record exists in the database

前端 未结 13 1720
借酒劲吻你
借酒劲吻你 2020-11-28 12:58

I am using these lines of code to check if the record exists or not.

SqlCommand check_User_Name = new SqlCommand(\"SELECT * FROM Table WHERE ([user] = \'\" +         


        
13条回答
  •  爱一瞬间的悲伤
    2020-11-28 13:18

    I would use the "count" for having always an integer as a result

    SqlCommand check_User_Name = new SqlCommand("SELECT count([user]) FROM Table WHERE ([user] = '" + txtBox_UserName.Text + "') " , conn);
    
    int UserExist = (int)check_User_Name.ExecuteScalar();
    
    if (UserExist == 1) //anything different from 1 should be wrong
    {
      //Username Exist
    }
    

提交回复
热议问题