asp.net C# '9' 附近有语法错误
代码如下:protectedvoidButton1_Click(objectsender,EventArgse){SqlConnectionsqlcon=newSqlCo...
代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = "Data Source=WIN-A0643GOM7LQ;Initial Catalog=TF;Persist Security Info=True;User ID=sa;Password=sa";
sqlcon.Open();
string strPark = "select parking from fenqu where ID ='" + Request.QueryString["id"] + "'";
string strPlace = "select place from fenqu where ID ='" + Request.QueryString["id"] + "'";
string strInsert = "insert into book(ID,name,tel,startTime,parking,place) values(";
strInsert = strInsert + "'" + TextBox3.Text + "',";
strInsert = strInsert + "'" + TextBox1.Text + "',";
strInsert = strInsert + "'" + TextBox2.Text + "',";
strInsert = strInsert + "'" + TextBox4.Text + "',";
strInsert = strInsert + "'" + strPark + "',";
strInsert = strInsert + "'" + strPlace + "')";
SqlCommand sqlcmd = new SqlCommand(strInsert, sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
Response.Write("<div align=center><li>预订成功!</li><li><a href=javascript:history.back()>点此返回</a></li>");
Response.End();
}
运行的时候它报错,说是'9' 附近有语法错误,我找了半天也没有9啊~~到底哪里错了,求高手指点~~急急急~~ 展开
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = "Data Source=WIN-A0643GOM7LQ;Initial Catalog=TF;Persist Security Info=True;User ID=sa;Password=sa";
sqlcon.Open();
string strPark = "select parking from fenqu where ID ='" + Request.QueryString["id"] + "'";
string strPlace = "select place from fenqu where ID ='" + Request.QueryString["id"] + "'";
string strInsert = "insert into book(ID,name,tel,startTime,parking,place) values(";
strInsert = strInsert + "'" + TextBox3.Text + "',";
strInsert = strInsert + "'" + TextBox1.Text + "',";
strInsert = strInsert + "'" + TextBox2.Text + "',";
strInsert = strInsert + "'" + TextBox4.Text + "',";
strInsert = strInsert + "'" + strPark + "',";
strInsert = strInsert + "'" + strPlace + "')";
SqlCommand sqlcmd = new SqlCommand(strInsert, sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
Response.Write("<div align=center><li>预订成功!</li><li><a href=javascript:history.back()>点此返回</a></li>");
Response.End();
}
运行的时候它报错,说是'9' 附近有语法错误,我找了半天也没有9啊~~到底哪里错了,求高手指点~~急急急~~ 展开
3个回答
展开全部
strPark和strPlace返回的是行,而不是具体值,也就是说你两个string根本就没取到正确的东西,因此insert语句第5,6个参数也不可能匹配到相应的类型。解决办法是用parameter取到值。你可以在SQL管理器里面去测试你的语句。
select parking from fenqu where id=9;
和参数语句:
declare @str varchar(10)
select @str=parking from fenqu where id=9;
print @str
你就知道区别了。具体C#参见MSDN SQLparameter,因为太久不用你的方法,忘了。
select parking from fenqu where id=9;
和参数语句:
declare @str varchar(10)
select @str=parking from fenqu where id=9;
print @str
你就知道区别了。具体C#参见MSDN SQLparameter,因为太久不用你的方法,忘了。
追问
怎么用parameter取值啊?还是不会呀~刚学这个~~不太懂~~麻烦您能给我举个例子说明一下么?
展开全部
应该是引号的问题吧,建议不要使用双引号来添加参数,使用string.format("{0}{1}",参数1,参数2),这样就避免了引号错误的问题。
追问
还是不行,我发现那个“9”是ID的值~~这个是什么错误啊?
追答
你设置断点调试,把strPark ,strPlace,strInsert 赋值后的语句拷贝出来,放在sql2000或者2005里面调试下就知道了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
请在ID两边加中括号试一试:[ID]
追问
不是这个问题~~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询