excel导入sqlserver数据库大数据量,可每秒控制数量
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了excel导入sqlserver数据库大数据量,可每秒控制数量相关的知识,希望对你有一定的参考价值。
数据库代码
USE [Test]
GO
/****** Object: Table [dbo].[Table_1] Script Date: 11/07/2017 17:27:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table_1](
[id] [varchar](100) NULL,
[NodeId] [varchar](100) NULL,
[t_B_NodeID] [varchar](100) NULL,
[NodeType] [varchar](100) NULL,
[AuthorityCode] [varchar](100) NULL,
[NodeName] [varchar](100) NULL,
http://www.mamicode.com/info-detail-2080904.html [varchar](100) NULL,
[ActionName] [varchar](100) NULL,
[NodeTag] [varchar](100) NULL,
[OrderNo] [varchar](100) NULL,
[CreateTime] [varchar](100) NULL,
[UpdateTime] [varchar](100) NULL,
[CreateUserId] [varchar](100) NULL,
[UpdateUserId] [varchar](100) NULL,
[IsDel] [varchar](100) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
C#
/// <summary>
/// Excel导入sql
/// </summary>
public void ExcelToSQLText() {
// block copy to DB from Excel
//By xijun,
//step 1 create an excel file C:\Inetpub\wwwroot\test.xls , fill cell(1,1) with "Data",cell(1,2) with "name"
//step 2 create table named "Data" with 2 column ("data","name") in your DB
//there the code below:
DateTime t1 = DateTime.Now;
Response.Write("<br>start time:" + t1.ToString());
string ExcelFile = @"E:\\test2.xlsx";
//IMEX=1:混合模式 HDR=Yes; 是否让第一行作为列头
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFile + ";" + "Extended Properties=\"Excel 12.0;HDR=yes;IMEX=1;\"";
using (OleDbConnection excelConnection = new OleDbConnection(excelConnectionString))
{
excelConnection.Open();
//Getting source data
//非空讀入數據
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", excelConnection);
//数据表
//DataSet ds = new DataSet();
//OleDbDataAdapter myData = new OleDbDataAdapter("select * from [Sheet1$]", excelConnection);
//myData.Fill(ds, "Sheet1$");//填充数据
// Initialize SqlBulkCopy object
using (OleDbDataReader dr = command.ExecuteReader())
{
var TEST = (IDataReader)dr;
// Copy data to destination
string sqlConnectionString = @"Data Source=.;database=Test;user id=sa;pwd=";
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "Table_1";
// //加入只加入一個列的話,那么就會其他數據庫列都默認為空。bulkCopy.ColumnMappings.Add("数据源列名", "插入数据表列名");
bulkCopy.ColumnMappings.Add("id", "id");
bulkCopy.ColumnMappings.Add("NodeId", "NodeId");
bulkCopy.ColumnMappings.Add("t_B_NodeID", "t_B_NodeID");
bulkCopy.ColumnMappings.Add("NodeType", "NodeType");
bulkCopy.ColumnMappings.Add("AuthorityCode", "AuthorityCode");
bulkCopy.ColumnMappings.Add("NodeName", "NodeName");
bulkCopy.ColumnMappings.Add("PageUrl", "PageUrl");
bulkCopy.ColumnMappings.Add("ActionName", "ActionName");
bulkCopy.ColumnMappings.Add("NodeTag", "OrderNo");
bulkCopy.ColumnMappings.Add("CreateTime", "CreateTime");
bulkCopy.ColumnMappings.Add("UpdateTime", "UpdateTime");
bulkCopy.ColumnMappings.Add("CreateUserId", "CreateUserId");
bulkCopy.ColumnMappings.Add("UpdateUserId", "UpdateUserId");
bulkCopy.ColumnMappings.Add("IsDel", "IsDel");
//bcp.BatchSize = 100;//每次传输的行数
// //bcp.NotifyAfter = 100;//进度提示的行数
bulkCopy.BatchSize = 100;
bulkCopy.NotifyAfter = 100;
bulkCopy.WriteToServer((IDataReader)dr);
}
}
//Closing connection
excelConnection.Close();
}
DateTime t2 = DateTime.Now;
Response.Write("<br>End time:" + t2.ToString());
Response.Write("<br>use time:" + ((TimeSpan)(t2 - t1)).Milliseconds.ToString() + " Milliseconds");
Response.Write("<br>inser record count :3307");
}
|
以上是关于excel导入sqlserver数据库大数据量,可每秒控制数量的主要内容,如果未能解决你的问题,请参考以下文章
使用OPENROWSETMicrosoft.ACE.OLEDB实现大数据量的高效导入