仅C#/MySQL,将一个查询输出到文件 [英] C#/MySQL-only outputs one query to a file

查看:104
本文介绍了仅C#/MySQL,将一个查询输出到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在C#中有一个MySQL查询,并且试图将整个查询的结果写入文件中.但是,该代码只会将第一个查询结果写入文本文件.当我告诉它输出到控制台时,尽管它显示所有500个结果.我知道我的问题可能与名为result的字符串中的换行符有关.我已经在Google上搜索了很长时间了,该如何解决-需要帮助.谢谢.

文本文件的输出应如下所示:
x1,x2,x3,x4
x1,x2,x3,x4
...

现在我只得到:
x1,x2,x3,x4

代码在这里:

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:

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;

		}
	}
}

推荐答案

似乎您正在循环中重新创建文件,因此您的代码行是:

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);



每次都覆盖文件.

根据StreamWriter文档:
如果该文件存在,则将其覆盖;否则,它将被覆盖.否则,将创建一个新文件.

这可能不是最佳选择,但要解决此问题,请尝试以下代码:



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:

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();


这篇关于仅C#/MySQL,将一个查询输出到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆