当前位置:编程学习 > C#/ASP.NET >>

为何此处的foreach语句不执行

this.windConnection = new SqlConnection("Server=localhost; User=sa; PWD=111111;database=DFZQ;Asynchronous Processing=true");
            this.windConnection.Open();
            this.windCommand = this.windConnection.CreateCommand();

windCommand.CommandText = "SELECT  stockCode,sum(volume) FROM [DFZQ].[dbo].[fundPortfolio] where stopDate='" + tradingDate + "'group by stockCode";
            SqlDataReader sdr5 = windCommand.ExecuteReader();
            Dictionary<string, double> getSumStockVolume1 = new Dictionary<string, double>();
            foreach (KeyValuePair<string, double> pair in getSumStockVolume1)
            {
                string stockCode = (string)sdr5["stockCode"];
                double sumStockVolume1 = (double)sdr5["sum(volume)"];
                getSumStockVolume1.Add(stockCode, sumStockVolume1);
            }
            sdr5.Close();
            return getSumStockVolume1; --------------------编程问答-------------------- ctionary<string, double> getSumStockVolume1 = new Dictionary<string, double>();
foreach (KeyValuePair<string, double> pair in getSumStockVolume1)
{

}
getSumStockVolume1中没有数据... --------------------编程问答-------------------- getSumStockVolume1 没有数据怎么会执行。

你要先有返回的数据集才行 --------------------编程问答-------------------- --------------------编程问答-------------------- ctionary<string, double> getSumStockVolume1 = new Dictionary<string, double>();
foreach (KeyValuePair<string, double> pair in getSumStockVolume1)
{

}
getSumStockVolume1是你new出来的,没有数据,为空。 --------------------编程问答--------------------
windCommand.CommandText = "SELECT stockCode,sum(volume) FROM [DFZQ].[dbo].[fundPortfolio] where stopDate='" + tradingDate + "'group by stockCode";
  SqlDataReader sdr5 = windCommand.ExecuteReader();
  Dictionary<string, double> getSumStockVolume1 = new Dictionary<string, double>();
  foreach (sdr5.Read())
  {
  string stockCode = (string)sdr5["stockCode"];
  double sumStockVolume1 = (double)sdr5["sum(volume)"];
  getSumStockVolume1.Add(stockCode, sumStockVolume1);
  }
--------------------编程问答-------------------- 失误!!!

windCommand.CommandText = "SELECT stockCode,sum(volume) FROM [DFZQ].[dbo].[fundPortfolio] where stopDate='" + tradingDate + "'group by stockCode";
  SqlDataReader sdr5 = windCommand.ExecuteReader();
  Dictionary<string, double> getSumStockVolume1 = new Dictionary<string, double>();
  While(sdr5.Read())
  {
  string stockCode = (string)sdr5["stockCode"];
  double sumStockVolume1 = (double)sdr5["sum(volume)"];
  getSumStockVolume1.Add(stockCode, sumStockVolume1);
  }
 这样就把它们放到字典dictionary里去了 --------------------编程问答-------------------- 漏了吧

while(sdr5.Read())
{
  String stockCode = String.Empty;
  if(!sdr5.IsDBNull(0))
  {
    stockCode = sdr5.GetString(0);
  }

  Int32 sum = -1;
  if(!sdr5.IsDBNull(1))
  {
    stockCode = sdr5.GetInt(1);
  }

  if(!String.IsNullOrEmpty(stockCode)
    && sum >0 )
  {
    getSumStockVolume1.Add(stockCode, sum);
  }
}
--------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,