I have a path to an Excel file which is located on my local machine and I want to import the data of the Excel file to SQL Server 2005.
I have tried this code but it gives me an error:
string sSourceConStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + textBox1 + "; Extended Properties=" + "\"Excel 8.0;HDR=YES;+\"";
string dDestConStr = @"server=severIPAddress;database=databaseName;uid=userName;password=pwd";
OleDbConnection sSourceConnection = new OleDbConnection(sSourceConStr);
using (sSourceConnection)
{
string sql = string.Format("Select * FROM [{0}]", "Sheet1$");
OleDbCommand command = new OleDbCommand(sql, sSourceConnection);
sSourceConnection.Open();
using (OleDbDataReader dr = command.ExecuteReader())
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(dDestConStr))
{
bulkCopy.DestinationTableName = "dbo.databaseName";
bulkCopy.WriteToServer(dr);
}
}
}
Error:
OleDBException was unhandled. Failure creating file.
What would cause this and how can I resolve it?
Change textBox1 to textBox1.Text in Data Source