- 积分
- 308
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2012-7-1
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace tester
{
public class getsql
{
/// <summary>
/// 返回列表sql,但站单要素多时次。
/// </summary>
/// <param name="tday">日期,yyyyMMdd格式</param>
/// <param name="sid">站点id</param>
/// <param name="zdname">查询字段名</param>
/// <returns></returns>
public string getsqlstr(string tday ,string sid,string zdname)
{
string sql = @"select
right([ObservTimes],2) as [时次/日期],";
string sqlw = "";
string sqlm = "";
DateTime mydatetime;
mydatetime = DateTime.ParseExact(tday, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
for (int i = 0; i < 15; i++)
{
string day = mydatetime.AddDays(i * -1).ToString("yyyyMMdd");
sqlw += "[" + day + "]" + "=max(case when left([ObservTimes],8)='" + day + "' then [" + zdname + "] else null end),";
for (int k = 0; k < 24; k++)
{
sqlm += "'" + day + k.ToString("00") + "',";
}
}
sqlw += "[" + mydatetime.AddDays(-15).ToString("yyyyMMdd") + "]" +
"=max(case when left([ObservTimes],8)='" + DateTime.Now.AddDays(-15).ToString("yyyyMMdd") +
"' then [DryBulTemp] else null end)";
for (int k = 0; k < 23; k++)
{
sqlm += "'" + mydatetime.AddDays(-15).ToString("yyyyMMdd") + k.ToString("00") + "',";
}
sqlm += "'" + mydatetime.AddDays(-15).ToString("yyyyMMdd") + "23'";
sql += sqlw + @"
FROM
[ELEMENT].[dbo].[tabTimeData]
where
[ObservTimes]in(" + sqlm + @") and [StationNum]='"+sid+@"'
group by right([ObservTimes],2)";
return sql;
}
/// <summary>
/// 获取多站单数据25时次
/// </summary>
/// <param name="tday">时间(yyyyMMddHH)</param>
/// <param name="zdname">字段名</param>
/// <returns></returns>
public string getsqlstan(string tday, string zdname)
{
string sql = @"select
[StationNum] as [站点/时次],";
string sqlw = "";
string sqlm = ""; DateTime mydatetime;
mydatetime = DateTime.ParseExact(tday, "yyyyMMddHH", System.Globalization.CultureInfo.InvariantCulture);
for (int i = 0; i < 24; i++)
{
string day = mydatetime.AddHours(i * -1).ToString("yyyyMMddHH");
sqlw += "[" + mydatetime.AddHours(i * -1).ToString("dd日HH时") + "]" + "=max(case when [ObservTimes]='" + day + "' then [" + zdname + "] else null end),";
}
sqlw += "[" + mydatetime.AddHours(-24).ToString("dd日HH时") + "]" + "=max(case when [ObservTimes]='" + mydatetime.AddHours(-23).ToString("yyyyMMddHH") + "' then [DryBulTemp] else null end)";
for (int k = 0; k < 24; k++)
{
sqlm += "'" + mydatetime.AddHours(k * -1).ToString("yyyyMMddHH") + "',";
}
sqlm += "'" + mydatetime.AddHours(-24).ToString("yyyyMMddHH") + "'";
sql += sqlw + @"
FROM
[ELEMENT].[dbo].[tabTimeData]
where
[ObservTimes]in(" + sqlm + @")
group by [StationNum]";
return sql;
}
}
}
以上类返回sql为自动站数据查询自动转换行转列,可设定查询数据,可设定站点、查询时间等。具体相关设定自己可以在where中调整。
查询速度中等,查询后结果用:
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection
("server=数据库地址;database=数据库;uid=用户名;pwd=密码");
System.Data.SqlClient.SqlDataAdapter sdb = new System.Data.SqlClient.SqlDataAdapter((new getsql()).getsqlstr(DateTime.Now.ToString("yyyyMMdd"), "区站号", "查询字段"), conn);
System.Data.SqlClient.SqlDataAdapter sdbtan = new System.Data.SqlClient.SqlDataAdapter((new getsql()).getsqlstan(DateTime.Now.ToString("yyyyMMddHH"), "DryBulTemp"), conn);
System.Data.DataSet dst = new System.Data.DataSet();
System.Data.DataSet dstan = new System.Data.DataSet();
sdb.Fill(dst, "mytab");
sdbtan.Fill(dstan, "mytab");
GridView1.DataSource = dst;
GridView1.DataBind();
GridView2.DataSource = dstan;
GridView2.DataBind();
绑定到GridView即可实现输出。
节省大家用forecah或其他循环处理数据的时间。 |
评分
-
查看全部评分
|