如何显示datareader的所有值 [英] how to dispaly all values of datareader

查看:58
本文介绍了如何显示datareader的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 DataTable ProTable =  new  DataTable(); 
ProTable.Columns.Add( 产品代码 typeof string ));
ProTable.Columns.Add( ProdDesc typeof string ));
ProTable.Columns.Add( UOM typeof string ));
ProTable.Columns.Add( OnHand typeof string ));
ProTable.Columns.Add( Price typeof string ));
ProTable.Columns.Add( 金额 typeof string ));

// int coutn = 0;
// Pr_Bar.Visible = true;
// Pr_Bar.Minimum = 0;
/// / sqlstr = sdbll.Count(ppbel);
/// / Pr_Bar.Maximum = Convert.ToInt32(sdbll.get_data(sqlstr) );
// ProTable.Rows.Clear();
probel.prod_cate = Ddl_Brand.Text;
Dgv_Summary.DataSource = srbll.CategoryData(probel,stock,dblAmount);
prodmax = srbll.prodmax(probel,prod_max);
for int i = 0 ; i < Dgv_Summary.RowCount; i ++)
{
// for(int j = 2; j< Dgv_Summary.ColumnCount; j ++)
// {
// if(Dgv_Summary [ j,i] .Value!= null)
// {
尝试
{
double ProdPrice = IsNumber(prodmax.ToString( ));
stock = int .Parse(srbll.getonhand(sqldateconverion(Dtp_ToDate.Value),Dgv_Summary [ 0 ,i] .Value.ToString(),probel.comp_id.ToString()));
dblAmount = Convert.ToDouble(((stock * ProdPrice).ToString()));
ProTable.Rows.Add(Dgv_Summary [ 0 ,i] .Value.ToString(),Dgv_Summary [ 1 ,i] .Value.ToString(),Dgv_Summary [ 2 ,i] .Value.ToString(),
stock,ProdPrice,dblAmount );
}
catch {}
// }
// }
}


// coutn ++;
< span class =code-comment> // 尝试
// {
// Pr_Bar.Value = coutn ;
// }
// catch {}
// Pr_Bar.Visible = false;
Dgv_Summary.DataSource = ProTable;
Dgv_Summary.Columns [ 产品代码]。宽度= 100 ;
Dgv_Summary.Columns [ ProdDesc]。AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
Dgv_Summary.Columns [ UOM]。宽度= 100 ;
Dgv_Summary.Columns [ OnHand]。宽度= 100 ;
Dgv_Summary.Columns [ Price]。宽度= 100 ;
Dgv_Summary.Columns [ 金额]。宽度= 100 ;
Dgv_Summary.Columns [ OnHand]。DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
Dgv_Summary.Columns [ Price]。DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
Dgv_Summary.Columns [ Amount]。DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
// lbl_TotalAmount.Text = ProTable.Compute(Convert.ToString(Sum(Amount)), ).ToString();
// DataRow [] dr = ProTable.Select (SUM(Amount));
// lbl_TotalAmount.Text = Convert.ToString (dr [0]);
decimal sum = 0 ;
foreach (DataRow dr in ProTable.Rows)
{
sum + = Convert.ToDecimal(dr [ Amount]。ToString());
}
lbl_TotalAmount.Text = sum.ToString();
}
public double IsNumber( string strData)
{
double Num;
double fnum = 0 ;
布尔 isNum = double .TryParse(strData, out Num);
if (isNum == true
fnum = Convert.ToDouble(strData );
return fnum;

}







存储程序

  ALTER   procedure  [dbo]。[SP_StockReport_Category] ​​
< span class =code-sdkkeyword> @ prod_cate nvarchar 30

as begin
select prod_code as [产品代码],prod_desc as [描述],prod_uom as UOM,prod_max 来自产品
其中 prod_cate = @ prod_cate comp_id = 1 prod_deleted = 0 prod_stock = 1 prod_suspend = 0 订单 prod_code

end





DAL

< pre lang =cs> public DataTable CategoryData(ProductBEL probel, int stock, double dblAmount)
{
if (cn.State!= ConnectionState.Open)
cn.Open();
cmd.Connection = cn;
cmd = new SqlCommand( SP_StockReport_Category ,cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ prod_cate,probel.prod_cate);
// cmd.Parameters.AddWithValue(@ prodcode,prod_code);
sda = new SqlDataAdapter(cmd);
ds = new DataSet();
sda.Fill(ds, products);

return ds.Tables [ 产品];

}
public double prodmax(ProductBEL probel, double prodmax)
{
if (cn.State!= ConnectionState.Open)
cn.Open();
cmd.Connection = cn;
cmd = new SqlCommand( SP_StockReport_Category ,cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ prod_cate,probel.prod_cate);
// cmd.Parameters.AddWithValue(@ prodcode,prod_code);
tdr = cmd.ExecuteReader();
while (tdr.Read())
{
prodmax = Convert.ToDouble(tdr [ prod_max]。ToString());
}
tdr.Close();
return prodmax;


}

解决方案

您不能在一个double中返回多个值:这就像试图用一只手装十几个苹果一样!

双重持有一个单一值:如果你想要返回超过,那么你需要一个集合
$ b $某种形式的b:

  public  List< double> prodmax(ProductBEL probel)
{
if (cn.State!= ConnectionState.Open)
cn.Open();
cmd.Connection = cn;
cmd = new SqlCommand( SP_StockReport_Category ,cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ prod_cate,probel.prod_cate);
// cmd.Parameters.AddWithValue(@ prodcode,prod_code);
tdr = cmd.ExecuteReader();
列表< double>> prodmax = 列表> double<();
while (tdr.Read())
{
prodmax.Add(Convert.ToDouble(tdr [ prod_max]。ToString()));
}
tdr.Close();
return prodmax;
}


DataTable ProTable = new DataTable();
          ProTable.Columns.Add("Product Code", typeof(string));
          ProTable.Columns.Add("ProdDesc", typeof(string));
          ProTable.Columns.Add("UOM", typeof(string));
          ProTable.Columns.Add("OnHand", typeof(string));
          ProTable.Columns.Add("Price", typeof(string));
          ProTable.Columns.Add("Amount", typeof(string));

           //int coutn = 0;
           //Pr_Bar.Visible = true;
           //Pr_Bar.Minimum = 0;
           ////sqlstr = sdbll.Count(ppbel);
           ////Pr_Bar.Maximum =Convert.ToInt32(sdbll.get_data(sqlstr));
           //ProTable.Rows.Clear();
           probel.prod_cate = Ddl_Brand.Text;
          Dgv_Summary.DataSource = srbll.CategoryData(probel, stock , dblAmount);
          prodmax = srbll.prodmax(probel, prod_max);
          for (int i = 0; i < Dgv_Summary.RowCount; i++)
          {
              //for (int j = 2; j < Dgv_Summary.ColumnCount; j++)
              //{
              //    if (Dgv_Summary[j, i].Value != null)
              //    {
                      try
                      {
                          double ProdPrice =IsNumber(prodmax.ToString());
                          stock = int.Parse(srbll.getonhand(sqldateconverion(Dtp_ToDate.Value), Dgv_Summary[0, i].Value.ToString(), probel.comp_id.ToString()));
                          dblAmount = Convert.ToDouble(((stock * ProdPrice).ToString()));
                          ProTable.Rows.Add(Dgv_Summary[0, i].Value.ToString(), Dgv_Summary[1, i].Value.ToString(), Dgv_Summary[2, i].Value.ToString(),
                               stock, ProdPrice, dblAmount);
                      }
                      catch { }
              //    }
              //}
          }


          //coutn++;
          //try
          //{
          //    Pr_Bar.Value = coutn;
          //}
          //catch { }
          //Pr_Bar.Visible = false;
          Dgv_Summary.DataSource = ProTable;
          Dgv_Summary.Columns["Product Code"].Width = 100;
          Dgv_Summary.Columns["ProdDesc"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
          Dgv_Summary.Columns["UOM"].Width = 100;
          Dgv_Summary.Columns["OnHand"].Width = 100;
          Dgv_Summary.Columns["Price"].Width = 100;
          Dgv_Summary.Columns["Amount"].Width = 100;
          Dgv_Summary.Columns["OnHand"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
          Dgv_Summary.Columns["Price"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
          Dgv_Summary.Columns["Amount"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
          //lbl_TotalAmount.Text = ProTable.Compute(Convert.ToString("Sum(Amount)"), "").ToString();
          //DataRow[] dr = ProTable.Select("SUM(Amount)");
          //lbl_TotalAmount.Text = Convert.ToString(dr[0]);
         decimal sum = 0;
          foreach (DataRow dr in ProTable.Rows)
          {
              sum +=Convert.ToDecimal(dr["Amount"].ToString());
          }
          lbl_TotalAmount.Text = sum.ToString();
       }
       public double IsNumber(string strData)
       {
           double Num;
           double fnum = 0;
           Boolean isNum = double.TryParse(strData, out Num);
           if (isNum == true)
               fnum = Convert.ToDouble(strData);
           return fnum;

       }




STORED PROCEDURE

ALTER procedure [dbo].[SP_StockReport_Category]
@prod_cate nvarchar(30)

as begin
select prod_code as[Product Code], prod_desc as[Description], prod_uom as UOM ,prod_max from products
where prod_cate=@prod_cate  and comp_id=1 and prod_deleted=0 and prod_stock=1 and prod_suspend = 0 order by prod_code

end



DAL

public DataTable CategoryData(ProductBEL probel, int stock, double dblAmount)
    {
        if (cn.State != ConnectionState.Open)
            cn.Open();
        cmd.Connection = cn;
        cmd = new SqlCommand("SP_StockReport_Category", cn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@prod_cate", probel.prod_cate);
        //cmd.Parameters.AddWithValue("@prodcode", prod_code);
        sda = new SqlDataAdapter(cmd);
        ds = new DataSet();
        sda.Fill(ds, "products");

        return ds.Tables["products"];

    }
    public double prodmax(ProductBEL probel, double prodmax)
    {
        if (cn.State != ConnectionState.Open)
            cn.Open();
        cmd.Connection = cn;
        cmd = new SqlCommand("SP_StockReport_Category", cn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@prod_cate", probel.prod_cate);
        //cmd.Parameters.AddWithValue("@prodcode",prod_code);
        tdr = cmd.ExecuteReader();
        while (tdr.Read())
        {
            prodmax =Convert.ToDouble( tdr["prod_max"].ToString());
        }
        tdr.Close();
        return prodmax;


    }

解决方案

You can't return more than one value in a single double: that's like trying to fit a dozen apples in one hand!
A double holds one single value: if you want to return more than than, then you would need a collection
of some form:

public List<double> prodmax(ProductBEL probel)
{
    if (cn.State != ConnectionState.Open)
        cn.Open();
    cmd.Connection = cn;
    cmd = new SqlCommand("SP_StockReport_Category", cn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@prod_cate", probel.prod_cate);
    //cmd.Parameters.AddWithValue("@prodcode",prod_code);
    tdr = cmd.ExecuteReader();
    List<double>>prodmax = new List>double<();
    while (tdr.Read())
    {
        prodmax.Add(Convert.ToDouble( tdr["prod_max"].ToString()));
    }
    tdr.Close();
    return prodmax;
}


这篇关于如何显示datareader的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆