Import Excel file into Microsoft SQL Server using C#(使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 C# 将 Excel 文件导入 Microsoft SQL Server


基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01