SqlDataReader读取数据库的内容不对 请帮我看看!!!

ms8248 2012-06-05 08:42:15
为了测试 我的数据库中只有一列 列名为:province
列中只有4个数据分别为10、20、50、20
本来我打算用ZEDGRAPH画个饼图 但是数据库的内容读取出来时 10、20、50、20、10、20、50、20重复了一遍 不知道为什么。后来我自己测试了下把循环中的其它部分注释掉。发现while(dr.read())循环了44次(也就是下面的i输出为44)这是怎么回事呢?我的数据库中只有4个数据啊。数据库连接没有问题,我把它绑定到GRIDVIEW上也能正常显示。各位帮帮忙!!!小弟谢过...

SqlConnection myconnection = new SqlConnection("server=WIN- 42QG6PPQFE7\\SQLEXPRESS;database=asd;Integrated Security=true");
SqlCommand mycommand = new SqlCommand("SELECT * FROM locate",myconnection);
myconnection.Open();
SqlDataReader dr = mycommand.ExecuteReader();
int i = 0;
while(dr.Read())
{

//int numbers = int.Parse(dr[0].ToString());
//string names = dr[0].ToString();
//画图
//PieItem segment = myPane.AddPieSlice(numbers, defaultColors[i], Color.White, 45f, 0, names);
//segment.LabelType = PieLabelType.Value;
i++;


}
Response.Write(i);
...全文
162 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ms8248 2012-06-05
  • 打赏
  • 举报
回复
谢谢大家 结贴了!
ms8248 2012-06-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

好像也不是直接写在pageload里面

应该是InitializeComponent在这里面写

private void InnitializeComponent()
{

this.ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(ZedGraphWeb1_Render……
[/Quote]
我找到错误了
哈哈 我找到错误了
this.ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraph)定义个别的方法就可以了。原来那个本来就是ZEDGRAPH的一个事件处理程序 所以就造成了重载。谢谢你哈
蝶恋花雨 2012-06-05
  • 打赏
  • 举报
回复
好像也不是直接写在pageload里面

应该是InitializeComponent在这里面写

private void InnitializeComponent()
{

this.ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(ZedGraphWeb1_RenderGraph);
}
试试。
ms8248 2012-06-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

是不是重复加载了



if(!IsPostBack)
{
this.ZedGraphWeb1.RenderGraph += new ZedGraph....
}
[/Quote]
我加了 还是不行 不知道为什么 不过还是谢谢你
JJYY0088 2012-06-05
  • 打赏
  • 举报
回复
是不是重复加载了



if(!IsPostBack)
{
this.ZedGraphWeb1.RenderGraph += new ZedGraph....
}
ms8248 2012-06-05
  • 打赏
  • 举报
回复
我把全部代码放上。我猜可能是ZedGraphWeb1_RenderGraph的问题 这个方法好像使while(dr.Read()==true)循环了两遍,至于为什么还请高手门帮忙解答。
因为我在这个方法后面 放了一个按钮CLICK事件来测试输出 这时候是正常的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZedGraph;
using ZedGraph.Web;
using System.Drawing;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication28
{
public partial class year : System.Web.UI.Page
{
//初始化颜色序列
List<Color> defaultColors = new List<Color>();
private void InitDefaultColors()
{
defaultColors.Add(Color.Red);
defaultColors.Add(Color.Green);
defaultColors.Add(Color.Blue);
defaultColors.Add(Color.Yellow);
defaultColors.Add(Color.YellowGreen);
defaultColors.Add(Color.Aqua);
defaultColors.Add(Color.Cyan);
defaultColors.Add(Color.DarkSeaGreen);
defaultColors.Add(Color.Indigo);
}
protected void Page_Load(object sender, EventArgs e)
{
this.ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(ZedGraphWeb1_RenderGraph);

}

protected void ZedGraphWeb1_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane)
{
InitDefaultColors();
GraphPane myPane =masterPane[0];
myPane.Title.Text = "用户所在地理位置统计";
//此处给ZEDGRAPH 背景填充颜色
myPane.Fill = new Fill(Color.Cornsilk);
myPane.Chart.Fill = new Fill(Color.Cornsilk);
myPane.Legend.Position = LegendPos.Right;
//
//连接数据库
//此处需要注意SQLEXPRESS实例前必须加上转义字符"/"否则会报错!
SqlConnection myconnection = new SqlConnection("server=WIN-42QG6PPQFE7\\SQLEXPRESS;database=asd;Integrated Security=true");
SqlCommand mycommand = new SqlCommand("SELECT province FROM locate",myconnection);
myconnection.Open();
SqlDataReader dr = mycommand.ExecuteReader();
int i = 0;
while(dr.Read()==true)
{


int numbers = int.Parse(dr[0].ToString());
string names = dr[0].ToString();
//画图
PieItem segment = myPane.AddPieSlice(numbers, defaultColors[i], Color.White, 45f, 0, names);
segment.LabelType = PieLabelType.Value;




}
dr.Close();
CurveList curves = myPane.CurveList;
masterPane.AxisChange(g);
}


ms8248 2012-06-05
  • 打赏
  • 举报
回复
我把全部代码放上。我猜可能是ZedGraphWeb1_RenderGraph的问题 这个方法好像使while(dr.Read()==true)循环了两遍,至于为什么还请高手门帮忙解答。
因为我在这个方法后面 放了一个按钮CLICK事件来测试输出 这时候是正常的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZedGraph;
using ZedGraph.Web;
using System.Drawing;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication28
{
public partial class year : System.Web.UI.Page
{
//初始化颜色序列
List<Color> defaultColors = new List<Color>();
private void InitDefaultColors()
{
defaultColors.Add(Color.Red);
defaultColors.Add(Color.Green);
defaultColors.Add(Color.Blue);
defaultColors.Add(Color.Yellow);
defaultColors.Add(Color.YellowGreen);
defaultColors.Add(Color.Aqua);
defaultColors.Add(Color.Cyan);
defaultColors.Add(Color.DarkSeaGreen);
defaultColors.Add(Color.Indigo);
}
protected void Page_Load(object sender, EventArgs e)
{
this.ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(ZedGraphWeb1_RenderGraph);

}

protected void ZedGraphWeb1_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane)
{
InitDefaultColors();
GraphPane myPane =masterPane[0];
myPane.Title.Text = "用户所在地理位置统计";
//此处给ZEDGRAPH 背景填充颜色
myPane.Fill = new Fill(Color.Cornsilk);
myPane.Chart.Fill = new Fill(Color.Cornsilk);
myPane.Legend.Position = LegendPos.Right;
//
//连接数据库
//此处需要注意SQLEXPRESS实例前必须加上转义字符"/"否则会报错!
SqlConnection myconnection = new SqlConnection("server=WIN-42QG6PPQFE7\\SQLEXPRESS;database=asd;Integrated Security=true");
SqlCommand mycommand = new SqlCommand("SELECT province FROM locate",myconnection);
myconnection.Open();
SqlDataReader dr = mycommand.ExecuteReader();
int i = 0;
while(dr.Read()==true)
{


int numbers = int.Parse(dr[0].ToString());
string names = dr[0].ToString();
//画图
PieItem segment = myPane.AddPieSlice(numbers, defaultColors[i], Color.White, 45f, 0, names);
segment.LabelType = PieLabelType.Value;




}
dr.Close();
CurveList curves = myPane.CurveList;
masterPane.AxisChange(g);
}


yuji821 2012-06-05
  • 打赏
  • 举报
回复
没问题啊

你在数据库中查询 SELECT * FROM locate 看看有多小条数据
蝶恋花雨 2012-06-05
  • 打赏
  • 举报
回复
只查询这列 。你的while看着没问题,
string queryString =
"SELECT OrderID, CustomerID FROM dbo.Orders;";

using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();

SqlDataReader reader = command.ExecuteReader();

// Call Read before accessing data.
while (reader.Read())
{
Console.WriteLine(String.Format("{0}, {1}",
reader[0], reader[1]));
}

// Call Close when done reading.
reader.Close();
}
蝶恋花雨 2012-06-05
  • 打赏
  • 举报
回复
SELECT province FROM locate

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧