sqlBulk insert C#

c# csv sqlbulkcopy

Question

I have a page where I want to upload a CSV file from my computer to database on the server and I have my opentext that looks like the following

using (StreamReader sr = File.OpenText(@"c:\users\workstationUsername\FileName.csv"))

This works fine on my local machine but when I push this to the server it tries to read the server's C Drive and I want it to read the physical file location that is sitting on the desktop of the user's computer not the server, when they click browse and upload.. Thank you

below is the complete code:

if (IsPostBack)
{

   // SetDefaultDates();
   Boolean fileOK = false;
   String dateString = DateTime.Now.ToString("MMddyyyy");
   String UserName = User.Identity.Name;
   String path = Server.MapPath("~/Uploads/CSVs/");

   string stringpath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

   String fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
   stringpath = stringpath + fileName; 
   String LocationToSave = path + "\\" + fileName;
   if (FileUpload1.HasFile)
   {
       String fileExtension =
                System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
       String[] allowedExtensions = { ".csv" };
       for (int i = 0; i < allowedExtensions.Length; i++)
       {
           if (fileExtension == allowedExtensions[i])
           {
               fileOK = true;
           }
       }
   }

   if (fileOK)
   {
       try
       {
           //FileUpload1.PostedFile.SaveAs(LocationToSave + dateString + "-" + FileUpload1.FileName);
           FileUpload1.PostedFile.SaveAs(LocationToSave);
           Label1.Text = "File " + FileUpload1.FileName + " uploaded!";

           DataTable dt = new DataTable();
           string line = null;
           int i = 0;

           using (StreamReader sr = File.OpenText(stringpath)) 
           {
               while ((line = sr.ReadLine()) != null)
               {
                   string[] data = line.Split(',');
                   if (data.Length > 0)
                   {
                       if (i == 0)
                       {
                           foreach (var item in data)
                           {
                               dt.Columns.Add(new DataColumn());
                           }
                           i++;
                       }
                       DataRow row = dt.NewRow();
                       row.ItemArray = data;
                       dt.Rows.Add(row);
                   }
               }
           }

           using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString))
           {
               cn.Open();
               using (SqlBulkCopy copy = new SqlBulkCopy(cn))
               {
                   copy.WriteToServer(dt);
               }
           }
       }
       catch (Exception ex)
       {
           Label1.Text = "File " + FileUpload1.FileName + " could not be uploaded." + ex.Message;
       }
   }
   else
   {
       Label1.Text = "Cannot accept files of this type. " + FileUpload1.FileName;
   }
}
SetDefaultDates();
}
1
0
4/25/2014 3:01:03 PM

Popular Answer

If you have a FileUpload control, then instead of using (StreamReader sr = File.OpenText(@"c:\users\workstationUsername\FileName.csv")) which obvously is pointing to the server's hard drive you can do this:

(StreamReader sr = new StreamReader(fileUploadControl.FileContent))
    //Do your stuff
3
4/25/2014 2:22:35 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow