Click here to Skip to main content
15,743,541 members
Articles / Desktop Programming / Windows Forms
Article
Posted 4 Nov 2008

Stats

531.5K views
31.4K downloads
204 bookmarked

C# - CSV Import Export

Rate me:
Please Sign up or sign in to vote.
4.83/5 (62 votes)
4 Nov 2008CPOL2 min read
This article introduces import and export functions between a flat CSV file and a SQL server.
CSV_import_export1.png

Introduction

This article introduces and implements import and export functions between a flat CSV file and a SQL server. This project implemented in Visual Studio C# 2005, and uses the .NET Framework 2.0.

The base of this project was a former Code Project article: Importing CSV Data and saving it in database.

This project contains new functionalities, for example: export functions, creating table in the database and using bulk copy, which is new in .NET 2.0. (The former project uses .NET 1.1.)

This example gives commented code samples to the following processes:

Import:

  • Connect to a CSV file through an ODBC driver, and read it as a database table (based on referenced article)
  • Use different encodings and separators (based on referenced article)
  • Load a CSV file into a DataSet (based on referenced article)
  • How to show a preview of a CSV file (based on referenced article)
  • Transfer data with a SqlBulkCopy instance to a SQL server
    Source: a DataSet instance
  • Transfer data with a SqlBulkCopy instance to a SQL server
    Source: an ODBC connection
  • Create a new table in a SQL database based on the CSV file using schema tables
  • Write the progress of the bulk copy operation using an event

Export:

  • Browse user tables in a SQL database
  • Use different encodings and separators
  • Read data from SQL server using a SqlDataReader, and transfer data to a flat file using a StreamWriter

CSV_import_export2.png

Using

  1. Download the project.

  2. Make a new database on a SQL server, or use an existing database.

  3. Modify the connection string in the application. You can find it in prop.cs:

    C#
    public static string sqlConnString = "server=(local);
    	database=Test_CSV_impex;Trusted_Connection=True";
  4. Run the project.

Some Piece of Code

Loading data from CSV into a DataSet:

C#
/*
 * Loads the csv file into a DataSet.
 * 
 * If the numberOfRows parameter is -1, it loads all rows, otherwise it
 * loads the first specified number of rows (for preview)
 */

public DataSet LoadCSV(int numberOfRows)
{
	DataSet ds = new DataSet();
	try
	{
		// Creates and opens an ODBC connection
		string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};
			Dbq=" + this.dirCSV.Trim() + ";
			Extensions=asc,csv,tab,txt;Persist Security Info=False";
		string sql_select;
		OdbcConnection conn;
		conn = new OdbcConnection(strConnString.Trim());
		conn.Open();

		//Creates the select command text
		if (numberOfRows == -1)
		{
			sql_select = "select * from [" + 
					this.FileNevCSV.Trim() + "]";
		}
		else
		{
			sql_select = "select top " + numberOfRows + 
				" * from [" + this.FileNevCSV.Trim() + "]";
		}

		//Creates the data adapter
		OdbcDataAdapter obj_oledb_da = new OdbcDataAdapter(sql_select, conn);
				
		//Fills dataset with the records from CSV file
		obj_oledb_da.Fill(ds, "csv");
				
		//closes the connection
		conn.Close();
	}
	catch (Exception e) //Error
	{
		MessageBox.Show(e.Message, "Error - LoadCSV",
				MessageBoxButtons.OK,MessageBoxIcon.Error);
	}
	return ds;
}

Transfer data from an ODBC connection into the SQL server with SqlBulkCopy:

C#
/*
 * Imports data to the database with SqlBulkCopy.
 * This method doesn't use a temporary dataset, it loads
 * data immediately from the ODBC connection
 */

private void SaveToDatabaseDirectly()
{
	try
	{
		if (fileCheck())
		{
			// select format, encoding, and write the schema file
			Format();
			Encoding();
			writeSchema();

			// Creates and opens an ODBC connection
			string strConnString = "Driver={Microsoft Text Driver 
				(*.txt; *.csv)};Dbq=" + this.dirCSV.Trim() + ";
				Extensions=asc,csv,tab,txt;
				Persist Security Info=False";
			string sql_select;
			OdbcConnection conn;
			conn = new OdbcConnection(strConnString.Trim());
			conn.Open();

			//Counts the row number in csv file - with an SQL query
			OdbcCommand commandRowCount = new OdbcCommand
				("SELECT COUNT(*) FROM [" + 
				this.FileNevCSV.Trim() + "]", conn);
			this.rowCount = System.Convert.ToInt32
					(commandRowCount.ExecuteScalar());

			// Creates the ODBC command
			sql_select = "select * from [" + 
					this.FileNevCSV.Trim() + "]";
			OdbcCommand commandSourceData = 
				new OdbcCommand(sql_select, conn);

			// Makes on OdbcDataReader for reading data from CSV
			OdbcDataReader dataReader = 
				commandSourceData.ExecuteReader();

			// Creates schema table. 
                           // It gives column names for create table command.
			DataTable dt;
			dt = dataReader.GetSchemaTable();

			// You can view that schema table if you want:
			//this.dataGridView_preView.DataSource = dt;

			// Creates a new and empty table in the sql database
			CreateTableInDatabase(dt, this.txtOwner.Text, 
				this.txtTableName.Text, prop.sqlConnString);

			// Copies all rows to the database from the data reader.
			using (SqlBulkCopy bc = new SqlBulkCopy
				("server=(local);database=Test_CSV_impex;
				Trusted_Connection=True"))
			{
				// Destination table with owner - 
				// this example doesn't
				// check the owner and table names!
				bc.DestinationTableName = "[" + 
					this.txtOwner.Text + "].[" + 
					this.txtTableName.Text + "]";

				// User notification with the SqlRowsCopied event
				bc.NotifyAfter = 100;
				bc.SqlRowsCopied += 
				   new SqlRowsCopiedEventHandler(OnSqlRowsCopied);

				// Starts the bulk copy.
				bc.WriteToServer(dataReader);

				// Closes the SqlBulkCopy instance
				bc.Close();
			}

			// Writes the number of imported rows to the form
			this.lblProgress.Text = "Imported: " + 
				this.rowCount.ToString() + "/" + 
				this.rowCount.ToString() + " row(s)";
			this.lblProgress.Refresh();
					
			// Notifies user
			MessageBox.Show("ready");
		}
	}
	catch (Exception e)
	{
		MessageBox.Show(e.Message, "Error - SaveToDatabaseDirectly", 
				MessageBoxButtons.OK, MessageBoxIcon.Error);
	}
}

Creating table in the database based on a schema table:

C#
/*
 * Generates the create table command using the schema table, and
 * runs it in the SQL database.
 */

private bool CreateTableInDatabase(DataTable dtSchemaTable, string tableOwner, 
		string tableName, string connectionString)
{
	try
	{
		// Generates the create table command.
		// The first column of schema table contains the column names.
		// The data type is nvarcher(4000) in all columns.

		string ctStr = "CREATE TABLE [" + tableOwner + "].[" + 
						tableName + "](\r\n";
		for (int i = 0; i < dtSchemaTable.Rows.Count; i++)
		{
			ctStr += "  [" + dtSchemaTable.Rows[i][0].ToString() + "]
					[nvarchar](4000) NULL";
			if (i < dtSchemaTable.Rows.Count)
			{
				ctStr += ",";
			}
			ctStr += "\r\n";
		}
		ctStr += ")";

		// You can check the SQL statement if you want:
		//MessageBox.Show(ctStr);

		// Runs the SQL command to make the destination table.		
		SqlConnection conn = new SqlConnection(connectionString);
		SqlCommand command = conn.CreateCommand();
		command.CommandText = ctStr;
		conn.Open();
		command.ExecuteNonQuery();
		conn.Close();

		return true;
	}
	catch (Exception e)
	{
		MessageBox.Show(e.Message, "CreateTableInDatabase", 
			MessageBoxButtons.OK, MessageBoxIcon.Error);
		return false;
	}
}

Getting user table names from the database:

C#
/*
 * Loads list of user tables from the SQL database, and fills
 * a ListBox control with tatble names.
 */

private void loadTables()
{
         // Connects to database, and selects the table names.
         SqlConnection cn = new SqlConnection(prop.sqlConnString);
	SqlDataAdapter da = new SqlDataAdapter
			("select name from dbo.sysobjects where xtype = 'U' 
		and name <> 'dtproperties' order by name", cn);
	DataTable dt = new DataTable();
			
         // Fills the list to an DataTable.
	da.Fill(dt);

         // Clears the ListBox
	this.lbxTables.Items.Clear();

         // Fills the table names to the ListBox.
         // Notifies user if there is no user table in the database yet.
	if (dt.Rows.Count == 0)
	{
	MessageBox.Show("There is no user table in the specified database. 
		Import a CSV file first.", "Warning", 
		MessageBoxButtons.OK, MessageBoxIcon.Warning);
	this.lbxTables.Items.Add("< no user table in database >");
	this.btnExportToCSV.Enabled = false;
	}
	else
	{
	this.btnExportToCSV.Enabled = true;

	for (int i = 0; i < dt.Rows.Count; i++)
	{
	    this.lbxTables.Items.Add(dt.Rows[i][0].ToString());
	}
	this.lbxTables.SelectedIndex = 0;
	}
}

Writing data with a StreamWriter:

C#
/*
 * Exports data to the CSV file.
 */

private void exportToCSVfile(string fileOut)
{
    // Connects to the database, and makes the select command.
    SqlConnection conn = new SqlConnection(prop.sqlConnString);
    string sqlQuery = "select * from " + this.lbxTables.SelectedItem.ToString();
    SqlCommand command = new SqlCommand(sqlQuery, conn);
    conn.Open();
			
    // Creates a SqlDataReader instance to read data from the table.
    SqlDataReader dr = command.ExecuteReader();

    // Retrieves the schema of the table.
    DataTable dtSchema = dr.GetSchemaTable();

    // Creates the CSV file as a stream, using the given encoding.
    StreamWriter sw = new StreamWriter(fileOut, false, this.encodingCSV);
			
    string strRow; // represents a full row

    // Writes the column headers if the user previously asked that.
    if (this.chkFirstRowColumnNames.Checked)
    {
	sw.WriteLine(columnNames(dtSchema, this.separator));
    }

    // Reads the rows one by one from the SqlDataReader
    // transfers them to a string with the given separator character and
    // writes it to the file.
    while (dr.Read())
    {
	strRow = "";
	for (int i = 0; i < dr.FieldCount; i++)
	{
	    strRow += dr.GetString(i);
	    if (i < dr.FieldCount - 1)
	    {
		strRow += this.separator;
	    }
	}
	sw.WriteLine(strRow);
    }

    // Closes the text stream and the database connection.
    sw.Close();
    conn.Close();

    // Notifies the user.
    MessageBox.Show("ready");
}

Reference

History

  • 5th November, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Hungarian Post Co. Ltd
Hungary Hungary
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionModifying ORACLE Server to MSSQL Server (MSSQLLocalDB) Pin
Member 1488845815-Jul-20 8:32
Member 1488845815-Jul-20 8:32 
QuestionC-Code - CSV-Importexport für Visual Studio 2019 Professional mit MSSQLLocalDB Pin
Member 1488845815-Jul-20 6:57
Member 1488845815-Jul-20 6:57 
AnswerRe: C-Code - CSV-Importexport für Visual Studio 2019 Professional mit MSSQLLocalDB Pin
Nelek15-Jul-20 6:58
protectorNelek15-Jul-20 6:58 
QuestionHow can I use with import date from excel file and export to access Pin
michael nabil7-Apr-18 20:57
michael nabil7-Apr-18 20:57 
QuestionHow can I use WHERE with different date format Pin
yogendarji8-Jan-18 21:48
yogendarji8-Jan-18 21:48 
QuestionInserts rare characters at beginning of first column name Pin
Member 126708246-Aug-16 9:59
Member 126708246-Aug-16 9:59 
Questioni code a parametric method base on "András Slyuch" project Pin
Member 1165240217-Aug-15 10:59
Member 1165240217-Aug-15 10:59 
GeneralMy vote of 4 Pin
Aakash Kelkar24-Apr-15 23:38
Aakash Kelkar24-Apr-15 23:38 
QuestionVery good and useful project. Congratulations. My 5. Pin
Antonio Barros17-Feb-15 3:39
professionalAntonio Barros17-Feb-15 3:39 
Questionhow convert this project into web? Pin
Member 1013455911-Aug-13 4:15
Member 1013455911-Aug-13 4:15 
QuestionDownload Project Pin
Member - Code.Monkey2-Aug-13 9:02
Member - Code.Monkey2-Aug-13 9:02 
QuestionAccessing Files on ftp site Pin
sai krishna kumboju10-Jul-13 11:42
sai krishna kumboju10-Jul-13 11:42 
QuestionHow to load all the column data as string data type? Pin
Member 840468528-Sep-12 13:19
Member 840468528-Sep-12 13:19 
Questionerror Pin
DaigleStyle25-Jul-12 0:51
DaigleStyle25-Jul-12 0:51 
AnswerRe: error Pin
stankotr25-Jul-13 1:22
stankotr25-Jul-13 1:22 
QuestionUsing SamrtDevice Application Pin
Member 914079826-Jun-12 21:30
Member 914079826-Jun-12 21:30 
QuestionHow to make a copy of the selected file and give it a .txt extension. Pin
devenv.exe20-Jun-12 21:31
professionaldevenv.exe20-Jun-12 21:31 
AnswerRe: How to make a copy of the selected file and give it a .txt extension. Pin
András Slyuch20-Jun-12 22:10
András Slyuch20-Jun-12 22:10 
GeneralRe: How to make a copy of the selected file and give it a .txt extension. Pin
devenv.exe20-Jun-12 22:26
professionaldevenv.exe20-Jun-12 22:26 
QuestionAm unable to open connection Pin
David Blay12-May-12 21:56
David Blay12-May-12 21:56 
QuestionWhat is this line? Pin
IrishChieftain20058-Feb-12 9:04
IrishChieftain20058-Feb-12 9:04 
GeneralThanks Pin
HMSaad30-Apr-11 17:17
HMSaad30-Apr-11 17:17 
QuestionUnable to Load CSV File Pin
rsalantry24-Apr-11 6:12
rsalantry24-Apr-11 6:12 
AnswerRe: Unable to Load CSV File Pin
richardcainglet14-Nov-11 0:08
richardcainglet14-Nov-11 0:08 
AnswerRe: Unable to Load CSV File Pin
tomyxd25-Jan-12 18:10
tomyxd25-Jan-12 18:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.