Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未闭合引号错误_C#_Asp.net - Fatal编程技术网

C# 未闭合引号错误

C# 未闭合引号错误,c#,asp.net,C#,Asp.net,我得到一个错误,因为 字符串“192”后未闭合的引号。不准确的 192年附近的语法 我已经检查了我的查询很多次,但我无法发现错误 我的代码片段如下所示:- if (Request.QueryString["id"] != null) { string catid = Request.QueryString["id"].ToString(); Query = "select * from messages where mess

我得到一个错误,因为

字符串“192”后未闭合的引号。不准确的 192年附近的语法

我已经检查了我的查询很多次,但我无法发现错误

我的代码片段如下所示:-

if (Request.QueryString["id"] != null)
        {   

            string catid = Request.QueryString["id"].ToString();
            Query = "select * from messages where messageid ='" + catid + "'";
            adap = new SqlDataAdapter(Query, con);
            ds = new DataSet();
            adap.Fill(ds);
            Repeater2.DataSource = ds;
            Repeater2.DataBind();

        }

请帮忙

尝试使用参数化查询,它将避免错误。在您的情况下,您不应该引用该id,而且您还暴露于SQL注入

if (Request.QueryString["id"] != null)
{
    string conString = "xxxxxxxxxxxxxxxxxxxxxxxx";
    using (SqlConnection con = new SqlConnection(conString))
    {
        con.Open();
        using (var cmd = new SqlCommand(
        "select * from messages where messageid =@catid",
        con))
        {
            cmd.Parameters.AddWithValue("@catid", catid);

            using (SqlDataReader reader = cmd.ExecuteReader())
            {
              //get the data
            } 
        }
    }
}

您不应该通过将字符串合并在一起来生成查询。这个错误消息是一个很好的解释。SQL代码可以通过用户的数据注入到查询中。您应该使用参数化查询。@Servy-将其作为具有正确格式/转义解决方案的答案发布错误消息告诉您问题'192。有三个。catid==192'。只需对代码进行一次简单的调试就可以告诉您需要知道的一切我建议您阅读我建议您研究SQL查询的参数化