Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
hi friends.

i want to save my query result to a text file programatically in sql server and c#.

how can i do that?

thank you
Posted
Comments
SRK90 8-Aug-14 4:24am    
Please try this and let me know.

{
SqlCommand sc = new SqlCommand("SELECT * FROM Table", oConn);
SqlDataAdapter da = new SqlDataAdapter(sc);
DataSet ds = new DataSet();
da.Fill(ds);

ds.WriteXml("MyFile.txt");
}
_Starbug_ 8-Aug-14 5:04am    
thanks friend.it works fine.
SRK90 8-Aug-14 5:06am    
Then please accept my solution. Click the Green Accept button. It will be useful for others too. Thank you :)
_Starbug_ 8-Aug-14 5:26am    
if i want to add other sql query result to this text file and second query result come after first query.how can i do that.
SRK90 8-Aug-14 5:48am    
Add the beow code and Please try



using(StreamWriter file = File.AppendText(@"c:\yourTextFile.txt"))
{
file.WriteLine(yourAppendString);
}


Also i have added my solution below in "Add Your Soltion Here TAB"... Please click the green color Accept button if this was useful to you. Thanks :)

 
Share this answer
 
try this one...


[^]
 
Share this answer
 
Please try this and let me know.

C#
{
SqlCommand sc = new SqlCommand("SELECT * FROM Table", oConn);
SqlDataAdapter da = new SqlDataAdapter(sc);
DataSet ds = new DataSet();
da.Fill(ds);

ds.WriteXml("MyFile.txt");
}

To Append the text file,

C#
using(StreamWriter file = File.AppendText(@"c:\MyFile.txt"))
 {
 file.WriteLine(yourAppendString);
 }
 
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