您的位置:首页 > 编程语言 > C#

C#代码,查询

2017-04-12 16:25 169 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DDWaterAndRainfallSystem.common;
using System.Collections;
using DDWaterAndRainfallSystem.Common;
using DDWaterAndRainfallSystem.WaterAndRainfall.WaterRegimenQuery;
using Microsoft.Reporting.WinForms;
//水情查询==多年水情查询==窗体文件
namespace DDWaterAndRainfallSystem
{
public partial class DuoNianShuiQingFrm : Office2007Form
{
DataBase db;
public DuoNianShuiQingFrm()
{

db = new DataBase();
InitializeComponent();
TreeConstuctor ts = new TreeConstuctor();
ts.addSWTreeNode(treeViewStcd, "'RR','ZZ'");
treeViewStcd.ExpandAll();
}

private void DuoNianShuiQingFrm_Load(object sender, EventArgs e)
{
//设置下拉列表年份,默认显示当前年份(2014)以及近20年年份
string Year = DateTime.Now.Date.Year.ToString();
int intYear = Convert.ToInt32(Year) + 1;
int chaYear = intYear - 1990;//年数
string[] Yearlist;
Yearlist = new string[chaYear];
for (int i = 0; i < chaYear; i++)
{
Yearlist[i] = (intYear-i-1).ToString();
}
comboBox1.DataSource = Yearlist;
comboBox1.SelectedIndex = 0;

}
//默认查询所有,点击按钮根据年份和水域名称查询
private void btnQuery_Click(object sender, EventArgs e)
{
string yearValue = this.comboBox1.SelectedValue.ToString();
ArrayList alStcd = TreeSelect.getTreeSelectStcd(treeViewStcd);

this.onSearch(yearValue, alStcd);
}
private void onSearch(String yearValue, ArrayList waterName)
{
DataTable dt = this.fillData(yearValue, waterName);
string rootpath = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\"));
rootpath = rootpath.Substring(0, rootpath.LastIndexOf("\\"));
reportViewer1.LocalReport.ReportPath = rootpath + "\\WaterAndRainfall\\WaterRegimenQuery\\yearsWaterReport.rdlc";
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dt));
ReportParameter rp = new ReportParameter("title", " 测站多年水位");
this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp });
this.reportViewer1.RefreshReport();
this.reportViewer1.Visible = true;
}
//根据查询条件(年份和水域名称)获得查询结果
private DataTable fillData(String yearValue, ArrayList waterName)
{
String yearValue2 =Convert.ToString( Convert.ToInt32(yearValue) + 1);
String waterStcds = "'" + "0" + "',";
if (null != waterName && waterName.Count > 0) {
for (int i = 0; i < waterName.Count; i++) {
waterStcds += "'"+waterName[i]+"'" + ",";
}
}
waterStcds = waterStcds.Trim().Substring(0, waterStcds.Length - 1);
// MessageBox.Show(waterStcds);
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("year"));//年份
dt.Columns.Add(new DataColumn("water"));//测站名称
dt.Columns.Add(new DataColumn("avg"));//年平均水位
dt.Columns.Add(new DataColumn("max"));//年最高水位
dt.Columns.Add(new DataColumn("min"));//年最低水位
db.open();
try
{

string sql = "select rtrim(b.stnm) as water, " +
" (select top 1 DatePart(YEAR,t0.tm) from st_rsvr_r t0 where t0.stcd=b.stcd and  t0.tm>='" + yearValue + "/1/1" + "' and t0.tm<='" + yearValue2 + "/1/1" + "' ) as year," +
" (select  cast(round(avg(t1.rz),2) as decimal(10,2)) from st_rsvr_r t1 where t1.stcd=b.stcd and  t1.tm>='" + yearValue + "/1/1" + "' and t1.tm<='" + yearValue2 + "/1/1" + "' ) as avg," +
" (select  cast(round(max(t2.rz),2) as decimal(10,2)) from st_rsvr_r t2 where t2.stcd=b.stcd and  t2.tm>='" + yearValue + "/1/1" + "' and t2.tm<='" + yearValue2 + "/1/1" + "' ) as max," +
" (select  cast(round(min(t3.rz),2) as decimal(10,2)) from st_rsvr_r t3 where t3.stcd=b.stcd and  t3.tm>='" + yearValue + "/1/1" + "' and t3.tm<='" + yearValue2 + "/1/1" + "' )  as min " +
" from st_stbprp_b  b where b.sttp in('RR','ZZ') and b.stcd in(" + waterStcds + ")  ";
dt = db.getDataTable(sql);
return dt;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
throw;
}
finally
{
if (db.connection.State == ConnectionState.Open)
db.close();
}

}

private void treeViewStcd_AfterCheck(object sender, TreeViewEventArgs e)
{
TreeSelect.afterCheck(sender, e);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: