JG Vimalan's Blog

It is all about coding!

Convert tables in a database into a CSV file using C#.NET

The following code converts a table to to a CSV file (comma seperated value)

The database used is ‘Interbase’.  

public partial class DbToCsv : Form
    {           
        BdpConnection connection;     

        public DbToCsv()
        {
                   //connect to database.
                    connection.Open();
            }
        }

        private void _SaveButon_Click(object sender, EventArgs e)
        {
            if (_PathTextBox.Text == “”)
            {
                MessageBox.Show(“Path cannot be empty.”);
                return;
            }

            BdpDataAdapter adapter = new BdpDataAdapter();
            BdpCommand command = new BdpCommand(“SELECT * FROM

            table”, connection);           
            adapter.SelectCommand = command;

            DataSet dataset = new DataSet();
            adapter.Fill(dataset, “table”);           

            SaveDataTableToCsvFile(_PathTextBox.Text, dataset.Tables[0]);
            MessageBox.Show(“Successfully created”);           
        } 

        public void SaveDataTableToCsvFile(string path, DataTable dt)
        {
            string separator = “,”;
            string quote = “\””;

            //Creates CSV file.
            StreamWriter sw = new StreamWriter(path);
     
            //Writes Header Line.
            int iColCount = dt.Columns.Count;
            for (int i = 0; i < iColCount; i++)
            {
                sw.Write(dt.Columns[i]);
                if (i < iColCount – 1)
                {
                    sw.Write(separator);
                }
            }
            sw.Write(sw.NewLine);
           
            //Writes Rows.
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    if (Convert.IsDBNull(dr[i]))
                        sw.Write(quote + “NULL” + quote); //Provide quotes for Null value.
                    else                       
                    {
                        string data = dr[i].ToString();                       
                        sw.Write(quote + data + quote); //Provide quotes for

                        data.                    
                    }
                    if (i < iColCount – 1)
                    {
                        sw.Write(separator);
                    }
                }
                sw.Write(sw.NewLine);
            }
            sw.Close();
        }
    }

Advertisements

October 9, 2009 - Posted by | C#.NET

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: