【问题标题】:Unable to insert and check for similar data in database无法在数据库中插入和检查类似数据
【发布时间】:2014-06-08 07:20:45
【问题描述】:

我正在尝试将一个帐户插入数据库。在添加下面的语法之前,我将简要解释我的代码的作用。通常,我正在检查特定文本框是否为空。如果不是,它将尝试将数据插入数据库。但是,如果数据库包含类似于文本框中输入的数据,它们将分别提示不同的错误。不幸的是,对于我来说,我什至无法插入任何数据,也没有错误表明文本框和数据库中的数据是相同的

protected void btnAdd_Click(object sender, EventArgs e)
    {

        if (tbpid.Text.Equals(""))
        {
            lbmsg.Text = "Please generate a police ID for this account";
        }
        else if (tbfullname.Text.Equals(""))
        {
            lbmsg.Text = "Please type the full name of the police officer";
        }
        else if (tbnric.Text.Equals(""))
        {
            lbmsg.Text = "Please enter the NRIC of the police officer";
        }
        else if (ddllocation.SelectedValue.Equals("Select Location"))
        {
            lbmsg.Text = "Please select the location of the policepost he will be posted to";
        }
        else { 

        SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI");
        con.Open();
        SqlCommand select = new SqlCommand("Select policeid, nric from PoliceAccount where policeid = @policeid", con);
        SqlDataReader dr;

select.Parameters.AddWithValue("@policeid", tbpid.Text);

        dr = select.ExecuteReader();
        if (dr.Read())
        {
            if (tbpid.Equals(dr["policeid"].ToString()))
            {

                lbmsg.Text = "Police ID already exists. Please generate another new Police ID";

            }
            else if (tbnric.Equals(dr["nric"].ToString()))
            {
                lbmsg.Text = "NRIC already exists. Please ensure the NRIC is correct";
            }

        }

        else
        {

            SqlConnection conn = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI");
            conn.Open();
            SqlCommand cmd = new SqlCommand("insert into PoliceAccount(policeid, password, nric, fullname, postedto)  values('" + tbpid.Text.Trim() + "','" + tbpid.Text.Trim() + "','" + tbnric.Text.Trim() + "','" + tbfullname.Text.Trim() + "', '" + ddllocation.SelectedValue + "')", conn);
            cmd.ExecuteNonQuery();
            conn.Close();

            Response.Redirect("AdminAddAccount");

        }

        }

    }

请参考本帖[正确答案][1]

【问题讨论】:

  • 构造sql语句时请使用prepared statements。这种方法是等待发生的 SQL 注入灾难
  • 您的选择语句也非常低效。你应该让select语句成为select * from policeAccount where policeid = <PoliceID> or nric = <nric>你现在的方式,你没有让SQL做SQL擅长做的事情
  • 说实话,我键入 SQL 语句语法的方式是我的学校教给我的。我从来没有真正听说过这种叫做准备好的陈述的东西。此外,我正在做一个仅供我使用的项目。

标签: c# asp.net


【解决方案1】:

它永远不会进入 else,插入数据,因为您的 select 语句没有被过滤。本声明:

Select policeid, nric from PoliceAccount

将返回该表中的所有行。然而,你真正想要的是:

Select policeid, nric from PoliceAccount where policeid = @policeid

然后,在执行阅读器之前,添加这行代码:

select.Parameters.AddWithValue("@policeid", tbpid.Text);

最后,在插入语句中使用相同的参数化语法,这样可以避免 SQL 注入。

【讨论】:

  • 不知何故,我能够将数据插入数据库。但是,我对文本框和数据库中类似数据的错误检查不起作用。
【解决方案2】:

解决方案说的是对的。还要在代码中 cmd.ExecuteNonQuery(); 之前添加 cmd.CommandType = CommandType.Text;

另外我认为你应该在这里添加.aspxResponse.Redirect("AdminAddAccount.aspx");

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多