C# 将存储过程中的数据添加到图表中

C# 将存储过程中的数据添加到图表中,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我正在尝试绘制一个aspx折线图。我有一个存储过程中的数据,我想把它绘制在图表上 正如您在下面看到的,我从名为“ConsoleSales”的存储过程中获取数据,并将该数据放入数据集。我还为图表的X轴创建了一个名为months的类。我想显示从1月到12月的月份,当然,对于Y轴,我想显示存储过程数据。我怎样才能做到这一点?我在谷歌上找不到任何有用的东西,有什么建议吗 以下是我的标记代码: <asp:Chart ID="Chart1" runat="server" Height="296px"

我正在尝试绘制一个aspx折线图。我有一个存储过程中的数据,我想把它绘制在图表上

正如您在下面看到的,我从名为“ConsoleSales”的存储过程中获取数据,并将该数据放入数据集。我还为图表的X轴创建了一个名为months的类。我想显示从1月到12月的月份,当然,对于Y轴,我想显示存储过程数据。我怎样才能做到这一点?我在谷歌上找不到任何有用的东西,有什么建议吗

以下是我的标记代码:

 <asp:Chart ID="Chart1" runat="server" Height="296px" Width="500px" BorderDashStyle="Solid"
        BackSecondaryColor="White" BackGradientStyle="TopBottom" BorderWidth="2px" BackColor="211, 223, 240"
        BorderColor="#1A3B69">
        <Titles>
            <asp:Title Text="Title of the Graph comes here" />
        </Titles>
        <Series>
            <asp:Series Name="Series1" BorderColor="180, 26, 59, 105" ChartType="Line">
            </asp:Series>
        </Series>
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid"
                BackSecondaryColor="White" BackColor="64, 165, 191, 228" ShadowColor="Transparent"
                BackGradientStyle="TopBottom">
                <Area3DStyle Rotation="10" Perspective="10" Inclination="15" IsRightAngleAxes="False"
                    WallWidth="0" IsClustered="False"></Area3DStyle>
                <AxisY LineColor="64, 64, 64, 64">
                    <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
                    <MajorGrid LineColor="64, 64, 64, 64" />
                </AxisY>
                <AxisX LineColor="64, 64, 64, 64">
                    <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
                    <MajorGrid LineColor="64, 64, 64, 64" />
                </AxisX>
            </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>

这是我的CS代码:

     public class Months
    {
        public string MonthsOfTheYear { get; set; }
    }


protected void Page_Load(object sender, EventArgs e)
    {

        var monthsList = new List<Months>()
        {
            new Months() {MonthsOfTheYear = "Jan"},
            new Months() {MonthsOfTheYear = "Feb"},
            new Months() {MonthsOfTheYear = "March"},
            new Months() {MonthsOfTheYear = "April"},
            new Months() {MonthsOfTheYear = "May"},
            new Months() {MonthsOfTheYear = "Jun"},
            new Months() {MonthsOfTheYear = "Jul"},
            new Months() {MonthsOfTheYear = "Aug"},
            new Months() {MonthsOfTheYear = "Sep"},
            new Months() {MonthsOfTheYear = "Oct"},
            new Months() {MonthsOfTheYear = "Nov"},
            new Months() {MonthsOfTheYear = "Dec"}
        };


        DateTime startDate = DateTime.Now;
        DateTime endDate = DateTime.Now.AddYears(-1);

        string cS = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cS))
        {
            SqlDataAdapter da = new SqlDataAdapter("[ConsoleSales]", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add(new SqlParameter("@StartDate", startDate));
            da.SelectCommand.Parameters.Add(new SqlParameter("@EndDate", endDate));
            DataSet ds = new DataSet();
            da.Fill(ds);


            Chart1.DataSource = ds.Tables[0];

            Chart1.Series["Series1"].YValueMembers = "TimeOfSales";
            Chart1.Series["Series1"].XValueMember = monthsList.ToString() ;

       }
   }
公共课月份
{
公共字符串monthsof year{get;set;}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
var monthsList=新列表()
{
新月份(){monthsof year=“Jan”},
新月份(){monthsof year=“Feb”},
新月份(){monthsof year=“三月”},
新月份(){monthsof year=“April”},
新月份(){monthsof year=“May”},
新月份(){monthsof year=“Jun”},
新月份(){monthsof year=“Jul”},
新月份(){monthsof year=“Aug”},
新月份(){monthsof year=“Sep”},
新月份(){monthsof year=“Oct”},
新月份(){monthsof year=“Nov”},
新月份(){monthsof year=“Dec”}
};
DateTime startDate=DateTime.Now;
DateTime endDate=DateTime.Now.AddYears(-1);
字符串cS=ConfigurationManager.connectionString[“connectionString”].connectionString;
使用(SqlConnection con=newsqlconnection(cS))
{
SqlDataAdapter da=新的SqlDataAdapter(“[ConsoleSales]”,con);
da.SelectCommand.CommandType=CommandType.StoredProcess;
da.SelectCommand.Parameters.Add(新的SqlParameter(“@StartDate”,StartDate));
da.SelectCommand.Parameters.Add(新的SqlParameter(“@EndDate”,EndDate));
数据集ds=新数据集();
da.填充(ds);
Chart1.DataSource=ds.Tables[0];
图表1.系列[“系列1”]。YValueMembers=“销售时间”;
Chart1.Series[“Series1”].XValueMember=monthsList.ToString();
}
}

请尝试以下操作

SqlConnection connection = new SqlConnection("......");

SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "<<<Enter your stored Procedure Name>>>";

// create sql parameter if your procedure expects any input
SqlParameter param1 = new SqlParameter("@spParam1",SqlDbType.NVarChar);

// add parameters to parameters collection
command.Parameters.Add(param1);

// you can define more parameters based on your Stored Procedure's design

// set this parameter to a value we would like to set
command.Parameters["@spParam1"].Value = "<<Input goes here...>>"; 

// open connection
command.Connection.Open();

// populate data reader with return data result set 
// and close connection after populating data set
SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);

// assign data source to Chart
chart1.DataSource = reader;

// Set series data source to stored procedures returned data set's columns
chart1.Series[0].ValueMemberX = "ProductName";
chart1.Series[0].ValueMembersY = "TotalPurchase";

// data bind chart
chart1.DataBind(); 
SqlConnection-connection=newsqlconnection(“…”);
SqlCommand=newsqlcommand();
command.Connection=连接;
command.CommandType=CommandType.storedProcess;
command.CommandText=“”;
//如果过程需要任何输入,请创建sql参数
SqlParameter param1=新的SqlParameter(“@spParam1”,SqlDbType.NVarChar);
//将参数添加到参数集合
command.Parameters.Add(param1);
//您可以根据存储过程的设计定义更多参数
//将此参数设置为我们想要设置的值
command.Parameters[“@spParam1”].Value=”“;
//开放连接
command.Connection.Open();
//用返回数据结果集填充数据读取器
//并在填充数据集后关闭连接
SqlDataReader=command.ExecuteReader(CommandBehavior.CloseConnection);
//将数据源分配给图表
图表1.DataSource=读取器;
//将series数据源设置为存储过程返回的数据集列
chart1.Series[0]。ValueMemberX=“ProductName”;
图表1.系列[0]。ValueMembersY=“TotalPurchase”;
//数据绑定图
图1.DataBind();
它可以工作

protected void Chart1_Load(object sender, EventArgs e)
{


    SqlCommand cmd = new SqlCommand("sp_chart", Cn); // Definir cmd
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@id_district", SqlDbType.Int);
    cmd.Parameters["@id_district"].Value = 1;

    Cn.Open();

    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    Graficas.DataSource = reader;

    Graficas.Series[0].XValueMember = "name";
    Graficas.Series[0].YValueMembers = "vote";

    Graficas.DataBind();

    reader.Close();
    Cn.Close();


}

欢迎使用StackOverflow:如果您发布代码、XML或数据示例,请在文本编辑器中突出显示这些行,并单击编辑器工具栏上的“代码示例”按钮(
{}
),以精确地格式化和语法突出显示它!