Check if a record exists in the database

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

    try this

     public static bool CheckUserData(string phone, string config)
        {
            string sql = @"SELECT * FROM AspNetUsers WHERE PhoneNumber = @PhoneNumber";
            using (SqlConnection conn = new SqlConnection(config)
                )
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@PhoneNumber", phone);
                    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    if (reader.HasRows)
                    {
                        return true;  // data exist
                    }
                    else
                    {
                        return false; //data not exist
                    }
                }
            }
        }
    

提交回复
热议问题