Form
This example shows SQL table output from application to the report result file. There is one button to open the database, and another to create the report.
Report template
The report template contains Scan-EndScan block and field names, enclosed within the \\ chars. The report prints columns from table Customers. Q is the alias for data reader, which is attached to the report.
Code to open the database
private void ConnectBtn_Click(object sender, EventArgs e) { // create a connection object conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =|DataDirectory|\\Nwind.mdb"); // create a command object cmd = new OleDbCommand("select * from Customers", conn); conn.Open(); ReportBtn.Enabled = true; ConnectBtn.Text = "Connection OK"; conn.Close(); }
Code to generate the report
private void ReportBtn_Click(object sender, EventArgs e) { conn.Open(); System.Data.OleDb.OleDbDataReader rdr; rdr = cmd.ExecuteReader(); rdr.Read(); //Place EkRtf component on a form, or create it in code like here System.ComponentModel.Container components = new System.ComponentModel.Container(); EkRtfReport.EkRtf ekRtf1 = new EkRtfReport.EkRtf(components); ekRtf1.ClearVars(); //Clear report variables (if any exists) ekRtf1.ReportData.Clear(); //Clear report datasources ekRtf1.ReportData.Add(rdr,"Q"); //Attach reader to the report with alias Q ekRtf1.InFile = "sql_report.rtf"; //Set the file for input report template ekRtf1.OutFile = "sql_result.rtf"; //Set the file for output result (if necessary) //ekRtf1.UnicodeByDefault = true; //set for unicode rtf output (database also must be in unicode, to take effect) //Execute report ekRtf1.ExecuteOpen(); rdr.Close(); conn.Close(); }