Asp.net图表...将数据库中的日期(数量)转换为月份(名称) [英] Asp.net chart...convert date(in number) from database into month(name)

查看:74
本文介绍了Asp.net图表...将数据库中的日期(数量)转换为月份(名称)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个asp.net图表,其中日期是从X轴上的数据库中获取的...现在我想要的是,从数据库中提取的日期有点像这样2016-01-01 ...我想只用2016-01-01这样的年份显示数字年份,应该像Jan2016。



我尝试了我的方法,但我收到了这个错误:

 字符串未被识别 一个有效的DateTime 



 我想要的图表(Link  for  image)。





我尝试过:



  protected   void  Page_Load( object  sender,EventArgs e)
{



if (!IsPostBack)
{
DataTable dt = new DataTable();
使用(OdbcConnection con = new OdbcConnection(ConfigurationManager.ConnectionStrings [ connect1]。ConnectionString))
{
con.Open();
OdbcCommand cmd = new OdbcCommand( select left(a.date,10)As Date,round(sum(a.mins),2)as MIns from summarydatashort

a,clientsrecord b其中a.orig_clli = b.orig_clli和a.date like '2016%'和b.emplye ='Ankit'组左边

(a.date,7),b.emplye
,con);
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
da.Fill(dt);
con.Close();
}



DateTime [] x = new DateTime [dt.Rows.Count];

int [] y = new INT [dt.Rows.Count];

for int i = 0 ; i < dt.Rows.Count; i ++)
{

CultureInfo ci = new CultureInfo( en-US);
x [i] = DateTime.ParseExact(dt.Rows [i] [ 1 ]。ToString(), MMMM,ci);

y [i] = Convert.ToInt32(dt.Rows [i] [ 1 ]);


}

Chart1.Series [ 0 ]。Points.DataBindXY(x,y) ;

}
}

解决方案

我只是自己解决.. ..我做的是我改变了我的查询,而不是从数据库中选择数字日期......我使用mysql的DATEFORMAT标签在月份名称中更改了它。





如下:



 选择 CAST(DATE_FORMAT(a.date,' %D%M')AS CHAR(  20 ))作为日期,总和(a.mins) MIns 来自 summarydatashort a,clientsrecord b 其中​​ a.orig_clli = b.orig_clli和a.date,如'   2016-08%'
b.emplye = ' Mridul' group by a.date,b.emplye


I am trying to make a asp.net chart where date is fetched from database on X-Axis...now what i want is to, the date that is fetched from database is somewhat like this "2016-01-01"...i want to display only month in words with year in number like for "2016-01-01" it should be like "Jan2016".

I tried my ways but i am getting this error:

String was not recognized as a valid DateTime


This is how i want the graph(Link for image).



What I have tried:

protected void Page_Load(object sender, EventArgs e)
    {
        


         if(!IsPostBack)
{
DataTable dt = new DataTable();
using (OdbcConnection con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connect1"].ConnectionString))
{
con.Open();
OdbcCommand cmd = new OdbcCommand("select left(a.date,10) As Date,round(sum(a.mins),2) as MIns from summarydatashort 

a,clientsrecord b where a.orig_clli=b.orig_clli and a.date like'2016%' and  b.emplye='Ankit'  group by left

(a.date,7),b.emplye", con);
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
da.Fill(dt);
con.Close();
}



DateTime[] x = new DateTime[dt.Rows.Count];

int [] y = new int[dt.Rows.Count];

for (int i = 0; i < dt.Rows.Count; i++)
{
    
    CultureInfo ci = new CultureInfo("en-US");
    x[i] = DateTime.ParseExact(dt.Rows[i][1].ToString(), "MMMM", ci);
 
    y[i] = Convert.ToInt32(dt.Rows[i][1]);
   
  
}

Chart1.Series[0].Points.DataBindXY(x,y);

}
}

解决方案

I just solve it by myself....What i did is that i changed my query and instead of picking the date in numeric from database... i changed it in month name using DATEFORMAT tag of mysql.


some how like this:

select CAST(DATE_FORMAT(a.date,'%D %M')AS CHAR(20)) As Date,sum(a.mins) as MIns from summarydatashort a,clientsrecord b where a.orig_clli=b.orig_clli and a.date like'2016-08%' and
b.emplye='Mridul'  group by a.date,b.emplye


这篇关于Asp.net图表...将数据库中的日期(数量)转换为月份(名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆