【问题标题】:ASP.net - "INCORRECT SYNTAX NEAR "=""ASP.net - “错误的语法靠近“=”
【发布时间】:2020-08-17 20:36:42
【问题描述】:

我正在尝试使用 ASP.NET 使用来自两个不同表的值登录,但我收到此错误:

“=”附近的语法不正确

代码:

protected void Button1_Click1(object sender, EventArgs e)
{
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("Select Company_Master.Company_Code ='" + TextBox1.Text + "',Employee_Master.Emp_ID ='" + TextBox2.Text + "',Employee_Master.Emp_Pass ='" + Password1.Text + "' from Company_Master, Employee_Master where Company_Master.Company_Code = Employee_Master.Company_Code;");

            cmd.Connection = conn;
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();

            sda.Fill(dt);

            cmd.ExecuteNonQuery();                

            sda.Fill(ds, "Company_Master,Employee_Master");

            if (ds.Tables[0].Rows.Count > 0)
            {
                Label1.Text = "sign up done";
            }
            else
            {
                Label1.Text = "wrong login";
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
}

【问题讨论】:

  • 嗨。阅读有关 SQL 注入的信息:w3schools.com/sql/sql_injection.asp。它不会解决这个问题,但它会在未来防止许多其他更严重的问题。
  • 过滤器应该在 where 子句中,而不是在 select 中。请使用参数来防止各种问题。并使用明确的“加入”子句
  • 另外,你为什么要执行你的查询三次sda.Fill(dt);,然后是 cmd.ExecuteNonQuery();(这对于 返回数据SELECT 查询完全没有意义),然后第三次使用 sda.Fill(ds, "Company_Master,Employee_Master"); ..... 一次 .Fill() 相当就够了!

标签: sql asp.net sql-server


【解决方案1】:

是否有人告诉您,您允许攻击者侵入您的 db 并获取任何信息或删除他们想要的任何内容?没有?

我必须说 Praveen,您应该始终使用参数化查询或尝试使用 Stored Procedures 从您的数据库中读取/写入/删除任何内容。

此外,您在那里的查询没有任何意义,所以,我假设您要选择选择 company_code 等于某事,employee id 等于某事,password 等于某事.

这是应该如何完成的,记住SqlInjections

protected void Button1_Click1(object sender, EventArgs e)
{
        var dt = new DataTable();
        try
        {
            using(SqlCommand cmd = new SqlCommand("Select * from Company_Master cm join Employee_Master em on cm.Company_Code = em.Company_Code where cm.Company_Code = @companyCode and em.Emp_ID = @employeeId and em.Emp_Pass = @password", conn))
            {

                cmd.Parameters.AddWithValue("@companyCode", TextBox1.Text);
                cmd.Parameters.AddWithValue("@employeeId", TextBox2.Text);
                cmd.Parameters.AddWithValue("@password", Password1.Text);

                conn.Open();

                var sdr = cmd.ExecuteReader();
                dt.Load(sdr);

                if (dt.Rows.Count > 0)
                {
                    Label1.Text = "sign up done";
                }
                else
                {
                    Label1.Text = "wrong login";
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
}

注意,我已修改您的查询以支持 sql join,并且我已将查询修改为 using 块中的参数化查询。另外,数据集已修改为 DataTable,因为我注意到您没有获取多个表,而是引用了数据集的第零个索引 Tables

【讨论】:

    【解决方案2】:
    String mycon = "Data Source =EL-0\\SQLEXPRESS; Initial Catalog=dbsBookManagementSystemAshnu;user id=sa;password=loncok#08";
                String myquery = "select bk_bookid,bk_bookname,bk_author,bk_bookdescription,bk_price,bk_specialprice from bmsa_books where bk_bookid = " + Request.QueryString["bk_bookid"];
                SqlConnection con = new SqlConnection(mycon);
                SqlCommand cmd = new SqlCommand
                {
                    CommandText = myquery,
                    Connection = con
                };
                SqlDataAdapter da = new SqlDataAdapter
                {
                    SelectCommand = cmd
                };
                DataSet ds=new DataSet();
                da.Fill(ds,"bmsa_books");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    lblBookID.Text = ds.Tables[0].Rows[0]["bk_bookid"].ToString();
                    lblBookName.Text = ds.Tables[0].Rows[0]["bk_bookname"].ToString();
                    lblAuthor.Text = ds.Tables[0].Rows[0]["bk_author"].ToString();
                    lblDescription.Text = ds.Tables[0].Rows[0]["bk_bookdescription"].ToString();
                    lblPrice.Text = ds.Tables[0].Rows[0]["bk_price"].ToString();
                    lblSpecialPrice.Text = ds.Tables[0].Rows[0]["bk_specialprice"].ToString();
                }
                con.Close();
    }
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 需要格式化
    猜你喜欢
    • 2016-06-18
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多