Asp.Net: Using SqlBulkCopy to upload a CSV (comma delimited) file

by Phill 16. June 2009 05:01

SqlBulkCopy is a really powerful & fast way to upload data from one source to another SQL based database.

Most examples are for use when uploading an Excel spreadsheet, however a common requirement is the need to upload a CSV file, yet strangely there is little info on the web. More importantly since we the asp.net FileUpload control doesnt like revealing the full path of the file it is uploading, it becomes near impossible to perform this action as required. This is because we need to know the location of the file inorder to connect to it and read its data.

Here i we show you a nice little work around. It works by first uploading the file to the server, transfering the data from it into the necessary database, before finally deleting the original file from the server.

if (FileUpload2.PostedFile.FileName != null){
//filenamestring strUPloadFile = FileUpload2.PostedFile.FileName;strUPloadFile =  strUPloadFile;//save to tmp folderstring strPath = MapPath("tmp_uploads/");
try{//save to: tmp_uploads/filenameFileUpload2.SaveAs(strPath +  strUPloadFile);}catch (Exception ex){throw ex;}
 
using (OleDbConnection dconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" 
+ strPath + ";Extended Properties=\"text;HDR=No;FMT=Delimited\"")){
using (OleDbCommand dcmd = new OleDbCommand("SELECT * FROM " + strUPloadFile + "", dconn)){try{dconn.Open();
using (OleDbDataReader dreader = dcmd.ExecuteReader()){
// Bulk Copy to SQL Serverusing (SqlBulkCopy bulkCopy = new SqlBulkCopy(WebConfigurationManager.ConnectionStrings["############"].ConnectionString)){
bulkCopy.DestinationTableName = "tbl_your_table_name_here";// Set the BatchSize.bulkCopy.BatchSize = 500;
try{//perform copybulkCopy.WriteToServer(dreader);}
catch (Exception ex){//errorthrow(ex);}}}}finally{dconn.Close();}} 
//delete file from servertry{File.Delete(strPath + strUPloadFile);}catch(Exception ex){throw ex;} 
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Theme by Extensive SEO

About the author

Phill Healey: I am a self-employed web & multimedia giraffe from Manchester now based in Greenwich London.

 

My company is called 'Electric Media', and this is my blog. Its all about the wonderful world of web design, life in Manchester, and my random random thoughts and mutterings.

 

I do hope you enjoy!

Tag cloud

RecentComments

Comment RSS

Copyright Disclaimer

Copyright © and Moral Rights for any discussion papers or working papers or articles on this site, images posted on this site and any postings authored on this site are retained by the individual authors and/or other copyright owners.

You may not engage in further distribution of the material, or use the material, for any profit-making activities or any commercial gain.