码迷,mamicode.com
首页 > 其他好文 > 详细

ReportView动态加载带参数的RDCL文件

时间:2014-04-29 17:16:47      阅读:409      评论:0      收藏:0      [点我收藏+]

标签:winform   style   blog   http   java   color   

在vs里新建一个winform程序"ReportViewTest",在form1中添加一个reportView控件,from1的load事件如下:

mamicode.com,码迷
 private void Form1_Load(object sender, EventArgs e)
        {

            DataSet ds = new DataSet();
            try
            {
                ds = getDS();
            }
            catch (Exception)
            {

                throw;
            }
            Microsoft.Reporting.WinForms.ReportDataSource r = new Microsoft.Reporting.WinForms.ReportDataSource();
            r.Name = "Dev_LandaHRM";
            r.Value = ds.Tables[0];
            this.reportViewer1.LocalReport.DataSources.Add(r);
            this.reportViewer1.RefreshReport();
        }
mamicode.com,码迷

 得到报表数据源代码如下:

mamicode.com,码迷
 DataSet getDS()
        {
            string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionSQL"].ToString();
            SqlConnection conn = new SqlConnection(connStr);
            //读取sql
            XmlDocument xmldoc = new XmlDocument();
            string fileName = System.Configuration.ConfigurationManager.AppSettings["file2"].ToString();
            xmldoc.Load(@"../../" + fileName);
            this.reportViewer1.LocalReport.ReportPath = @"../../" + fileName;
            XmlNodeList sqlM = xmldoc.GetElementsByTagName("CommandText");
            string sql = sqlM[0].InnerXml.ToString();
            XmlNodeList canshu = xmldoc.GetElementsByTagName("QueryParameter");
            //获取数据源
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            List<string> list = new List<string>();
            list.Add("11887307");
            list.Add("建筑系");
            //如果有参数 传参数
            if (canshu.Count > 0)
            {
                XmlNodeList canshuList = xmldoc.GetElementsByTagName("Prompt");
                if (canshuList.Count > 0)
                {
                    for (int i = 0; i < canshuList.Count; i++)
                    {
                        //XmlNodeList typeList = xmldoc.GetElementsByTagName("DataType");
                        //if (typeList[i].InnerXml.ToString().Equals("DateTime"))
                        //{
                        //    DateTime datetime = DateTime.Parse(list[i]);
                        //    da.SelectCommand.Parameters.Add("@" + canshuList[i].InnerXml, datetime);
                        //    ReportParameter rp = new ReportParameter(canshuList[i].InnerXml.ToString(), datetime.ToString());
                        //    this.reportViewer1.LocalReport.SetParameters(rp);
                        //}
                        //else
                        //{
                            da.SelectCommand.Parameters.Add("@" + canshuList[i].InnerXml, list[i]);  //参数传给数据源
                            ReportParameter rp = new ReportParameter(canshuList[i].InnerXml.ToString(), list[i]);
                            this.reportViewer1.LocalReport.SetParameters(rp);  //参数传给报表
                        //}
                    }
                }

            }
            DataSet de = new DataSet();
            da.Fill(de, "Table");
            conn.Close();
            return de;
        }
mamicode.com,码迷

注释部分为动态传参数使用,为了测试方便,不动态长参数。

其中得到数据库连接字符串和报表名字可配置在app.config中,代码如下:

 

mamicode.com,码迷
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="ConnectionSQL"
       connectionString="Data Source=192.168.0.111\SQL2005;Initial Catalog=Dev_LandaHRM;Persist Security Info=True;User ID=sa;Password=landa"
       providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="file1" value="CSEMPL1.rdl"/>
    <add key="file2" value="CSEMPL2.rdl"/>
    <add key="file3" value="Dept.rdl"/>
    <add key="file4" value="SubDept.rdlc"/>
  </appSettings>
</configuration>
mamicode.com,码迷

 

 

ReportView动态加载带参数的RDCL文件,码迷,mamicode.com

ReportView动态加载带参数的RDCL文件

标签:winform   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/wwfjcy479/p/3699805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!