繁体   English   中英

'='附近的语法不正确。

[英]Incorrect syntax near '='.

我正在尝试使用asp.net和c#在我的网站上显示我的数据,但我收到此错误System.Data.SqlClient.SqlException:'='附近的语法不正确。 请帮助,我已经仔细检查了我的代码,但似乎没有错。

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RDCINFOS"].ConnectionString);
    SqlCommand cmd;
    SqlDataReader dr;
    string str;
    protected void Page_Load(object sender, EventArgs e)
    {  
        SqlCommand cmd =new SqlCommand("select * from articles where id=" + Request.Params["x"], con);
        cmd.CommandType = CommandType.Text;
        con.Open();
        dr= cmd.ExecuteReader();
        while(dr.Read())
        {
            art.InnerHtml +=  "<br>" + dr["title"] + "</br><br>";
            art.InnerHtml +=  dr["details"] + "<br>";
            art.InnerHtml +=  "<img src=pict/" + dr["photo"] + " height=300 width=200/><br>";
        }

    }

首先,检查Request.Params["x"]是否不为空,然后更改

SqlCommand cmd =new SqlCommand("select * from articles where id=" + Request.Params["x"], con);

SqlCommand cmd =new SqlCommand("select * from articles where id=@id", con);
cmd.Parameters.AddWithValue("@id", Request.Params["x"]);

Request.Params["x"]可能为emptynull或数据库中id列以外的other type

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM