问题描述
我有我想要导入Excel文件到的Microsoft SQL Server 2008 R2的C#程序。
能有人给我一个链接或教程,我可以完全对如何呢?
我明白已经被长时间这样做的,我真的没有对如何实现这一点的想法。
请帮忙。谢谢你。
字符串excelConnectionString = @供应商=微软
.Jet .OLEDB.4.0;数据源= Book1.xls的;扩展
属性=的Excel 8.0; HDR = YES;;
//创建连接到Excel工作簿
我们可以导入Excel中使用SQL这样的服务器
(OleDbConnection的连接=
新的OleDbConnection(excelConnectionString)){
OleDbCommand的命令=新的OleDbCommand
(选择ID,数据[数据$],连接);
connection.Open();
`//使用创建DbDataReader到数据表`
(DbDataReader博士= Command.ExecuteReader却())
{
// SQL Server连接字符串
字符串sqlConnectionString =数据源=;
初始目录=测试;集成安全性=真;使用
//批量复制到SQL Server
(SqlBulkCopy的bulkCopy =
新SqlBulkCopy的(sqlConnectionString))
{
bulkCopy.DestinationTableName =ExcelData;
bulkCopy.WriteToServer(DR);
}
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.
Please help. Thanks.
string excelConnectionString = @"Provider=Microsoft
.Jet.OLEDB.4.0;Data Source=Book1.xls;Extended
Properties=""Excel 8.0;HDR=YES;""";
// Create Connection to Excel Workbook
We can Import excel to sql server like this
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);
}
这篇关于导入Excel文件到Microsoft SQL Server的使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!