title: Uploading an Excel sheet and importing the data into SQL Server database
body: <p>I am developing this simple application to upload an Excel file (<code>.xlsx</code>) and import the data present in that Excel worksheet into a SQL Server Express database in .NET </p>
  
  <p>I'm using the following code on click of the import button after browsing and selecting the file to do it.</p>
  
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
  {
          String strConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename='C:\\Users\\Hemant\\documents\\visual studio 2010\\Projects\\CRMdata\\CRMdata\\App_Data\\Database1.mdf';Integrated Security=True;User Instance=True";
          //file upload path
          string path = FileUpload1.PostedFile.FileName;
          //string path="C:\\ Users\\ Hemant\\Documents\\example.xlsx";
          //Create connection string to Excel work book
          string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
          //Create Connection to Excel work book
          OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
          //Create OleDbCommand to fetch data from Excel
          OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation] from [Sheet1$]", excelConnection);
          excelConnection.Open();
          OleDbDataReader dReader;
          dReader = cmd.ExecuteReader();
          SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
          //Give your Destination table name
          sqlBulk.DestinationTableName = "Excel_table";
          sqlBulk.WriteToServer(dReader);
          excelConnection.Close();
      }
  </code></pre>
  
  <p>But the code doesn't run when I use </p>
  
  <pre><code>string path = FileUpload1.PostedFile.FileName;`
  </code></pre>
  
  <p>and even </p>
  
  <pre><code>string path="C:\ Users\ Hemant\Documents\example.xlsx";` 
  </code></pre>
  
  <p>The <code>dReader</code> is unable to take the path in this format.</p>
  
  <p>It is only able to take path in the following format </p>
  
  <pre><code>string path="C:\\ Users\\ Hemant\\Documents\\example.xlsx";
  </code></pre>
  
  <p>i.e. with the the <code>\\</code> in the path.For which I have to hard code the path but we have to browse the file. </p>
  
  <p>So,can any one please suggest a solution to use the path taken by the <code>FileUpload1</code> to import the data?</p>