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

时间:2017-03-07 07:23:06

标签: c# asp.net

我正在尝试使用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>";
        }

    }

2 个答案:

答案 0 :(得分:2)

首先,检查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"]);

答案 1 :(得分:0)

Request.Params["x"]可能emptynullother type而不是数据库中的id列