I'm reading data from a table for the RTFDocument method/class and its having an issue with large tables of more 660,000 records. With this process does it keep all the data in memory and then write/create the document when its done reading everything.
The process works fine when writing to a text file as it writes lines as it goes along. Table I'm reading from is 807,000 records and when I look at the text file that is being written along with it it stops at record 604,087.
Is there a way to write out to the rtf file at the same time so that there is less memory being used or something like that?
static void MostFrequentlyCalledNumber()
{
iDB2Connection cn = new iDB2Connection();
iDB2Connection cn1 = new iDB2Connection();
cn.ConnectionString = "DataSource = Birch01; UserId=DPCBP; Password = DPCBP;";
cn1.ConnectionString = "DataSource = XXXXX; UserId=XXXX; Password = XXXX;";
cn.Open();
cn1.Open();
iDB2Command cmd = cn.CreateCommand();
iDB2Command cmd1 = cn1.CreateCommand();
StreamWriter sw;
RtfDocument doc;
RtfTextFormat format = new RtfTextFormat();
cmd.CommandText = "SELECT * FROM QGPL.FPBSUMCUST"; cmd.CommandTimeout = 0; try { iDB2DataReader dr = cmd.ExecuteReader(); while (dr.Read()) { int k = dr.GetiDB2Integer(0); Console.WriteLine(k); Directory.CreateDirectory("C:\\fpb"); Directory.CreateDirectory("c:\\fpb\\fp" + k); Console.WriteLine("c:\\fpb\\fp" + k); //rtf file output Console.WriteLine("c:\\fpb\\fp" + k + "\\RPT_MOST_FREQUENTLY_CALLED.doc"); Console.WriteLine(File.Exists("C:\\fpb\\fp" + k + "\\RPT_MOST_FREQUENTLY_CALLED.doc")); format.font = "Courier"; format.size = 18; format.bold = true; format.underline = true; doc.AddText("Most Frequent Called Number", format); doc.AddNewLine(); format.size = 12; doc.AddText("", format); doc.AddNewLine(); format.size = 9; format.bold = false; format.underline = false; doc.AddText("Customer ", format); doc.AddText(dr.GetiDB2Char(0), format); doc.AddText(" - ", format); doc.AddText(dr.GetString(1), format); doc.AddNewLine(); format.size = 8; format.bold = true; format.underline = true; doc.AddText("Phone", format); format.underline = false; doc.AddText(" ", format); format.underline = true; doc.AddText("City", format); format.underline = false; doc.AddText(" ", format); format.underline = true; doc.AddText("State", format); format.underline = false; doc.AddText(" ", format); format.underline = true; doc.AddText("Calls", format); format.underline = false; doc.AddText(" ", format); format.underline = true; doc.AddText("Total Duration", format); format.underline = false; doc.AddText(" ", format); format.underline = true; doc.AddText("Total Charge", format); format.underline = false; doc.AddNewLine(); format.underline = false; format.bold = false; doc.AddText("", format);/**/ //} sw = File.CreateText("c:\\fpb\\fp" + k + "\\fp" + k + "m.txt"); sw.WriteLine(" Most Frequently Called Number "); sw.WriteLine(""); sw.WriteLine(dr.GetString(1)); sw.WriteLine("PHONE " + " " + "CITY " + "State" + " " + "Calls" + " " + "Duration " + "Total Cost");/**/ cmd1.CommandText = "SELECT * FROM QGPL.FP" + k.ToString() + "N ORDER BY PHONE"; cmd1.CommandTimeout = 0; try { iDB2DataReader dr1 = cmd1.ExecuteReader(); while (dr1.Read()) { //Console.WriteLine(dr1.GetString(0)); ad = dr1.GetiDB2Decimal(4); tc = dr1.GetiDB2Decimal(5); sw.WriteLine(dr1.GetString(0) + " " + dr1.GetString(1) + " " + dr1.GetString(2) + new string(' ', (7 - dr1.GetiDB2Integer(3).ToString().Length)) + dr1.GetiDB2Integer(3) + " " + new string(' ', (10 - ad.ToString().Length)) + String.Format("{0:0.00}", ad) + " " + String.Format("{0:C}", tc));/**/ fullLine = dr1.GetString(0); doc.AddText(fullLine, format); //doc.AddText(" "); fullLine = dr1.GetString(1); doc.AddText(fullLine, format); doc.AddText(" "); fullLine = dr1.GetString(2); doc.AddText(fullLine, format); doc.AddText(" "); fullLine = String.Format("{0,-4:0,0}",dr1.GetiDB2Integer(3)); doc.AddText(fullLine, format); doc.AddText(" "); fullLine = String.Format("{0,-4:0,0.0}", ad); doc.AddText(fullLine, format); doc.AddText(" "); fullLine = String.Format("{0,-10:C}", tc); doc.AddText(fullLine, format); doc.AddNewLine();/**/ doc.Close(); calls = calls + dr1.GetiDB2Integer(3); avgDuration = avgDuration + ad; totalCost = totalCost + tc; j++; } dr1.Close(); } catch (Exception exc) { Console.WriteLine("inner exception for db2: " + exc.Message); //dr1.Close(); //throw; } sw.WriteLine(""); sw.WriteLine("Totals " + calls + " " + String.Format("{0:0.00}", avgDuration) + " " + String.Format("{0:C}", totalCost)); sw.Close();/**/ format.bold = true; doc.AddText("Totals", format); format.bold = false; doc.AddText(" "); doc.AddText(calls.ToString(), format); doc.AddNewLine(); doc.Close();/**/ calls = 0; avgDuration = 0; totalCost = 0; j = 1; } calls = 0; avgDuration = 0; totalCost = 0; dr.Close(); } catch (Exception exc1) { Console.WriteLine("outer exception for db2: " + exc1.Message); //dr.Close(); //throw; } cn.Close(); cn1.Close(); }