Store Excel Sheet data to sql server

I am getting error message like

Format of the initialization string does not conform to specification starting at index 0.

While exporting an Excel file. Please advise

This is my code:

string file = string.Format("{0}\\{1}", Request.PhysicalApplicationPath,Guid.NewGuid().ToString().Replace("-",string.Empty));
fileuploadExcel.SaveAs(file);

//Create connection string to Excel work book
string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HRD=YES;IMEX=1'", fileuploadExcel.PostedFile.FileName);

//Create Connection to Excel work book
OleDbConnection excelConnection =new OleDbConnection(file);

//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Desi] from [Varun]", 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();

Answers


Well, I see one thing that's easy to solve... try changing:

SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);

to be:

SqlBulkCopy sqlBulk = new SqlBulkCopy(excelConnection);

In case you still have problems... general points:

  1. A trick to know, for searching for error messages on Google: enclose your search string in quotes. When I searched on "Format of the initialization string does not conform to specification starting at index 0" (the QUOTES were critical), I found a lot of examples, all of which said:
  2. It's a problem with the connection string.

Of course please let us know if the change to excelConnection helps... whether or not you have other questions... and if your final code is much different, please add that final code to the bottom of your question as an "Update:"

Two other questions: (A) What version of SQL Server are you working with (often helpful to know)? (B) Have you considered importing the Excel spreadsheet into SQL Server first, then accessing it as a table? In Management Studio (or Enterprise Manager), if you right-click on a database name, then go to "Tasks" you'll see an option to import data. In SQL Server 2005 or later, that wizard uses SSIS; in SQL Server 2000, that wizard uses DTS.

If all you're intending is a one-time import, those wizards are a much easier approach.

With the thought that this import is to be re-used, for a spreadsheet that's updated over time... I'd still consider importing to SQL Server first, because if you're talking about an Excel spreadsheet that's being updated by human beings, you're quite possibly going to run into problems with format predictability: did the humans paste in some cells with a particular (different) format? (That can result in NULL/empty data... esp. with SSIS -- DTS was more forgiving.) Did they try to create a new tab in the spreadsheet? (That effectively changes your FROM: the "tablename" has to match the name of the "tab" in Excel.) Of course those events can similarly effect the importing into SQL Server... but at least the import target -- the SQL Server table -- can always exist, and always be the same format, so the user-visible or real-time problems affecting your webpage / ASP.NET app will be more predictable.

Hope that helps...


Need Your Help

Display a number on the screen Phone SDK

ios iphone cocoa-touch load

Does anyone know how to display a number on the screen/iphone simulator?