string txtcmd2 = "select top 1 * from SYDJB where BGH=" + bgh + "";
改成这样
string txtcmd2 = "select top 1 * from SYDJB where BGH= ' " + bgh + "'";
改成这样
string txtcmd2 = "select top 1 * from SYDJB where BGH= ' " + bgh + "'";
调试欢乐多
如果数据库中的“BGH”为字符串,就写成string txtcmd2 = "select top 1 * from SYDJB where BGH= ' " + bgh + " '";
应该注意数据的类型。
用' " + bgh + " '的,可是有时候.net搞错,把bgh的值当作数值型了,这样才是奇怪啊。
所以在数据库当中,查询一个条件为姓名为张三的记录,应该是
select * from user where name='张三'
而直接写张三,不发引号的情况下,他就会误认为他是一个数字或是一个字段值,从而得不到正确的结果所以我们在C#中写Sql语句时,在涉及从中有变量时,多采用传参的方式进行处理
如果不用参数也要写成 string sql="select * from user where name='"+条件+"'";的形式
这类的赋值的。
问题是同一个页面,不同的程序里面都会出问题,比如说都是在main.aspx.cs下,有2个,
一个是dbind()一个是Button2_Click:第一种:public void dbind()
{
string bgh = Request["bgh"].Trim();
string txtcmd = "select * from SYDJB s INNER JOIN JC011 j ON s.SYH=j.SYH and s.BGH=" + bgh + "";
.......下面语句省略
}
---这个就要" + bgh + ",用'" + bgh + "'就提示出错。第二种: protected void Button2_Click(object sender, EventArgs e)
{
string bgh = Request["BGH"].ToString();
string cmdtxt = "update SYDJB set S_JCFS='1' where BGH='" + bgh + "'";
.........
}
----这个就没有问题 按道理应该是用'" + bgh + "'这种才是对的,但是有时候会出现第一种情况,而且在同一页面出现,
真是奇快啊。而且bgh的值都是字符和数字的组合而成的字符串,真是疑难问题啊。