簡體   English   中英

登錄到SQL Server數據庫不起作用

[英]Login to SQL Server database not working

我正在嘗試在C#網頁中創建登錄頁面。 我寫了我認為正確的代碼。 用戶應該鍵入正確的用戶名和密碼。 在成功登錄后再進行任何修改之前,我會暫時將標簽1設置為向我顯示是否正確。 但是,它不起作用,並且每次我嘗試鍵入正確的數據時,它始終顯示為“失敗”。

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ("Data Source=localhost;Initial Catalog=SeminarDB; Integrated security=true;");

        try
        {
            con.Open();
            string str = "select * from Member where Username='" + signintext.Text + "' and Password='" + passwordtext.Text + "'";

            SqlCommand cmd = new SqlCommand(str, con);
            SqlDataReader dr = cmd.ExecuteReader();

            string login = signintext.Text;
            string pwd = passwordtext.Text;

            while (dr.Read())
            {
                if ((dr["Username"].ToString() == login) && (dr["Password"].ToString() == pwd))
                {
                    Label1.Text = "success!";
                    visibl = true;
                }
                else
                {
                     Label1.Text = "failed!";
                }
            }

            dr.Close();
            con.Close();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
}

試試這個,希望它能起作用

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ("Data Source=localhost;Initial Catalog=SeminarDB; Integrated security=true;");

        try
        {
            con.Open();
            string str = "select * from Member where Username='" + signintext.Text + "' and Password='" + passwordtext.Text + "'";

            SqlCommand cmd = new SqlCommand(str, con);
            SqlDataReader dr = cmd.ExecuteReader();
            if(dr.Read())
            {

                 Label1.Text = "success!";
                 visibl = true;
            }
            else
            {
                 Label1.Text = "failed!";
            }    

            con.Close();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
}

試試這個簡單的方法。

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = ("Data Source=localhost;Initial Catalog=SeminarDB; Integrated security=true;");

    try
    {
        con.Open();
        string str = "select count(*) from Member where Username='" + signintext.Text + "' and Password='" + passwordtext.Text + "'";

        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader dr = cmd.ExecuteReader();

        string login = signintext.Text;
        string pwd = passwordtext.Text;

        while (dr.Read())
        {
            if ((dr[0] > 0)
            {
                Label1.Text = "success!"; 
            }
            else
            {
                 Label1.Text = "failed!";
            }
        }

        dr.Close();
        con.Close();
    }
    catch (Exception ex)
    {
        Label1.Text = ex.Message;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM