创建ASP.net数据库寄存器连接时出错

时间:2014-10-01 20:47:03

标签: c# sql asp.net

我试图犯下以下错误:类型' System.Data.SqlClient.SqlException'发生在System.Data.dll中但未在用户代码中处理

其他信息:关键字'表'附近的语法不正确。

    protected void Button_Login_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegisterConnectionString"].ConnectionString);
        conn.Open();
        string checkuser = "select count(*) from [Table] where Användarnamn='" + TextBoxAnvändarelogin.Text + "'";
        SqlCommand com = new SqlCommand(checkuser, conn);
        int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
        conn.Close();
        if (temp == 1)
        {
            conn.Open();
            string checkPasswordQuery = "select password from Table where Användarnamn='" + TextBoxAnvändarelogin.Text + "'";
            SqlCommand passComm = new SqlCommand(checkPasswordQuery, conn);
            string password = passComm.ExecuteScalar().ToString().Replace(" " , ""); // ERROR HERE!
            conn.Close();
            if (password==TextBoxLösenordlogin.Text)
            {
                Session["New"] = TextBoxAnvändarelogin.Text;
                Response.Write("Lösenord är rätt!");
                Response.Redirect("Admin.aspx");
            }
            else
            {
                Response.Write("Lösenord är fel!");
            }
        }
        else
        {
            Response.Write("Användarnamn är inte rätt!");
        }

    }
}

1 个答案:

答案 0 :(得分:0)

string checkPasswordQuery = "select password from Table where Användarnamn
应该是  string checkPasswordQuery = "select password from [Table] where Användarnamn

旁注:使用string append构建动态sql绝不是一个好习惯。有关详细信息,请参阅Sql Injection。并且不应该使用"表"作为表名