使用C#将Excel文件导入Microsoft SQL Server

sea*_*ean 0 c# excel import-from-excel sql-server-2008-r2

我有一个C#程序,我想将Excel文件导入Microsoft SQL Server 2008 R2.

有人可以给我一个链接或教程,我可以完全理解如何做到这一点?

我已经做了很长时间了,我真的不知道如何实现这个.

请帮忙.谢谢.

小智 6

string excelConnectionString = @"Provider=Microsoft 
.Jet.OLEDB.4.0;Data Source=Book1.xls;Extended 
Properties=""Excel 8.0;HDR=YES;"""; 
Run Code Online (Sandbox Code Playgroud)

// Create Connection to Excel Workbook

我们可以像这样导入excel到sql server

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=.; 
       Initial Catalog=Test;Integrated Security=True"; 


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