Return a value from sql

前端 未结 2 1912
遥遥无期
遥遥无期 2021-01-26 04:24

Im trying move my sql connection to a method in a class file and return a value to check if the username existing in the db and display as text in the page to show others that t

2条回答
  •  借酒劲吻你
    2021-01-26 04:56

    public static bool searchusername(string username)
    {
        connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        using(SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();
    
            bool exists = false;
    
            using (SqlCommand comm = new SqlCommand("select count(*) from Member where UserName = @UserName", conn))
            {
                comm.Parameters.AddWithValue("@username", username);
                exists = (int)comm.ExecuteScalar() > 0;
            }
            return exists;
        }
    

    In your page

    if(MemberDB.searchusername(UNameTxtBox.Text))
    {
        ExistLabel.Text = "UserName exists";
    }
    

提交回复
热议问题