代码如下:
    private int GC(int GradeId,int ClassId)
        {
            int result = -1;
            string sql = string.Format("select count(*) from Class where ClassId={0} and GradeId={1}", ClassId, GradeId);
            try
            {
                SqlCommand command = new SqlCommand(sql, SQL.connection);
                SQL.connection.Open();
                result = (int)command.ExecuteScalar();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                SQL.connection.Close();
            }
            return result;
        }
我在数据库里面用SQL查询可以查到结果为1 但是在C#中返回值就是0了 这是什么问题啊

解决方案 »

  1.   

    追踪下你的SQL语句 看下是否是参数的值的问题。
      

  2.   

    带入参数 在SQL查询 结果是 0?
      

  3.   

    嗯 我的SQLselect count(*) from Class where ClassId=2 and GradeId=1
    查询结果返回值是1
      

  4.   

    我一步步找到了问题了 
    private int GC(int GradeId,int ClassId)
            {
                int result = -1;
                string sql = string.Format("select count(*) from Class where ClassId={0} and GradeId={1}", ClassId, GradeId);
                try
                {
                    SqlCommand command = new SqlCommand(sql, SQL.connection);
                    SQL.connection.Open();
                    result = (int)command.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    SQL.connection.Close();
                }
                return result;
            }是GradeId和ClassId的问题 我在select count(*) from Class where ClassId={0} and GradeId={1}把{0}和{1}换成2和1就没问题了 但是为什么定义一个int型的就会出问题呢
      

  5.   

      string sql = string.Format("select count(*) from Class where ClassId={0} and GradeId={1}", ClassId, GradeId);
    在查询分析器执行,参数顺序
      

  6.   

    额 
    SQL 不会 执行 隐形转换吗?
      

  7.   

    ClassId, GradeId 数据库里什么类型?