Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a MySQL query in C# and I'm trying to write the results of the entire query to a file. However, the code will only write the first query result into the text file. When I tell it to output to the console though it displays all 500 results. I know that my problem may have something to do with newline in the string called result. I've been Googling for a long time on how to fix this- need the help. Thanks.

The output to the text file should look like:
x1,x2,x3,x4
x1,x2,x3,x4
...

Right now I am only getting:
x1,x2,x3,x4

The code is here:
C#
using System;
using System.Data;
using System.IO;
using MySql.Data.MySqlClient;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;

namespace sqljunk1
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			string result="";
			string connectionString =
          			"Server=localhost;" +
          			"Database=mysql;" +
          			"User ID=***;" +
          			"Password=***;" +
          			"Pooling=false";
       			
		IDbConnection dbcon;
       		dbcon = new MySqlConnection(connectionString);
       		dbcon.Open();
       		IDbCommand dbcmd = dbcon.CreateCommand();
      
			string sql =
           		"SELECT x1, x2, x3, x4" +"FROM table " 
			+"WHERE x3 like '%GJ71%'";
       		
		dbcmd.CommandText = sql;
       		IDataReader reader = dbcmd.ExecuteReader();
       	
	while(reader.Read()) 
		{
            string x1 = (string) reader["x1"];
            string x2 = (string) reader["x2"];
	    string x3 = (string) reader["x3"];
            string x4 = (string) reader["x4"];
			
	    result= x1+","+x2+","+x3+","+x4+; 	
 		
				
System.IO.StreamWriter file= new System.IO.StreamWriter("example.txt");
	    file.WriteLine(result);
	    file.Close();
			
		}
			
       reader.Close();
       reader = null;
       dbcmd.Dispose();
       dbcmd = null;
       dbcon.Close();
       dbcon = null;

		}
	}
}
Posted
Updated 29-Nov-10 12:38pm
v2
Comments
fjdiewornncalwe 29-Nov-10 18:38pm    
Just added the pre tags to make reading the code easier

1 solution

It looks like you are recreating the file within the loop, so your line:

System.IO.StreamWriter file= new System.IO.StreamWriter("example.txt");
file.WriteLine(result);


Is overwriting the file every time.

According to StreamWriter documentation:
If the file exists, it is overwritten; otherwise, a new file is created.

This may not be optimal, but to fix it try this code:

C#
System.IO.StreamWriter file= new System.IO.StreamWriter("example.txt");
while(reader.Read())
{
string x1 = (string) reader["x1"];
string x2 = (string) reader["x2"];
string x3 = (string) reader["x3"];
string x4 = (string) reader["x4"];
result= x1+","+x2+","+x3+","+x4+;


file.WriteLine(result);
}
file.Close();
 
Share this answer
 
v2
Comments
theknight46 29-Nov-10 16:52pm    
That worked! Thank you so much!
wizardzz 29-Nov-10 16:58pm    
You're welcome theknight46.
Dalek Dave 29-Nov-10 18:45pm    
Good Call.

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