简体   繁体   中英

Import excel file which is on local machine to SQL server 2005

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?

在数据源中将textBox1更改为textBox1.Text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM