SqlBulkCopy - 外部表不是预期的格式

发布于 2024-11-14 01:15:25 字数 1570 浏览 3 评论 0原文

我正在尝试按照 使用 SqlBulkCopy 将 Excel 电子表格数据导入 SQL Server 数据库表

我已经创建了表格和 Excel 工作表并完成了编码,但我不断收到

外部表的格式不符合预期。

connection.open() 行。

我该如何摆脱这个问题?

    // Connection String to Excel Workbook
    String savePath = @"C:\TEMP\";

    String fileName = "upload.xls";
    savePath += fileName;
    //newpath += fileName;
    FileUpload1.SaveAs(savePath);

    string excelConnectionString =
        "Provider=Microsoft.Jet.OLEDB.4.0;" +
        "Data    Source=" + savePath + ";" +
        "Extended Properties=Excel 8.0;";

    // Create Connection to Excel Workbook
    using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
    {
        OleDbCommand command = new OleDbCommand("Select ID,Data FROM [Data$]", connection);

        connection.Open();

        // Create DbDataReader to Data Worksheet
        using (DbDataReader dr = command.ExecuteReader())
        {
            // SQL Server Connection String
            string sqlConnectionString = "Data Source=GRACC011334\\SQLEXPRESS;Initial Catalog=ComputerBroadcastNetwork;Integrated Security=True";

            // Bulk Copy to SQL Server
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
            {
                bulkCopy.DestinationTableName = "ExcelData";
                bulkCopy.WriteToServer(dr);
            }
        }
    }

I'm trying to use SqlBulkCopy as per the example in Import Excel Spreadsheet Data into SQL Server Database Table Using SqlBulkCopy.

I've created the table and Excel sheet OK and done the coding, but I keep getting

External table is not in the expected format.

at the connection.open() line.

How do I get rid of this problem?

    // Connection String to Excel Workbook
    String savePath = @"C:\TEMP\";

    String fileName = "upload.xls";
    savePath += fileName;
    //newpath += fileName;
    FileUpload1.SaveAs(savePath);

    string excelConnectionString =
        "Provider=Microsoft.Jet.OLEDB.4.0;" +
        "Data    Source=" + savePath + ";" +
        "Extended Properties=Excel 8.0;";

    // Create Connection to Excel Workbook
    using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
    {
        OleDbCommand command = new OleDbCommand("Select ID,Data FROM [Data$]", connection);

        connection.Open();

        // Create DbDataReader to Data Worksheet
        using (DbDataReader dr = command.ExecuteReader())
        {
            // SQL Server Connection String
            string sqlConnectionString = "Data Source=GRACC011334\\SQLEXPRESS;Initial Catalog=ComputerBroadcastNetwork;Integrated Security=True";

            // Bulk Copy to SQL Server
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
            {
                bulkCopy.DestinationTableName = "ExcelData";
                bulkCopy.WriteToServer(dr);
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慢慢从新开始 2024-11-21 01:15:25

我得到了它。我不得不更改连接字符串的格式并将其更改为

string excelConnectionString = (@"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\TEMP\Book1.xls;Extended Properties='Excel 8.0;HDR=NO;IMEX=1'"); 

I got it. I had to change the format of the connection string and changed it to

string excelConnectionString = (@"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\TEMP\Book1.xls;Extended Properties='Excel 8.0;HDR=NO;IMEX=1'"); 
孤城病女 2024-11-21 01:15:25

对我来说,此错误是由于尝试从较新的 .xlsx 文件格式的电子表格中批量复制而引起的。将电子表格转换为旧的 .xls 格式后,我就可以进行复制而不会收到错误。

我使用与上面指定的相同连接字符串,

Provider=Microsoft.Jet.OLEDB.4.0 和 Extended Properties=Excel 8.0

所以显然 .xlsx 文件需要不同的 Provider 和/或扩展属性

For me this error was caused by trying to bulk copy from a spreadsheet in the newer .xlsx file format. After I converted the spreadsheet to the older .xls format, then I was able to do teh copy without receiving the error.

I am using the same connection string that you specified above, with

Provider=Microsoft.Jet.OLEDB.4.0 and Extended Properties=Excel 8.0

So apparently .xlsx files need different Provider and/or Extended Properties

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文