报错!请进来看看!

alan841207 2009-12-16 04:03:08
为什么下面两段代码,第一段运行报错,而第二段运行可以通过,为什么啊?
第一段
SqlConnection conn = sqlconn.sqlconn1();
conn.Open();
string sql = "select * from XLImport5 where PO=" + this.textBox1.Text.ToString();
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
adapter.Fill(ds, "XLImport5");
conn.Close();
dataGridView1.DataSource = ds.Tables["XLImport5"];

第二段

SqlConnection conn = sqlconn.sqlconn1();
conn.Open();
string sql = "select * from XLImport5 where PO='123' ";
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
adapter.Fill(ds, "XLImport5");
conn.Close();
dataGridView1.DataSource = ds.Tables["XLImport5"];
...全文
67 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
alan841207 2009-12-16
  • 打赏
  • 举报
回复
谢谢各位!以后我一定注意!
平生我自如 2009-12-16
  • 打赏
  • 举报
回复
string sql = "select * from XLImport5 where PO='" + this.textBox1.Text.ToString()"';
在双引号外面要套一个单引号
hhc123 2009-12-16
  • 打赏
  • 举报
回复
string sql = "select * from XLImport5 where PO='"+ this.textBox1.Text.ToString()+"'";
shighui 2009-12-16
  • 打赏
  • 举报
回复
string sql = "select * from XLImport5 where PO=" + this.textBox1.Text.ToString();
错在这一句:
this.textBox1.Text.ToString() 的两边均加单引号就可以。因为该字段不是数值型的,所以加跟不加是不一样的。
j432432432432432 2009-12-16
  • 打赏
  • 举报
回复
string sql = "select * from XLImport5 where PO=" + this.textBox1.Text.ToString();

上面代码应该这样写 ,你试试

string sql = string.format("select * from XLImport5 where PO={0}",this.textBox1.Text.ToString());

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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