Request["id"]和数据库查询的问题

rgccxning 2008-05-13 07:00:43
SqlConnection con = new SqlConnection("server=localhost;database=bbs;uid=sa;pwd=''");
con.Open();
string sql = "select * from discuss where topicNo= "+Request["id"];
SqlCommand comm = new SqlCommand(sql ,con);
SqlDataReader dr = comm.ExecuteReader();



报错:



Exception Details: System.Data.SqlClient.SqlException: 第 1 行: '=' 附近有语法错误。

Source Error:

Line 19: string sql = "select * from discuss where topicNo= "+Request["id"];
Line 20: SqlCommand comm = new SqlCommand(sql ,con);
Line 21: SqlDataReader dr = comm.ExecuteReader();
Line 22: try
Line 23: {





单独 string sql = "select * from discuss where topicNo= 1“是没有问题的

为什么Request["id"]加上后就出问题了啊
...全文
119 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Novelty 2008-05-14
  • 打赏
  • 举报
回复
Request["id"]需要加一个判断,是否存在值。
if(Request["id"] != null)
{
SqlConnection con = new SqlConnection("server=localhost;database=bbs;uid=sa;pwd=''");
con.Open();
string sql = "select * from discuss where topicNo= "+Request["id"];
SqlCommand comm = new SqlCommand(sql ,con);
SqlDataReader dr = comm.ExecuteReader();
}
rgccxning 2008-05-14
  • 打赏
  • 举报
回复
问题解决.....
只是在?和id间多打了空格....
skyaspnet 2008-05-13
  • 打赏
  • 举报
回复
用属性编辑器设置DataNavigateUrlFormatString

为("topicNO","showQA.aspx?id={0}")即可
oec2003 2008-05-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 insus 的回复:]
试试:

string sql = "select * from [discuss] where [topicNo]= '"+ Request["id"].Replace("'","''") +"'";
[/Quote]
rgccxning 2008-05-13
  • 打赏
  • 举报
回复
修改了一下,依然有问题
String sql = "select * from [discuss] where topicNo = " + Convert.ToInt32(Request["id"]);
这样不会显示出错了,但是通过Response.Write(Convert.ToInt32(Request["id"]));测试显示值为0,是不是哪里还有什么毛病啊
和下面的设定是不是有关系啊
<asp:HyperLinkColumn DataTextField="topic" HeaderText="提出问题" DataNavigateUrlField="topicNo"
Target="_blank" DataNavigateUrlFormatString="showQA.aspx? id={0}">
<ItemStyle Width="400px" />
<HeaderStyle Width="500px" />
</asp:HyperLinkColumn>
topic和topicNo是数据库的不同的两列~
rgccxning 2008-05-13
  • 打赏
  • 举报
回复
修改了一下,依然有问题
String sql = "select * from [discuss] where topicNo = " + Convert.ToInt32(Request["id"]);
这样不会显示出错了,但是通过Response.Write(Convert.ToInt32(Request["id"]));测试显示值为0,是不是哪里还有什么毛病啊
和下面的设定是不是有关系啊
<asp:HyperLinkColumn DataTextField="topic" HeaderText="提出问题" DataNavigateUrlField="topicNo"
Target="_blank" DataNavigateUrlFormatString="showQA.aspx? id={0}">
<ItemStyle Width="400px" />
<HeaderStyle Width="500px" />
</asp:HyperLinkColumn>
topic和topicNo是数据库的不同的两列~
rgccxning 2008-05-13
  • 打赏
  • 举报
回复
修改了一下,依然有问题
String sql = "select * from [discuss] where topicNo = " + Convert.ToInt32(Request["id"]);
这样不会显示出错了,但是通过Response.Write(Convert.ToInt32(Request["id"]));测试显示值为0,是不是哪里还有什么毛病啊
和下面的设定是不是有关系啊
<asp:HyperLinkColumn DataTextField="topic" HeaderText="提出问题" DataNavigateUrlField="topicNo"
Target="_blank" DataNavigateUrlFormatString="showQA.aspx? id={0}">
<ItemStyle Width="400px" />
<HeaderStyle Width="500px" />
</asp:HyperLinkColumn>
topic和topicNo是数据库的不同的两列~
skyaspnet 2008-05-13
  • 打赏
  • 举报
回复
输出一下你所得的页面的Request["id"]值,看里面是否有单引号', 逗号,这些符号

有的话会报错
insus 2008-05-13
  • 打赏
  • 举报
回复
试试:

string sql = "select * from [discuss] where [topicNo]= '"+ Request["id"].Replace("'","''") +"'";
backham7 2008-05-13
  • 打赏
  • 举报
回复
两个错误
1、discuss应用中括号[discuss]
2、CONVERT.TOINI32(REQUEST["ID"])
idragonet 2008-05-13
  • 打赏
  • 举报
回复
string sql = "select * from discuss where topicNo= "+ Request.QueryString["id"];

这样会有SQL注入漏洞!!


62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧