Solved

c# write database to text file

Posted on 2010-11-29
3
348 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now