2013-05-10 79 views
0
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="postreplyadmin.aspx.cs" Inherits="postreplay" MasterPageFile="~/AdminMaster.master" Title="Post-Reply Page"%> 
<%@ Import Namespace="System.Data" %> 
<%@ Import Namespace="System.Data.SqlClient" %> 

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
<% 
    String postid = Request.QueryString["id"]; 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["civilRegDB"].ConnectionString); 
    con.Open(); 
    String sql = "select * from Forumthread where PostID=" + postid; 
    SqlDataAdapter da=new SqlDataAdapter(sql,con);   
    DataSet ds=new DataSet(); 
    da.Fill(ds); 
    DataRow drow = ds.Tables[0].Rows[0]; 
    String name = drow["Name"].ToString(); 
    String desc = drow["Description"].ToString(); 
    DateTime dt = Convert.ToDateTime(drow["PostDate"].ToString()); 
    String postdate = dt.ToString("dd/MM/yyyy",System.Globalization.CultureInfo.InvariantCulture); 
    String mailid = drow["Email"].ToString(); 
    %> 
</asp:content> 

我收到一个sqlexception而我试图回复帖子。错误:'='附近语法不正确。我环顾四周寻找类似于我的其他问题,但我找不到任何值得我的东西。运行时异常语法错误'='

我在“da.fill(ds);”上收到错误消息。任何人都可以帮我这个... :(

回答

1

postid从查询字符串可能不具有价值,其结果是TSQL语句无效。

添加的支票是否查询字符串提供价值

也验证它的值是你所期望的范围,并考虑使用预处理语句/参数 - 。当前的方法是让你打开到SQL注入

+0

有帖子ID值。但我仍然收到错误.. – user2369497 2013-05-10 10:11:12

+0

postid的值是多少? – Kami 2013-05-10 10:11:56

+0

其int数据类型 – user2369497 2013-05-10 10:19:12

2

postid是一个字符串,它应该是用单引号括起来'

String sql = "select * from Forumthread where PostID='" + postid +"'"; 

但是,如果您使用SqlParameter可以让您免受SQL Injection的困扰,那就更好了。

像:

String postid = Request.QueryString["id"]; 
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["civilRegDB"].ConnectionString); 
con.Open(); 
String sql = "select * from Forumthread where [email protected]"; //parameter 
SqlCommand cmd = new SqlCommand(sql, con); 
cmd.Parameters.AddWithValue("postID", postid); 
SqlDataAdapter da = new SqlDataAdapter(cmd); // pass command to adapter 
DataSet ds = new DataSet(); 
da.Fill(ds); 
DataRow drow = ds.Tables[0].Rows[0]; 
String name = drow["Name"].ToString(); 
String desc = drow["Description"].ToString(); 
DateTime dt = Convert.ToDateTime(drow["PostDate"].ToString()); 
String postdate = dt.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); 
String mailid = drow["Email"].ToString(); 
0

有作为答案的一个建议

更换String sql = "select * from Forumthread where PostID=" + postid;

String sql = "select * from Forumthread where PostID='" + postid + "'";

或U尝试以下操作:

如果它不起作用,那么可能你将不得不直接在查询中使用int。 为此,只需在整型变量中获取您的queryString值即可。 像:

int postid = Convert.ToInt32(Request.QueryString["id"]); 

String sql = "select * from Forumthread where PostID=" + postid;