OleDbConnection odcConnection = new OleDbConnection(MyConnectionString); //打开连接 C#操作Access之按列读取mdb
odcConnection.Open();
OleDbCommand odCommandValue = odcConnection.CreateCommand();//value
OleDbCommand odCommandYear = odcConnection.CreateCommand();//year odCommandValue.CommandText = "select 全年平均 from 洱海各月出流流量"; //读取全年平均项,建立读取 C#操作Access之按列读取mdb
odCommandYear.CommandText = "select 年 from 洱海各月出流流量"; //读取year OleDbDataReader odrReaderValue = odCommandValue.ExecuteReader();//建立SQL查询value
OleDbDataReader odrReaderYear = odCommandYear.ExecuteReader();//建立SQL查询year //ArrayList ArrValue = new ArrayList();//定义动态数组ArrValue
//ArrayList ArrYear = new ArrayList();//定义动态数组ArrYear //double []Yearavrg;//数组定义
//Yearavrg = new double[100];//初始化大小,分配内存
//double []Year;//数组定义
//Year = new double[100]; double[] ArrValue;//数组定义
ArrValue = new double[];//初始化大小,分配内存
double[] ArrYear;//数组定义
ArrYear = new double[];
int im = , jm = ;
while (odrReaderValue.Read())
{
//ArrValue.Add(odrReaderValue["全年平均"].ToString());//读取列的每一个
ArrValue[im] = Convert.ToDouble(odrReaderValue["全年平均"]);
im++;
}
while (odrReaderYear.Read())
{
//ArrYear.Add(odrReaderYear["年"].ToString());//读取列的每一个
ArrYear[jm] = Convert.ToDouble(odrReaderYear["年"]);
jm++;
} odrReaderValue.Close();
odrReaderYear.Close();
odcConnection.Close();
//test
//int leng = ArrValue.Count;//计算数组长度
int leng = ArrValue.Length; // MessageBox.Show(leng.ToString());//test
GraphPane MyPane=zedGraphControl1.GraphPane;
MyPane.Title.Text = "洱海年平均出流流量";
MyPane.XAxis.Title.Text = "年份";
MyPane.YAxis.Title.Text = "流量(立方米每秒)"; //
PointPairList pplist = new PointPairList();//显示点数组
//test-OK
foreach (double a in ArrValue)
{ textBox1.Text += a;
//pplist.Add(0,b); } //test-OK
//foreach (string a in ArrYear)
//foreach (string a in ArrValue)
//{
// //MessageBox.Show(a);
// double b = double.Parse(a);//类型转换
// textBox1.Text += b;//test
//} //foreach (string a in ArrValue)
//{
// //MessageBox.Show(a);
// int i = 0;
// Yearavrg[i] = double.Parse(a);//类型转换
// i++; //} //foreach (string b in ArrYear)
//{
// //MessageBox.Show(a);
// int i = 0;
// Year[i] = double.Parse(b);//类型转换
// i++;
// textBox1.Text+=Year[i].ToString(); //} for (int i = ; i < leng; i++)
{ pplist.Add(ArrYear[i], ArrValue[i]);//xaile是年,yaile是年平均流量值
} LineItem myCurve = MyPane.AddCurve("流量",pplist,Color.Red,SymbolType.Diamond);//标注样式 myCurve.Symbol.Fill = new Fill(Color.White);//背景填充白色
MyPane.XAxis.MajorGrid.IsVisible = true;//显示X网格
MyPane.YAxis.MajorGrid.IsZeroLine = true;//Y坐标不显示0坐标线 // Align the Y axis labels so they are flush to the axis
MyPane.YAxis.Scale.Align = AlignP.Inside; //坐标范围划定
MyPane.YAxis.Scale.Min = -;
MyPane.YAxis.Scale.Max = ; MyPane.XAxis.Scale.Min = ;
MyPane.XAxis.Scale.Max = ; MyPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f); // Add a text box with instructions
TextObj text = new TextObj(
"左键拖拽放大\n鼠标中键滚放缩\n右键菜单",
0.05f, 0.95f, CoordType.ChartFraction, AlignH.Left, AlignV.Bottom);
text.FontSpec.StringAlignment = StringAlignment.Near;
MyPane.GraphObjList.Add(text); zedGraphControl1.IsShowHScrollBar = true;
zedGraphControl1.IsShowVScrollBar = true;
zedGraphControl1.IsAutoScrollRange = true; zedGraphControl1.IsShowPointValues = true;
zedGraphControl1.PointValueEvent += new ZedGraphControl.PointValueHandler(MyPointValueHandler);
// OPTIONAL: Add a custom context menu item
// zedGraphControl1.ContextMenuBuilder += new ZedGraphControl.ContextMenuBuilderEventHandler(MyContextMenuBuilder); // OPTIONAL: Handle the Zoom Event
// zedGraphControl1.ZoomEvent += new ZedGraphControl.ZoomEventHandler(MyZoomEvent); // Tell ZedGraph to calculate the axis ranges
// Note that you MUST call this after enabling IsAutoScrollRange, since AxisChange() sets
// up the proper scrolling parameters
zedGraphControl1.AxisChange();
// Make sure the Graph gets redrawn
zedGraphControl1.Invalidate();
}
/// <summary>
/// Display customized tooltips when the mouse hovers over a point
/// </summary>
private string MyPointValueHandler(ZedGraphControl control, GraphPane pane,CurveItem curve, int iPt)
{
// Get the PointPair that is under the mouse
PointPair pt = curve[iPt]; // return curve.Label.Text + " is " + pt.Y.ToString("f2") + " m3/s at " + pt.X.ToString("f1") + " 年";
//return pt.X.ToString("f1") + "年"+curve.Label.Text + "是" + pt.Y.ToString("f2") + " wm3/s";
return pt.X.ToString() + "年" + curve.Label.Text + "是" + pt.Y.ToString("f2") + " wm3/s";//OKZWJ
} /// <summary>
/// Customize the context menu by adding a new item to the end of the menu
/// </summary>
private void MyContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip,
Point mousePt, ZedGraphControl.ContextMenuObjectState objState)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Name = "add-beta";
item.Tag = "add-beta";
item.Text = "Add a new Beta Point";
item.Click += new System.EventHandler(AddBetaPoint); menuStrip.Items.Add(item);
} /// <summary>
/// Handle the "Add New Beta Point" context menu item. This finds the curve with
/// the CurveItem.Label = "Beta", and adds a new point to it.
/// </summary>
private void AddBetaPoint(object sender, EventArgs args)
{
// Get a reference to the "Beta" curve IPointListEdit
IPointListEdit ip = zedGraphControl1.GraphPane.CurveList["Beta"].Points as IPointListEdit;
if (ip != null)
{
double x = ip.Count * 5.0;
double y = Math.Sin(ip.Count * Math.PI / 15.0) * 16.0 * 13.5;
ip.Add(x, y);
zedGraphControl1.AxisChange();
zedGraphControl1.Refresh();
}
} // Respond to a Zoom Event
private void MyZoomEvent(ZedGraphControl control, ZoomState oldState,ZoomState newState)
{
// Here we get notification everytime the user zooms
} }
调试蛮辛苦的,尝试了很多次,各种改变,最后终于可以了!!!