Solved

c# write database to text file

Posted on 2010-11-29
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 30

Expert Comment

ID: 34235809
change to:

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

LVL 30

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month3 days, 10 hours left to enroll