各位大虾,我是个c#新手,现在遇到一个问题,亟待解决:
我用c# 编写了一个应用程序,在连接到SQL serve数据库时遇到问题了.我连接到数据库,返回结果dataset,我为了选择结果中特定的某条纪录,进行string类型比较时发现不能比较,我快急死了.各位大虾帮帮忙啊!我的部分代码如下:
 public void getconnection()                                          //和数据库建立连接
        {
             string petnamee;
             string petcodee;
           
            
                string connectionString = "server=localhost;" +                "Trusted_Connection=yes; database=pigpet";                // 从Customers表获取记录                string commandString = "Select petname,petcode from pigpet";                // 创建DataSet命令对象
          
                // 和DataSet                SqlDataAdapter DataAdapter = new SqlDataAdapter(commandString, connectionString);                DataSet Dataset = new DataSet();                // 填充DataSet对象                DataAdapter.Fill(Dataset, "pigpet");                // 从DataSet获取一个表                DataTable datatable = Dataset.Tables[0];                // 对于表中的每一列,显示信息                datatable = Dataset.Tables[0];                int rows = datatable.Rows.Count;
             ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                for (int i = 0; i < rows; i++)
                {
                   
                    
                   petnamee  = datatable.Rows[i]["petname"].ToString();
                   petcodee  = datatable.Rows[i]["petcode"].ToString();                   if (this.comboBox1.Text == petnamee  & this.textBox1.Text == petcodee)
                    {
                         Form1 form1 = new Form1();                            
                         form1.Show();
                         this.Hide();
                         petkey =1; 
                    }
                }           if (petkey!= 1)
            {
                Form13 form13 = new Form13();                                      
                form13.Show();
                this.Hide();            }   
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        }
经调试我发现能返回结果,是 if (this.comboBox1.Text == petnamee  & this.textBox1.Text == petcodee)总不成立,即使返回的结果中有是它成立的条件
例如,我让this.comboBox1.Text="mmm",this.textBox1.Text="nnn",数据库中有相应的纪录,但if语句还是不执行.各位大虾麻烦该看看吧.帮帮我吧

解决方案 »

  1.   

    F5  一下 后F11“// 从Customers表获取记录                string commandString = "Select petname,petcode from pigpet";”
    说是从Customers表获取记录 ,但是为什么是Select petname,petcode from pigpet这个
    而不是Select petname,petcode from Customers
    你的ADO.NET 要加强啊你可以到我的博客上坐坐ado.net c#.net2005 From第一讲 
      

  2.   

    确认其他地方没错的话,if (this.comboBox1.Text == petnamee  & this.textBox1.Text == petcodee)这里请再加一个&,即if (this.comboBox1.Text == petnamee  && this.textBox1.Text == petcodee)
      

  3.   

    & 这个符号 应该 写为 && 吧!
    没用过 & 呢。
      

  4.   

    try if (this.comboBox1.Text == petnamee  & this.textBox1.Text == petcodee)
    改为:
     if (this.comboBox1.Text == petnamee  && this.textBox1.Text == petcodee)
      

  5.   

    try 去掉空格if (this.comboBox1.Text.Trim() == petnamee.Trim()  && this.textBox1.Text.Trim()  == petcodee.Trim())
      

  6.   

    不是了,"// 从Customers表获取记录"是以前做的注释拿来后忘了去掉了
      

  7.   

    LZ 调试的时候 
     petnamee  = datatable.Rows[i]["petname"].ToString();
      petcodee  = datatable.Rows[i]["petcode"].ToString();
    这2个变量能取到值不?
      

  8.   

    在 int rows = datatable.Rows.Count; 处设个断点,跟踪调试下
      

  9.   

    LZ 调试的时候 
     petnamee  = datatable.Rows[i]["petname"].ToString();
      petcodee  = datatable.Rows[i]["petcode"].ToString();
    这2个变量能取到值不?
    能取到值,还能把值赋给别的东西,例如label的text
      

  10.   

    有的资料上说"dataset类的tostring 方法返回包含Component名称的string" 这句话我不是很懂,是不是返回string 含有什么东西使 if (this.comboBox1.Text == petnamee  && this.textBox1.Text == petcodee)总不成立????
      

  11.   

    for (int i = 0; i < rows.count; i++)
    //注意这里:应该是rows.count
    {
     
          petnamee  = datatable.Rows[i]["petname"].ToString();
          petcodee  = datatable.Rows[i]["petcode"].ToString();
          
          if (this.comboBox1.Text == petnamee  & this.textBox1.Text == petcodee)
          // 这里很显然&应为&&
          {
               Form1 form1 = new Form1();                            
               form1.Show();
               this.Hide();
               petkey =1; 
          }
    }或者你最好用foreach
    foreach(DataRow dr in datatable.Rows)
    {
          petnamee  = dr["petname"].ToString();
          petcodee = dr["petcode"].ToString();
          // ...
    }
      

  12.   

    C# 中,当用于逻辑运算时, && 与 & , || 于 | 等价
      

  13.   

    看不透,打个断点,然后 debug,看看具体的值,或者,输出到 DataGrid ,确认是否真的有目标值且 for 循环内部似乎不合情理,假如找到目标值, 应该 break 
      

  14.   

    if (this.comboBox1.Text == petnamee  & this.textBox1.Text == petcodee)
    --->
    if (this.comboBox1.Text == petnamee  && this.textBox1.Text == petcodee)
      

  15.   

    我在本地测试了一下,语句没有错误,你将this.comboBox1.Text,this.textBox1.Text附上具体值再试一下,是不是数据库中没有相应数据?
      

  16.   

    if (this.comboBox1.Text == petnamee  & this.textBox1.Text == petcodee)
    改为:
    if ((this.comboBox1.Text == petnamee)  && (this.textBox1.Text == petcodee))
    若还是不行,估计是你的combobox1与textbox1取出来的值有问题了,
      

  17.   

    是 if (this.comboBox1.Text == petnamee  & this.textBox1.Text == petcodee)总不成立,即使返回的结果中有是它成立的条件
    =========================================
    改为if(this.comboBox1.Text.ToString().equals(petnamee) & this.textBox1.Text.ToString().equals(petcodee))=========================================
    还有一个问题就是LZ的代码有问题,那个DataSet 实例的值为空。因为你的代码都没有去执行,所以,无论你怎么去取值,怎么去比较,都是没有用的...
      

  18.   

    if (petnamee.StartsWith(this.comboBox1.Text.Trim()) && petcodee.StartsWith(this.textBox1.Text.Trim())这样就防止了数据库取出来的数据有空格或者空字符.
      

  19.   

    工资到底要多少才满足?
    http://www.jobhere.net/Article/ArticleShow.asp?ArticleID=6575
      

  20.   

    樓主有沒有注意到大小寫問題?
    SQL里不分大小寫
    而C#里要分的
      

  21.   

    if (this.comboBox1.Text == petnamee  & this.textBox1.Text == petcodee)if (this.comboBox1.Text == petnamee  && this.textBox1.Text == petcodee)
    少一个“&”你的if就不成立了
      

  22.   

    哈哈,解决了,是空格问题,去掉就行了.
    try 去掉空格if (this.comboBox1.Text.Trim() == petnamee.Trim()  && this.textBox1.Text.Trim()  == petcodee.Trim())感谢大家的支持!
    其实昨天sqllong(阿秋)已经提醒我了,都怪我没注意.谢谢啊