C#代码,查询



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);
        }




          
    }
}

          
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值