private bool save(string dwsb)
        {
            bool success = true;
            try
            {
               mybase.getcom("update sb set jfbj='是',jfrq='" + System.DateTime.Now.ToShortDateString().ToString() + "'where " + dwsb + "='" + this.textBox7.Text.Trim() + "'and nf='" + this.numericUpDown4.Value.ToString().Trim() + "'and yf='" + this.numericUpDown3.Value.ToString().Trim() + "' ");
             }            
            catch (System.Exception E)
            {
                success = false;
                MessageBox.Show(E.ToString());
            }
            return success;
        }
     我想用以上更新方法来进行更新标记字段,如果更新成功返回true,否则返回false
但是现在遇到一个问题,就是如果没有正确的更新条件,比如编号不存在,也就是没有一条记录符合更新,其实就是没更新,也返回了true。
我怎么才能让它返回false

解决方案 »

  1.   

    executenonquery()>0
    表示成功.
      

  2.   

    private static void CreateCommand(string queryString,
        string connectionString)
    {
        using (SqlConnection connection = new SqlConnection(
                   connectionString))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
    }对连接执行 Transact-SQL 语句并返回受影响的行数。 
      

  3.   

    3楼这表情太cute了,俺稀罕……
      

  4.   

    你用了mybase类的getcom方法,看看它的返回值是什么类型。
    如果作者有心,返回值应该是int类型
    你的mybase类的getcom方法可能是:.............
    public static int getcom(string comstr)
    {
        ........................
        return i;   //i是返回的受影响的行数
    }如果是上面我猜测的写法,有返回值,你的程序改为:
      try
      {
      int isupdateok=mybase.getcom("update sb set jfbj='是',jfrq='" + System.DateTime.Now.ToShortDateString().ToString() + "'where " + dwsb + "='" + this.textBox7.Text.Trim() + "'and nf='" + this.numericUpDown4.Value.ToString().Trim() + "'and yf='" + this.numericUpDown3.Value.ToString().Trim() + "' ");
        
        //就是上面的更新语句影响的行数大于0就认为更新成功了,否则是没有更新或更新失败
        if(isupdateok>0)
        { 
            MessageBox.Show("更新成功");
        }
        else
        {
            MessageBox.Show("更新失败!");
        }
      }
      catch
      {
        MessageBox.Show("更新失败");
      }
      

  5.   

    mybase.getcom方法返回值
    ExecuteNonQuery()方法主要用户更新数据,通常它使用Update,Insert,Delete语句来操作数据库,其方法返回值意义:对于 Update,Insert,Delete  语句 执行成功是返回值为该命令所影响的行数,如果影响的行数为0时返回的值为0,如果数据操作回滚得话返回值为-1