1

ログインページを作成しようとしています。

4つの列を持つユーザー用のテーブルがあります。

idユーザー名パスワード管理者

投稿が1つありますが、ログインできません。コードは次のとおりです。

    try
    {


        string strcon = System.Configuration.ConfigurationManager.ConnectionStrings["blogCS"].ToString();
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = strcon;
        myConnection.Open();

        string strSql = "SELECT COUNT(*) FROM users WHERE username=' " + txtUsername + "' AND password='";
        SqlCommand command = new SqlCommand(strSql, myConnection); 
        int count = Convert.ToInt32(command.ExecuteScalar());
        myConnection.Close();

        if (count == 1)
            Response.Redirect("index.aspx");
        else
            lblStatus.Text = Convert.ToString(count);

    }

    catch (Exception k)
    {
        lblStatus.Text = k.Message;
    }
4

2 に答える 2

0

ここに洗練されたコードがあります:

    try
    {


        string strcon = System.Configuration.ConfigurationManager.ConnectionStrings["blogCS"].ToString();
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = strcon;
        myConnection.Open();

        string strSql = "SELECT COUNT(id) FROM users WHERE username=' " + txtUsername + "' AND password='" + txtPassword + "'";
        SqlCommand command = new SqlCommand(strSql, myConnection); 
        int count = Convert.ToInt32(command.ExecuteScalar());
        myConnection.Close();

        if (count == 1)
            Response.Redirect("index.aspx");
        else
            lblStatus.Text = Convert.ToString(count);

    }

    catch (Exception k)
    {
        lblStatus.Text = k.Message;
    }
于 2012-05-29T19:17:52.497 に答える
-1
"SELECT COUNT(*) FROM users WHERE username=' " + txtUsername + "' AND password='";

パスワードを渡す

于 2012-05-29T19:13:46.000 に答える