登录到SQL Server数据库不起作用

时间:2015-03-21 10:53:33

标签: c# sql sql-server

我正在尝试在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;
        }
}

2 个答案:

答案 0 :(得分:0)

试试这个,希望它有效

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;
        }
}

答案 1 :(得分:0)

试试这个简单的方法..

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;
    }

}