|
Subject:
|
How to include a new line after every row --Help
|
|
Posted By:
|
srkarthik_82
|
Post Date:
|
1/4/2007 8:07:16 PM
|
This is the code to write a text file form the database.It writtes the data correctly.But,i need a new line after every row.so where shud i include \n in this code.....I am new to the coding....somebody help me.....
public void TextWritter() { SqlConnection con = null; try { con = new SqlConnection("server=localhost; uid='sa';pwd='buildfolio1819'; database=pubs;"); con.Open(); SqlDataAdapter da = new SqlDataAdapter("Select * from authors", con); DataSet ds=new DataSet(); da.Fill(ds); StringBuilder str = new StringBuilder(); for(int i=0;i<=ds.Tables[0].Rows.Count - 1; i++) for(int j=0;j<=ds.Tables[0].Columns.Count - 1; j++) // if(j==0) str.Append(ds.Tables[0].Rows[i][j].ToString() + "\t"); //Response.Write(str.ToString()); StreamWriter writer = new StreamWriter("c:\\qazwsx.txt"); writer.WriteLine(str.ToString()); writer.Close(); } catch(Exception ex) { ex = ex; } finally { if(null!=con) if(con.State == System.Data.ConnectionState.Open) con.Close(); } }
|
|
Reply By:
|
planoie
|
Reply Date:
|
1/5/2007 9:43:15 AM
|
One thing I'd recommend right up front is that you always include { and } around branching logic. It makes it far easier to read and troubleshoot. Also, you can use the StringBuilder.AppendFormat method for cleaner (and more efficient) string concatenation (particularly when using the string builder.
Try this:
for(int i=0;i<=ds.Tables[0].Rows.Count - 1; i++) { for(int j=0;j<=ds.Tables[0].Columns.Count - 1; j++) { str.AppendFormat("{0}\t", ds.Tables[0].Rows[i][j]); } str.Append("\n"); }
-Peter
|
|
Reply By:
|
srkarthik_82
|
Reply Date:
|
1/7/2007 8:05:09 PM
|
Thanks a lot peter ..........u r idea worked out....
Karthik [Nothing is impossible]
|