Solved

c# write database to text file

Posted on 2010-11-29
3
350 Views
Last Modified: 2012-05-10

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;" +
                "PASSWORD='';";
            MySqlConnection connection = new MySqlConnection(MyConString);
            MySqlCommand command = connection.CreateCommand();
            MySqlDataReader Reader;
            command.CommandText = "select * from teera";
            connection.Open();
            Reader = command.ExecuteReader();
            while (Reader.Read())
            {
                string thisrow = "";
                for (int i = 0; i < Reader.FieldCount; i++)
                    thisrow += Reader.GetValue(i).ToString() + ",";
                listBox1.Items.Add(thisrow);
                System.IO.File.AppendAllText(@"C:\file.txt", "thisrow");
            }
            connection.Close();

        }
    }
}

Open in new window

0
Comment
Question by:teera
  • 2
3 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 34235809
change to:

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

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 34235905
Just to add :)

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;" +
                "PASSWORD='';";
            MySqlConnection connection = new MySqlConnection(MyConString);
            MySqlCommand command = connection.CreateCommand();
            MySqlDataReader Reader;
            command.CommandText = "select * from teera";
            connection.Open();
            Reader = command.ExecuteReader();
            StringBuilder sb = new StringBuilder();
            while (Reader.Read())
            {
                string thisrow = "";
                for (int i = 0; i < Reader.FieldCount; i++)
                    thisrow += Reader.GetValue(i).ToString() + ",";
                listBox1.Items.Add(thisrow);
                //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());

Open in new window

0
 

Author Closing Comment

by:teera
ID: 34235948
Thank you
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Article by: Ivo
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.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question