Click here to Skip to main content
16,021,430 members
Articles / Web Development / ASP.NET
Article

A Simplified SQL-CSV Import/Export Functionality

Rate me:
Please Sign up or sign in to vote.
4.58/5 (41 votes)
20 Oct 20056 min read 793.1K   11.2K   204   163
A simplified SQL-CSV import/export functionality.

Image 1

Introduction

This small but important utility has been created for users who would like to understand how to import and export data between a database and CSV files. There are many alternatives to this utility. The (BCP) bulk copy of SQL Server provides a facility to insert multiple records through a single statement. You can also create a nice package to do this job. But I know C# and this is the way I do it. I don't know if I was Googling wrong but didn't find a nice code that can do this functionality in a more generalized manner, and of course, in a way that a normal user (coder) can understand it.

Thank you all for viewing and using this utility. This has helped me to improve and provide something better.

Great News

The application has been updated. What for? Now you can export CSV or text data to your SQL Server or an Access database. Like wise you can export data from SQL Server or an Access database to a CSV or text file. The class files ClsSQLCSV.cs and ClsMSACSV.cs are updated to give more simplicity. Just plug in your control and provide some data to the boxes and you are done. Here is a utility that will allow you to use SQL Server databases as well as MS-Access databases.

Updates

  • The screen-looks have been changed to give some more facility.
  • Instead of separate screens for SQL Server and Access, now a single screen will provide you with a drop down list to select the database of your choice.
  • A separate class file named SetProperties.cs is provided to allow you to set preferences of your choice. You can set different properties like giving the name of the table to import your CSV/text file, and save the CSV/text file to the web server at your specified location (under root folder).
  • For export functionality, you can give the table name to export, give the name of the file to be stored on the web server and the location to where it will be stored.
  • An additional control is provided (not in screen shot) to transfer data between SQL Server and Access. By providing the name of the table, you can transfer data.

Important Points

This sample is prepared using the Northwind database in SQL Server and the Northwind database in MS-Access. The connection string for SQL Server, Access and CSV files are kept in the Web.Config file. The string is read using the namespace System.Collections.Specialized. Remember to make changes to the connection string in the Config file according to your requirement. The sample CSV files from Northwind are in the folder named Sample (CSVSample.csv, TXTSample.txt). Here is the settings in the Web.Config file:

XML
<appSettings>
  <add key="connsql" value="server=DATABASESERVER;database=Northwind;
                     Trusted_Connection=false;uid=sqluserid;pwd=sqlpassword"/>
  <add key="connmsa" value="Provider=Microsoft.Jet.OleDb.4.0;
                     datasource=C:\\Inetpub\\wwwroot\\exportcsv\\Northwind.mdb" />
  <add key="connxls" value="Provider= Microsoft.Jet.OLEDB.4.0;
                     Extended Properties='text;HDR=Yes;FMT=Delimited';Data Source=" />
</appSettings>

How to Run the Application

  • Unzip the files in to a folder (ExportCSV).
  • Create a virtual directory in your IIS, named ExportCSV.
  • Make appropriate changes to the config file as mentioned above.
  • View the sample in the browser using the path: http://localhost/ExportCSV/ImportExport.aspx
  • Select the CSV/TXT file from anywhere on your system and click the "Import" button.
  • Give the name of the existing table to import the records, or provide a new table name.
  • The result of successful completion or failure will be displayed as shown in the above image.
  • Click "Export To CSV" to see the same table being exported to the folder named "exported".

Using the Code

Here is some explanation about using the source:

ImportCSV.ascx.cs

Submit_Click

This event is mentioned here to give you an idea about how to pass the data to the functions and complete your task. You can see an instance of SetProperties() to set the various parameters for importing a file to a database. Check out this list of properties and their use:

  • TableName: Name of the table (new or existing) where the records will be stored.
  • FileInformation: Stores the details of the selected file.
  • DropExistingTable: Checks for the table in your database and deletes it only when this flag is set to true (default is false).
  • CsvDirOnServer: Name of the folder on your web server where the CSV/text file will be stored. This is useful only when you set SaveFileOnServer is set to true.

Note: Make sure that two of the properties TableName and FileInformation are provided. These two are necessary because without them your CSV/text file will not be exported.

C#
private void Submit_Click(object sender, System.EventArgs e)
{
    try
    {
        FileInfo FICSV = new FileInfo(OFDSelectFile.PostedFile.FileName);
        SetProperties sp = new SetProperties();
    
        if (IsValidFile(FICSV))
        {
            sp.TableName = txtTableName.Text;
            sp.CsvDirOnServer = txtCSVDir.Text;
            sp.DropExistingTable=true;
            sp.SaveFileOnServer=true;
            sp.FileInformation = FICSV;

            switch(ddlDB.SelectedItem.Value)
            {
                case "SQL":    
                    ClsSQLCSV objSQL = new ClsSQLCSV();
                    importstatus.Text = objSQL.GenerateTable(sp);
                    break;
                case "MSA":
                    ClsMSACSV objMSA = new ClsMSACSV();
                    importstatus.Text = objMSA.GenerateTable(sp);
                    break;
            }
        }
    }
    catch(Exception ex)
    {
        importstatus.Text = ex.Message.ToString() + "<br>";
        importstatus.Text += "Error importing. Please try again";
    }
}

ExportCSV.ascx.cs

Submit_Click

This event gives you an idea about how to pass the data to the functions and complete your task of export. Here too you can see an instance of SetProperties() to set various parameters for importing a file to a database. Check out this list of properties and their use:

  • ExportTableName: Name of the database table (new or existing) from where the records will be fetched.
  • ExportCSVasName: Name of the CSV/text file to store the data.
  • ExportCSVDirOnServer: Name of the folder where the CSV/text file will be stored on the web server (note that the folder will take the root path of your application). Default is the root path.
  • ExportAsCsvOrText: Default will be "C". This means that the file will be stored with a .csv extension. Not setting this property will also save your file as .csv. You can just provide the name of the file without the extension in the textbox provided to save the file as .csv.

Note: Make sure that two of the properties ExportTableName and ExportCSVasName are provided. These two are necessary because without them your CSV/text file will not be generated.

C#
private void Submit_Click(object sender, System.EventArgs e)
{
    try
    {                
        SetProperties sp = new SetProperties();
        sp.ExportTableName = txtTableName.Text;
        sp.ExportCSVasName = txtCSVFileName.Text;
        sp.ExportCSVDirOnServer = txtCSVDir.Text;
        sp.ExportAsCsvOrText = "T"; //"C" for CSV or "T" for Text
        switch(ddlDB.SelectedItem.Value)
        {
            case "SQL":
                ClsSQLCSV objSQL = new ClsSQLCSV();
                importstatus.Text = objSQL.GenerateCSVFile(sp);
                break;
            case "MSA":
                ClsMSACSV objMSA = new ClsMSACSV();
                importstatus.Text = objMSA.GenerateCSVFile(sp);
                break;
        }
    }
    catch(Exception ex)
    {
        importstatus.Text = ex.Message.ToString() + "<br>";
        importstatus.Text += "Error exporting. Please try again";
    }
}

Library Usage

  • System.Data.Odbc (Connection and fetch records from CSV file).
  • System.Data.SqlClient (Connection and fetch records from SQL Server database table).
  • System.Data.OleDb (Connection and fetch records from Access database table).
  • System.Collections.Specialized (NameValueCollection - get key value pair from configuration settings).
  • System.Configuration (ConfigurationSettings - provides access to configuration settings in a specified configuration section).
  • System.IO (FileStream - read and write the CSV file).

Points of Interest

The two controls for import and export are used in the ImportExport.aspx file. Use them as a separate controls and place them wherever you want on the page/s.

History of Previous Version

The previous version of the utility had no facility to transfer data from a text file. There was no provision for allowing the user to select a CSV file from other locations (except from the web path). No facility was provided for allowing different names to the table other than "TempTable". TempTable was dropped every time before creating a new table. This allowed only new records to be stored.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
India India
He is a Professional developer and project leader, working on .Net Technologies. He works in a firm based in the Hub of IT - Bangalore. His core interests are C#, ASP.net and any microsoft technology that brings him near to Web. Likes to share his views and ideas that help other developers.

Comments and Discussions

 
GeneralRe: Performance with large datasets? Implementation questions... Pin
codeajay15-Jun-05 0:22
codeajay15-Jun-05 0:22 
GeneralRe: Performance with large datasets? Implementation questions... Pin
DigitalStupor27-Jun-05 9:30
DigitalStupor27-Jun-05 9:30 
Generalpoor csv parsing performance using odbc Pin
shriop26-Apr-05 11:19
shriop26-Apr-05 11:19 
GeneralRe: poor csv parsing performance using odbc Pin
codeajay26-Apr-05 20:47
codeajay26-Apr-05 20:47 
GeneralError on writing characters in japanese format Pin
dec9200012-Apr-05 22:21
sussdec9200012-Apr-05 22:21 
GeneralRe: Error on writing characters in japanese format Pin
codeajay13-Apr-05 20:40
codeajay13-Apr-05 20:40 
GeneralRe: Error on writing characters in japanese format Pin
Anonymous13-Apr-05 20:54
Anonymous13-Apr-05 20:54 
GeneralRe: Error on writing characters in japanese format Pin
SQLMonger26-Apr-05 4:54
SQLMonger26-Apr-05 4:54 
Have you considered adding support for TAB delimited format? I find TAB delimited files far less error prone to deal witht than CSV files, as many IT folks don't understand how to build a clean standard CSV file with quoted fields. Using a tab Char(9) as a delimiter is much easier and results in slightly smaller files.

Also, any chance of adding a command line interface so exports and imports can be automated using this tool?

Regards,

Clayton
GeneralRe: Error on writing characters in japanese format Pin
codeajay26-Apr-05 23:49
codeajay26-Apr-05 23:49 
QuestionModification for ACCESS/MDB Use? Pin
DigitalStupor28-Mar-05 4:45
DigitalStupor28-Mar-05 4:45 
AnswerRe: Modification for ACCESS/MDB Use? Pin
codeajay28-Mar-05 21:38
codeajay28-Mar-05 21:38 
GeneralRe: Modification for ACCESS/MDB Use? Pin
DigitalStupor31-Mar-05 7:59
DigitalStupor31-Mar-05 7:59 
GeneralRe: Modification for ACCESS/MDB Use? Pin
codeajay12-Apr-05 18:56
codeajay12-Apr-05 18:56 
GeneralRe: Modification for ACCESS/MDB Use? Pin
DigitalStupor13-Apr-05 5:06
DigitalStupor13-Apr-05 5:06 
GeneralRe: Modification for ACCESS/MDB Use? Pin
codeajay13-Apr-05 19:35
codeajay13-Apr-05 19:35 
Generaldelimiter-bug Pin
Aviram Solomon26-Feb-05 11:12
Aviram Solomon26-Feb-05 11:12 
GeneralRe: delimiter-bug Pin
bigals12-Apr-05 12:19
bigals12-Apr-05 12:19 
GeneralGood Work!!! Pin
mahaveergang4-Feb-05 17:26
mahaveergang4-Feb-05 17:26 
GeneralRe: Good Work!!! Pin
codeajay7-Feb-05 18:23
codeajay7-Feb-05 18:23 

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.