ขั้นตอนคือสร้าง form เปล่าแล้วโค้ดตามนี้เลย
1 using System;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.IO;
5 using System.Windows.Forms;
6 using System.Xml;
7 using System.Xml.Serialization;
8 using Accsa.Data;
9 using Accsa.Data.DAL;
10 using Microsoft.Reporting.WinForms;
11 12 namespace WindowsFormsApplication1
13 {14 public partial class Form1 : Form
15 {16 public Form1()
17 { 18 InitializeComponent(); 19 } 20 21 private Microsoft.Reporting.WinForms.ReportViewer reportViewer1;
22 23 private void Form1_Load(object sender, EventArgs e)
24 {25 XmlReader xrd = XmlReader.Create(Path.Combine(Application.StartupPath, "Report1.rdlc"));
26 xrd.Read();
27 XmlSerializer serialize = new XmlSerializer(typeof(Rdlc.Report));
28 Rdlc.Report rpt = (Rdlc.Report)serialize.Deserialize(xrd);
29 30 // create list of item element name.
31 32 List<Rdlc.ItemsChoiceType37> choice37 = new List<Rdlc.ItemsChoiceType37>(rpt.ItemsElementName);
33 int DataSetsIndex = choice37.IndexOf(Rdlc.ItemsChoiceType37.DataSets);
34 35 Rdlc.DataSetsType dstype = rpt.Items[DataSetsIndex] as Rdlc.DataSetsType;
36 foreach (var item in dstype.DataSet)
37 {38 Rdlc.DataSetType dtype = item as Rdlc.DataSetType;
39 Rdlc.QueryType qtype = Array.Find<object>(dtype.Items, (oo) =>
40 {41 return oo.GetType() == typeof(Rdlc.QueryType);
42 }) as Rdlc.QueryType;
43 44 List<Rdlc.ItemsChoiceType2> choice2 = new List<Rdlc.ItemsChoiceType2>(qtype.ItemsElementName);
45 int indexCommandText = choice2.IndexOf(Rdlc.ItemsChoiceType2.CommandText);
46 int indexCommandType = choice2.IndexOf(Rdlc.ItemsChoiceType2.CommandType);
47 int indexDatasourceName = choice2.IndexOf(Rdlc.ItemsChoiceType2.DataSourceName);
48 int indexQueryParas = choice2.IndexOf(Rdlc.ItemsChoiceType2.QueryParameters);
49 50 dictQuery.Add(item.Name, qtype.Items[indexCommandText].ToString());
51 //string commandType = qtype.Items[indexCommandType].ToString();
52 string datasourceName = qtype.Items[indexDatasourceName].ToString();
53 //Rdlc.QueryParametersType qParas = qtype.Items[indexQueryParas] as Rdlc.QueryParametersType;
54 } 55 56 57 this.timer1.Start();
58 } 59 60 /// <summary>
61 /// CommandText DataSet_DataTable, CommandText
62 /// </summary>
63 Dictionary<string, string> dictQuery = new Dictionary<string, string>();
64 65 private void timer1_Tick(object sender, EventArgs e)
66 {67 this.timer1.Stop();
68 69 DataSet ds = new DataSet("DSReport");
70 71 using (AsAdapter da= new AsAdapter())
72 {73 da.ConnectionInfo = new ConnectionInfo();
74 da.ConnectionInfo.SetConnectionString("localhost", "db2006", "sa", "system");
75 da.Open();
76 foreach (var item in dictQuery)
77 {78 DataTable dt = da.GetData(item.Value, item.Key.Split('_')[1]);
79 ds.Tables.Add(dt);
80 }81 da.Close();
82 }83 this.reportViewer1 = new ReportViewer();
84 this.reportViewer1.ProcessingMode = ProcessingMode.Local;
85 this.reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
86 87 for (int i = 0; i < ds.Tables.Count; i++)
88 {89 string datasource_Name = string.Format("{0}_{1}", ds.DataSetName, ds.Tables[i].TableName);
90 ReportDataSource rds = new ReportDataSource(datasource_Name, ds.Tables[i]);
91 this.reportViewer1.LocalReport.DataSources.Add(rds);
92 } 93 94 95 this.reportViewer1.Dock = DockStyle.Fill;
96 this.Controls.Add(this.reportViewer1);
97 98 this.reportViewer1.RefreshReport();
99 } 100 101 } 102 }
ไม่มีความคิดเห็น:
แสดงความคิดเห็น