ASP.NET绘制折线图---(2)获取数据

 1.从数据库中获取数据
          建立数据库连接并查询,可以专门新建一个C#类来提高代码复用率,也可以只写一个函数。下面的代码是我写的一个C#类

SQL_Connection,放在App_Code文件夹下,专门用来执行数据库操作,代码如下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
///公用的连接数据库的类
/// </summary>
public class SQL_Connection
{
    private string sql = null;

    public SQL_Connection(string sql)
    {
        this.sql = sql;
    }
    public DataSet query()
    {
        string ConStr = "Data Source=……;Initial Catalog=……;Persist SecurityInfo=True;UserID=……;Password=……";
        SqlConnection conn = new SqlConnection(ConStr);
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = this.sql;
        cmd.CommandTimeout = 10000;
        conn.Open();
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        conn.Close();
        return ds;
    }
}

 

      2.在后台的CS文件中执行数据库查询,并将结果放在数组中,具体代码如下:

        //查询数据库
        string sql = "select * from manqi_cx where manqpfl <>0 and manqpfl1<>0 and manqpfl2<>0 and bmid=2";
        SQL_Connection sqlCon = new SQL_Connection(sql);
        DataSet ds = sqlCon.query();

        //将结果放进数组中
        double[] manqpfl = new double[TotalMonths];
        double[] manqpfl1 = new double[TotalMonths];
        double[] manqpfl2 = new double[TotalMonths];
        for (int i = 0; i <ds.Tables[0].Rows.Count; i++)
        {
            manqpfl[i] = Convert.ToDouble(ds.Tables[0].Rows[i]["manqpfl"]);
            manqpfl1[i] = Convert.ToDouble(ds.Tables[0].Rows[i]["manqpfl1"]);
            manqpfl2[i] = Convert.ToDouble(ds.Tables[0].Rows[i]["manqpfl2"]);
        }

 

      本文为原创作品,转载请注明出处:http://www.cnblogs.com/luzx/

posted @ 2010-04-28 14:28  陆宗轩  阅读(1952)  评论(0编辑  收藏  举报