Solved

c# write database to text file

Posted on 2010-11-29
3
349 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 54
Run time Error 4 35
Device Location 4 19
Finally almost got it 5 23
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

862 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

23 Experts available now in Live!

Get 1:1 Help Now