你的语句写错了,  MessageBox.Show("该行数据不存在表中!"); 
多了个分号

解决方案 »

  1.   

    ExecuteNonQuery对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于所有其他类型的语句,返回值为 -1。
      

  2.   

    你的代码修改如下:
    public   void   SelectTable()//查询超市货物表 
                  { 
                          int   Isexist; 
                          conn   =   new   SqlConnection("server=(local);database=Myproject;uid=sa;"); 
                          string   SQL   =   "select   *   from   SuperMarket   where 
                 数据字段='"+txt_selectdata.Text+"'"; (//模糊查询在分号和引号中间加个百分号
                   例子:数据字段 like '%"+txt_selectdata.Text+"%'
    )
                          
                          conn.Open();//打开数据库                      myCmd   =   new   SqlCommand(SQL,   conn); 
              
                          //Isexist   =   myCmd.ExecuteNonQuery(); 这里错了,
                             修改如下
                             sqldatareader myreader=cmd.executereader();
                          if   (myreader.read()) //如果搜索到数据
                          { 
                                  da   =   new   SqlDataAdapter(SQL,   conn); 
                                  ds   =   new   DataSet(); 
                                  da.Fill(ds,   "SuperMarket"); 
                                  GoodsGrid.SetDataBinding(ds,   "SuperMarket"); 
                                  GoodsGrid.ReadOnly   =   true;                       } 
                          else   //如果没有搜索到数据
                          {
                                  MessageBox.Show("该行数据不存在表中!"); 
                          }
                          myCmd.Connection.Close(); 
                    } 
      

  3.   

    还有就是,在数据库操作时,要放在try{..你的代码段..}catch(expression ex){messagebox.show(ex.message);}里面,这样好捕捉错误信息
      

  4.   

    myCmd.ExecuteNonQuery(); 返回影响的行数,查询不影响行数的,所以是0
    string str=comm.ExecuteScalar().tostring();返回第一行第一列的值
    之后判读str是否为空~
      

  5.   

    谢谢大家问题解决了
    代码如下:
    public void SelectTable()//查询超市货物表
           {           conn = new SqlConnection(connectstring);
               string SQL = "select * from SuperMarket where "+cmb_selecttype.Text+"='"+txt_selectdata.Text+"'";
               da = new SqlDataAdapter(SQL, conn);
               ds = new DataSet();
               da.Fill(ds, "SuperMarket");
               if (ds.Tables["SuperMarket"].Rows.Count == 0)
                   MessageBox.Show("此数据不存在表中!");
               else GoodsGrid.SetDataBinding(ds, "SuperMarket");
               GoodsGrid.ReadOnly = true;
               myCmd.Connection.Close();
            }