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


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

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

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

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

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

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

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <connectionStrings>
        <add name="ConnectionSQL"
           connectionString="Data Source=192.168.0.111SQL2005;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>
  • 相关阅读:
    【JavaWeb】DbUtils入门之QueryRunner
    详解 hibernate mapping配置
    hibernate-mapping的各种属性配置
    Hibernate入门这一篇就够了
    记一次Sql优化过程
    ExtJs双ActionResult共用同一Js文件ID冲突解决方案
    ExtJs批量更新
    枚举初使用
    Dapper连接Oracle
    读取Excel任务列表并显示在Outlook日历上
  • 原文地址:https://www.cnblogs.com/wwfjcy479/p/3699805.html
Copyright © 2020-2023  润新知