本文介绍了获取结果的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表名更新属性是Date,Jan,feb,march,april ... dec
这意味着我有Date列和12几个月
问题是我必须根据日期选择3个月。假设如果日期为1,则应选择当前月份和最近2个月。从第1天到第31天,它应显示当前月份和过去两个月的结果。
所以问题是如何自动选择月份。
我当前的查询是
I have a table name updates property is Date,Jan,feb,march,april...dec
That means that i have the Date column and 12 months
The question is that i have to select 3 months on the bases of the date. Suppose the if the Date is 1 it should select the current month and last 2 months. That is for from date 1 to 31 it should display the results for the current months and last two months.
so question is how can the months could be selected automatically.
My current query is
SqlCommand command = new SqlCommand("SELECT Date_,Decm,Jan,Feb from updates order by Date_", con);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
推荐答案
public static string GetMonths(int month)
{
string[] monthColumns = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
string monthName = string.Empty;
//// Get current month with next two months
//monthName = monthColumns[month - 1] + ", "
// + monthColumns[(12 + (((month) > 11) ? 0 : month)) - 12] + ", "
// + monthColumns[(12 + (((month + 1) > 11) ? (month + 1) - 12 : month + 1)) - 12];
// Get current month with previous month
monthName = monthColumns[((month - 3) < 0) ? (12 + (month - 3)) : month - 3] + ", "
+ monthColumns[((month - 2) < 0) ? (12 + (month - 2)) : month - 2] + ", "
+ monthColumns[month-1];
return monthName;
}
接下来调用上面的方法:
Next call the above method:
string sqlQuery = "SELECT Date_, "+ GetMonths(DateTime.Now.Month)+ " FROM updates order by Date_";
// Next execute above sqlQuery as per your code
SqlCommand command = new SqlCommand(sqlQuery, con);
SqlDataAdapter da = new SqlDataAdapter(command);
这篇关于获取结果的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!