使用 C# 将 Excel 文件导入 Microsoft SQL Server [英] Import Excel file into Microsoft SQL Server using C#

查看:27
本文介绍了使用 C# 将 Excel 文件导入 Microsoft SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a C# program that I want to import an Excel file into Microsoft SQL Server 2008 R2.

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

Can someone give me a link or tutorial where I can fully understand on how to this?

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

I've been doing this for a long time and I really don't have an idea on how to implement this.

请帮忙.谢谢.

推荐答案

string excelConnectionString = @"Provider=Microsoft 
.Jet.OLEDB.4.0;Data Source=Book1.xls;Extended 
Properties=""Excel 8.0;HDR=YES;"""; 

//创建到 Excel 工作簿的连接

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

We can Import excel to sql server like this

使用(OleDbConnection 连接 =新的 OleDbConnection(excelConnectionString)){

OleDbCommand 命令 = 新的 OleDbCommand("Select ID,Data FROM [Data$]", 连接);

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); 
    } 

这篇关于使用 C# 将 Excel 文件导入 Microsoft SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆