|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Configuration;
using System.Diagnostics;
using ChecksumFiles.Utilities;
namespace ChecksumFiles.Utilities
{
class FileGenerator
{
public static string m_results = Settings.Default.ResultFile.ToString();
public static string m_errors = Settings.Default.ErrorFile.ToString();
public static string CreateFile(DataTable dt, string strFilePath)
{
try
{
// StreamWriter and StringBuilder are used because the email body and
// the file contents have slightly different data. This method allows for
// fewer database calls and a single loop
StreamWriter sw = new StreamWriter(strFilePath, false);
StringBuilder sb = new StringBuilder();
// First we will write the headers.
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
sb.AppendLine();
// Now write all the rows.
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
sb.Append(dr["FILEPATH"].ToString());
sb.AppendLine();
}
sw.Close();
return sb.ToString();
}
catch (Exception ex)
{
using (StreamWriter swerr = File.AppendText(m_errors))
{
Logger.LogMessage(ex.Message.ToString(), swerr);
swerr.Close();
}
return "Unable to create file or email content.";
}
}
public static string CreateFile(DataTable dt)
{
try
{
// This method creates email content only.
StringBuilder sb = new StringBuilder();
sb.AppendLine();
// Now write all the rows.
foreach (DataRow dr in dt.Rows)
{
sb.Append(dr["FILEPATH"].ToString());
sb.AppendLine();
}
return sb.ToString();
}
catch (Exception ex)
{
using (StreamWriter swerr = File.AppendText(m_errors))
{
Logger.LogMessage(ex.Message.ToString(), swerr);
swerr.Close();
}
return "Unable to create email content.";
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I am an MBA with a bunch of MS certifications. Technically, I am a DBA, but I do a good deal of sys admin work and web development using .NET. I like to focus on business intelligence, database design, messaging architectures, and web services.