public double Get_Tj_Num(string byorder,string stj)//获取符合条件stj的记录总数
{
string sSqlTj=stj;
if (sSqlTj.Length ==0)
{
sSqlTj="select sum ("+byorder+") as TotalRec from PointsDetails_List";
}
else
{
sSqlTj="select sum ("+byorder+") as TotalRec from PointsDetails_List where " + stj;
}
try
{
if (m_cn.State!=ConnectionState.Open)
{
m_cn.ConnectionString =m_ConnString.ConnString;
m_cn.Open();
}
SqlCommand myCommand = new SqlCommand(sSqlTj,m_cn);

myCommand.CommandType=CommandType.Text;  
         
               double TotalRec=(double) myCommand.ExecuteNonQuery();
   return TotalRec;
}
catch(Exception e)
{
throw new dbclass.ClassCallException ("CPointsDetails_List类中异常:"+e.Message);
}
}帮我看一下,我想返回统计TotalRec的值,应该怎样写.

解决方案 »

  1.   

    ExecuteNonQuery()只会返回在数据库受影响的行数
      

  2.   

    ACCESS数据库的话
    OleDbDataAdapter   oda   =   new   OleDbDataAdapter(myCommand); 
             DataSet   ds   =   new   DataSet(); 
             oda.Fill(ds); 
             int   count   =   ds.Tables[0].Rows.Count;SQL的话把OleDbDataAdapter换成SqlDataAdapter,还得在前面引入空间System.Data.SqlClient
    整个代码:public   double   Get_Tj_Num(string   byorder,string   stj)//获取符合条件stj的记录总数 

        string   sSqlTj=stj; 
        if   (sSqlTj.Length   ==0) 
        { 
            sSqlTj="select   sum   ("+byorder+")   as   TotalRec   from   PointsDetails_List"; 
        }  
        else 
        { 
            sSqlTj="select   sum   ("+byorder+")   as   TotalRec   from   PointsDetails_List   where   "   +   stj; 
        } 
        try 
        { 
            if   (m_cn.State!=ConnectionState.Open) 
            { 
                m_cn.ConnectionString   =m_ConnString.ConnString; 
                m_cn.Open(); 
            } 
            SqlCommand   myCommand   =   new   SqlCommand(sSqlTj,m_cn); 
            OleDbDataAdapter   oda   =   new   OleDbDataAdapter(myCommand); 
            DataSet   ds   =   new   DataSet(); 
            oda.Fill(ds); 
            double sum= (double)ds.Tables[0].Rows[0][0];
            return sum;
        } 
        catch(Exception   e) 
        { 
            throw   new   dbclass.ClassCallException   ("CPointsDetails_List类中异常:"+e.Message); 
        } 
      

  3.   

    double TotalRec = Convert.ToDouble(myCommand.ExecuteScalar()); 
      

  4.   

    4楼的运行后出现这个错误CPointsDetails_List类中异常:指定的转换无效
      

  5.   

    myCommand.ExecuteNonQuery(); 
    是影响的条数, sum当然是一条了 
    如果要用myCommand.ExecuteNonQuery(); 

    if   (sSqlTj.Length   ==0) 

    sSqlTj="select   byorder  from   PointsDetails_List"; 

    else 

    sSqlTj="select   byorder  from   PointsDetails_List   where   "   +   stj; 

      

  6.   

    你的where 后面条件没有 colum = 'stj'?
      

  7.   

    不好意思,写错一个地方
    table.rows[0][0]是Object,得加toString()......public   double   Get_Tj_Num(string   byorder,string   stj)//获取符合条件stj的记录总数 

        string   sSqlTj=stj; 
        if   (sSqlTj.Length   ==0) 
        { 
            sSqlTj="select   sum   ("+byorder+")   as   TotalRec   from   PointsDetails_List"; 
        }  
        else 
        { 
            sSqlTj="select   sum   ("+byorder+")   as   TotalRec   from   PointsDetails_List   where   "   +   stj; 
        } 
        try 
        { 
            if   (m_cn.State!=ConnectionState.Open) 
            { 
                m_cn.ConnectionString   =m_ConnString.ConnString; 
                m_cn.Open(); 
            } 
            SqlCommand   myCommand   =   new   SqlCommand(sSqlTj,m_cn); 
            OleDbDataAdapter   oda   =   new   OleDbDataAdapter(myCommand); 
            DataSet   ds   =   new   DataSet(); 
            oda.Fill(ds); 
            double sum= (double)ds.Tables[0].Rows[0][0].toString();
            return sum;
        } 
        catch(Exception   e) 
        { 
            throw   new   dbclass.ClassCallException   ("CPointsDetails_List类中异常:"+e.Message); 
        }