特殊字符不会显示在带有参数的 SQL 数据库的网格视图中



好吧,我已经尝试了你们所有人的建议,但问题仍然是一样的

让我简要地告诉你:

表格名称 : 奥津论坛.aspx

具有 1 个 GridView,它使用 QF 中的选择 * 显示数据库中的所有字段

它工作良好,索引也工作,网格视图有按钮和点击我打开新的表格Auzine论坛答案.aspx..还行

我想从 AuzineForum 中挑选一条记录.aspx并在 AuzineForumAnswer 上显示.aspx就像它发生在 http://stackoverflow.com 中一样(我们单击线程然后打开新页面,其中包含我们之前单击的问题和答案)......还行

所以在AuzineForum.aspx的按钮上,代码是

Button lb = (Button)sender;
            GridViewRow row = (GridViewRow)lb.NamingContainer;
            if (row != null)
            {
                int index = row.RowIndex; //gets the row index selected

                Label AID1 = (Label)ForumQuesView.Rows[index].FindControl("AID1");
               Label AID2 = (Label)ForumQuesView.Rows[index].FindControl("AID2");
               Label AID3 = (Label)ForumQuesView.Rows[index].FindControl("AID3");
               HyperLink Question = (HyperLink)ForumQuesView.Rows[index].FindControl("Question");
               Label Questiontags = (Label)ForumQuesView.Rows[index].FindControl("Questiontags");
               Label Askedby = (Label)ForumQuesView.Rows[index].FindControl("Askedby");

               Response.Redirect(String.Format("AuzineForumAnswer.aspx?Question=" + Question.Text + "&Questiontags=" + Questiontags.Text + "&Askedby=" + Askedby.Text + "&AID1=" + AID1.Text + "&AID2=" + AID2.Text + "&AID3=" + AID3.Text, Server.UrlEncode(Question.Text), Server.UrlEncode(Questiontags.Text), Server.UrlEncode(Askedby.Text), Server.UrlEncode(AID1.Text), Server.UrlEncode(AID2.Text), Server.UrlEncode(AID3.Text)));

由于准确性,我传递了很多参数......

现在,当我运行它并单击按钮时,它打开 AuzineForumAnswer.AuzineForumAnswer并很好地显示该记录,但是当 qtags 字段具有"#"类型的数据(如此处标签(C#、GridView 等)时,就会出现问题,因此,当标签字段包含包含"#"chracter 的数据时,它会给出"对象引用未设置为对象的实例",如果 qtag 具有正常数据,例如 (特殊字符网格视图 sql C),那么它会打开 AuzineForumAnswer。ASPX 并显示数据而没有错误

AuzineForumAnswer.aspx背后的代码如下

 protected void GetAllData()
        {
            string connection = System.Configuration.ConfigurationManager.ConnectionStrings["AuzineConnection"].ConnectionString;

            using (SqlConnection sqlconn = new SqlConnection(connection))
            {
                using (SqlCommand sqlcomm = sqlconn.CreateCommand())
                {
                   sqlcomm.CommandText = "Select * From QF where Question='" + Server.UrlDecode(Request.QueryString["Question"].ToString()) + "' And qtags='" + Server.UrlDecode(Request.QueryString["Questiontags"].ToString()) + "' And UserFullName='" + Server.UrlDecode(Request.QueryString["Askedby"].ToString()) + "' And AID1='" + Server.UrlDecode(Request.QueryString["AID1"].ToString()) + "' And AID2='" + Server.UrlDecode(Request.QueryString["AID2"].ToString()) + "' And AID3='" + Server.UrlDecode(Request.QueryString["AID3"].ToString()) + "'";
                    SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    try
                    {
                        sqlconn.Open();
                        ForumQuesView.DataSource = dt;
                        ForumQuesView.DataBind();
                        ForumQuesView.AllowPaging = true;
                    }
                    catch (Exception ex)
                    {
                        Status.Text = ex.Message.ToString();
                    }
                }
            }
        }

现在我也不明白这里有什么问题,因为只有 QTAG 和问题是用户允许根据需要存储数据的两个字段,问题是文本和 QTAGS,都是 CHAR 字段,但问题不在数据库中,问题出在这里,字符 #

尝试更改 sql 语句以包含参数,看看是否有效。

您现在拥有的东西不仅难以维护并导致错误,而且很容易受到SQL注入攻击。

sqlcomm.CommandText = "Select * From QF where Question=@Question And qtags=@Qtags And UserFullName=@UserName And AID1=@AID1 And AID2=@AID2 And AID3=@AID3";
sqlcomm.Parameters.Add(new SqlParameter("@Question", Server.UrlDecode(Request.QueryString["Question"])));
sqlcomm.Parameters.Add(new SqlParameter("@Qtags", Server.UrlDecode(Request.QueryString["Questiontags"])));
sqlcomm.Parameters.Add(new SqlParameter("@UserName", Server.UrlDecode(Request.QueryString["Askedby"])));
sqlcomm.Parameters.Add(new SqlParameter("@AID1", Server.UrlDecode(Request.QueryString["AID1"])));
sqlcomm.Parameters.Add(new SqlParameter("@AID2", Server.UrlDecode(Request.QueryString["AID2"])));
sqlcomm.Parameters.Add(new SqlParameter("@AID3", Server.UrlDecode(Request.QueryString["AID3"])))

;

据我所知,即使您在条件中使用 #,查询也可以。

怀疑了一会儿,然后我尝试了这个查询

Select * From QF where Question='question 1'
And qtags='tag #1';

这些查询仍然运行顺利并返回记录。

最新更新