Solved

# c# write database to text file

Posted on 2010-11-29
350 Views

the code below output is
thisrowthisrowthisrowthisrowthisrowthisrow

How can I change it to value from database i think must correct this statement
( System.IO.File.AppendAllText(@"C:\file.txt", "thisrow");)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Web;

namespace WindowsFormsApplication1
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost;" +
"DATABASE=registration;" +
"UID=root;" +
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "select * from teera";
connection.Open();
{
string thisrow = "";
for (int i = 0; i < Reader.FieldCount; i++)
System.IO.File.AppendAllText(@"C:\file.txt", "thisrow");
}
connection.Close();

}
}
}

0
Question by:teera
• 2

LVL 29

Expert Comment

ID: 34235809
change to:

System.IO.File.AppendAllText(@"C:\file.txt", thisrow + Environment.NewLine) // remove quotations
0

LVL 29

Accepted Solution

anarki_jimbel earned 500 total points
ID: 34235905

According to my comment in another question, i'd change the code as below.
Benefits:
- just one use of IO, this is much faster
- also, you may close connection earlier, not waiting for time-consuming IO operations

The first one might be very important if you have hundreds of lines. Also, your UI will respond faster, I believe.
string MyConString = "SERVER=localhost;" +
"DATABASE=registration;" +
"UID=root;" +
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "select * from teera";
connection.Open();
StringBuilder sb = new StringBuilder();
{
string thisrow = "";
for (int i = 0; i < Reader.FieldCount; i++)
//System.IO.File.AppendAllText(@"C:\file.txt", "thisrow");
sb.Append(s + Environment.NewLine);
}
connection.Close();
System.IO.File.WriteAllText(@"C:\path\to\file.txt", sb.ToString());

0

Author Closing Comment

ID: 34235948
Thank you
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.