string strOrderID=System.Convert.ToInt32(TextBox1.Text); strSQL = String.Format("select * from orderTable where (orderID='{0}'", strOrderID);//...using this sql statement execute your query below写的方法很多,问题的关键在于构建sql语句。 可以将构键好的sql语句在调试的时候拷贝出来到查询器中验证一下语法的正确性。
哦,写错了,因为orderID在数据库中是int型,所以 //不需要单引号 strSQL = String.Format("select * from orderTable where (orderID={0}", strOrderID);
sql = "select * from orderTable where orderID='"+System.Convert.ToInt32(TextBox1.Text)+'";时总是报"varchar转换类型时出错 是因为你把把''System.Convert.ToInt32(TextBox1.Text)'作为值传过去啦!
select * from orderTable where (orderID=" + System.Convert.ToInt32(TextBox1.Text) + ")"
又有个问题了...我点击"查询"按钮时,会对数据库进行两次查询: SqlConnection myConn=new SqlConnection("server=Localhost;uid=sa;pwd=sa;database=Ecommerce"); string selCMD="select count(*) from orderTable where (orderID ='"+System.Convert.ToInt32(TextBox1.Text)+"')"; SqlCommand myCommand=new SqlCommand(selCMD,myConn); myCommand.Connection.Open(); int selNum=(int)myCommand.ExecuteScalar(); //检查是否存在此类订单的标志 myCommand.Connection.Close(); myConn.Close(); if(selNum==0) { Response.Write("<script language='javascript'>alert('对不起,没有此号的订单!请重新输入查询订单号!')</script>"); } else { //以下就是利用拖出来的控件显示数据 sqlDataAdapter1.Fill(dataSet21); DataGrid1.DataBind(); } ------------------------------------------------ 以上select count(*)....处都没有问题,能够跳到相应的条件中去,但我在拖出来的控件sqlDataAdapter1中配置加入SQL语句也是用的是:select * from orderTable where (orderID='"+System.Convert.ToInt32(TextBox1.Text)+"') 为什么每次在这里又出现什么varchar类型转换出错问题呢?前面都没有问题,而这里就有问题,难道在sqlDataAdapter控件中TextBox1.Text传不过值来,还是什么呢?该怎么解决啊?
strSQL = String.Format("select * from orderTable where (orderID='{0}'", strOrderID);//...using this sql statement execute your query below写的方法很多,问题的关键在于构建sql语句。
可以将构键好的sql语句在调试的时候拷贝出来到查询器中验证一下语法的正确性。
//不需要单引号
strSQL = String.Format("select * from orderTable where (orderID={0}", strOrderID);
是因为你把把''System.Convert.ToInt32(TextBox1.Text)'作为值传过去啦!
SqlConnection myConn=new SqlConnection("server=Localhost;uid=sa;pwd=sa;database=Ecommerce");
string selCMD="select count(*) from orderTable where (orderID ='"+System.Convert.ToInt32(TextBox1.Text)+"')";
SqlCommand myCommand=new SqlCommand(selCMD,myConn);
myCommand.Connection.Open();
int selNum=(int)myCommand.ExecuteScalar(); //检查是否存在此类订单的标志
myCommand.Connection.Close();
myConn.Close();
if(selNum==0)
{
Response.Write("<script language='javascript'>alert('对不起,没有此号的订单!请重新输入查询订单号!')</script>");
}
else
{
//以下就是利用拖出来的控件显示数据
sqlDataAdapter1.Fill(dataSet21);
DataGrid1.DataBind();
}
------------------------------------------------
以上select count(*)....处都没有问题,能够跳到相应的条件中去,但我在拖出来的控件sqlDataAdapter1中配置加入SQL语句也是用的是:select * from orderTable where (orderID='"+System.Convert.ToInt32(TextBox1.Text)+"') 为什么每次在这里又出现什么varchar类型转换出错问题呢?前面都没有问题,而这里就有问题,难道在sqlDataAdapter控件中TextBox1.Text传不过值来,还是什么呢?该怎么解决啊?