Click here to Skip to main content
15,887,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Good morning everyone,

I have one sql view and its have 5 field. I need to write that view data into ASCII file with deliminater (|). please anyone help me and provide me good and easy source.

I'm using Visual Studio 2008 with C# and Sql server 2008.

Thanks
Posted
Updated 5-Feb-11 3:55am
v2
Comments
Sandeep Mewara 5-Feb-11 2:48am    
What have you tried from your side? Tried to write yourself? Search?
OriginalGriff 5-Feb-11 9:14am    
Answer modified: in my defence it was early this morning when I threw that together...

1 solution

I'm not sure what part of this is giving you problems, but here you go:

C#
private void createFlatFile(SqlDataAdaptor da)
    {
    DataTable dt = new DataTable();
    da.Fill(dt, "myTableName");
    DataView dv = dt.DefaultView;
    List<string> lines = new List<string>();
    foreach (DataRowView row in dv)
        {
        StringBuilder sb = new StringBuilder();
        string sep = "";
        for (int i = 0; i < dv.Table.Columns.Count; i++)
            {
            sb.Append(sep + row[i].ToString());
            sep = "|";
            }
        lines.Add(sb.ToString());
        }
    File.WriteAllLines(@"File:\Temp\myFlatFileWithSeperators.txt", lines.ToArray());
    }

[edit] :-O I put the separator after the data instead of before. :-O - OriginalGriff[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900