I have a table that has a field called ACTION_TYPE. The potential values for it are "ADD ", "Change", "DELETE", and "-". I want to export the rows which have an ACTION_TYPE value that isn't "-". I'm successfully exporting the table row in the correct format to the file, however the headers aren't working/ being written.
Basically what I want is to separate the entries by the type in the file. In example
ADD
00 00 56 55
00 00 59 42
00 00 36 45
CHANGE
00 00 96 25
DELETE
00 00 76 75
00 00 38 95
So far I've been unsuccessful in even getting it to work with ADD. Also, when I do get it working with one of the three types, is there a better way than just cloning the process for each type (ADD, CHANGE, DELETE)?
using (StreamWriter writer =
new StreamWriter("C:\\BillingExport\\EXPORTS\\EFTHLDAY.TABLE.TYPE08.TRANSACTIONS.txt"))
{
writer.Write("ADD");
}
string selectCommandText42 = "SELECT * FROM HOLIDAY_DATE_TABLE WHERE ACTION_TYPE = 'ADD '";
using (SqlDataAdapter adapter42 = new SqlDataAdapter(selectCommandText42, connection))
{
using (DataTable table42 = new DataTable("HOLIDAY_DATE_TABLE"))
{
adapter42.Fill(table42);
System.Text.StringBuilder commaDelimitedText = new System.Text.StringBuilder();
//commaDelimitedText.AppendLine("col1,col2,col3"); // optional if you want column names in first row
foreach (DataRow row in table42.Rows)
{
string value = string.Format("{0} {0} {1} {2}", row[1], row[2], row[3]); // how you format is up to you (spaces, tabs, delimiter, etc)
commaDelimitedText.AppendLine(value);
}
System.IO.File.WriteAllText("C:\\BillingExport\\EXPORTS\\EFTHLDAY.TABLE.TYPE08.TRANSACTIONS.txt", commaDelimitedText.ToString());
}
}
Update: Implementing some of the suggestions below, I now have the following code
string selectCommandText42 = "SELECT * FROM HOLIDAY_DATE_TABLE WHERE ACTION_TYPE != '-'";
using (SqlDataAdapter adapter42 = new SqlDataAdapter(selectCommandText42, connection))
{
using (DataTable table42 = new DataTable("HOLIDAY_DATE_TABLE"))
{
adapter42.Fill(table42);
System.Text.StringBuilder commaDelimitedText = new System.Text.StringBuilder();
//commaDelimitedText.AppendLine("col1,col2,col3"); // optional if you want column names in first row
foreach (DataRow row in table42.Rows)
{
string rowtype = row[4].ToString();
using (StreamWriter writer =
new StreamWriter("C:\\BillingExport\\EXPORTS\\EFTHLDAY.TABLE.TYPE08.TRANSACTIONS.txt")){
writer.WriteLine(rowtype);
}
string value = string.Format("{0} {0} {1} {2}", row[1], row[2], row[3]); // how you format is up to you (spaces, tabs, delimiter, etc)
commaDelimitedText.AppendLine(value);
}
System.IO.File.AppendAllText("C:\\BillingExport\\EXPORTS\\EFTHLDAY.TABLE.TYPE08.TRANSACTIONS.txt", commaDelimitedText.ToString());
}
}
This is providing the following output
ADD
00 00 96 25
00 00 76 75
00 00 38 95
00 00 56 55
00 00 59 42
00 00 36 45
It seems to not be applying the CHANGE or DELETE headers, and is also not listing entries in the appropriate order (the ADD entries are at the bottom of the created file now).