Check if a record exists in the database

前端 未结 13 1707
借酒劲吻你
借酒劲吻你 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:37

    sqlConnection.Open();
    using (var sqlCommand = new SqlCommand("SELECT COUNT(*) FROM Table WHERE ([user] = '" + txtBox_UserName.Text + "'", sqlConnection))
    {
    
        SqlDataReader reader = sqlCommand.ExecuteReader();
        if (reader.HasRows)
        {
            lblMessage.Text ="Record Already Exists.";
    
        }
        else
        {
            lblMessage.Text ="Record Not Exists.";
        }
    
        reader.Close();
        reader.Dispose();
    }
    
    sqlConnection.Close();
    

提交回复
热议问题