关于把数据库中的数据与textbox中比较的问题

来源:百度知道 编辑:UC知道 时间:2024/06/05 08:04:15
我有个数据库A,有字段名a(char),b(char)。界面上有textbox1,textbox2,按钮
textbox1一个对应于a,当我在textbox1,textbox2中输入数据时,把输入textbox1中的a在表A中对应b的数据与textbox2中输入的比较,我的代码是:
SqlDataAdapter sda1 = new SqlDataAdapter("select b from A where a='" + this.textobx1.Text + "' ", con);
DataSet ds1 = new DataSet();
sda1.Fill(ds1);
string x = ds1.Tables[0].Rows[0][0].ToString();
if (x == this.textbox2.Text)
{... }
else
{... }
为什么无论我输什么,程序都认为x != this.textbox2.Text,总是执行else的语句呢?谢谢

if(x.Trim().Equals(this.textbox2.Text.Trim());){
...
}else{
...
}

一般字符串比较不用"=="的...用Equals

你可以debug看一下x的实际值,如果你看到的相同,可能编码方式不同,不要简单地用==,尝试一下专用的字符串比较函数strcomp,可以带参数的