- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我在这里看到了这篇文章并阅读了它,似乎批量复制可能是要走的路。
What’s the best way to bulk database inserts from c#?
我仍然有一些问题,想知道实际情况如何。
所以我找到了2个教程。
http://www.codeproject.com/KB/cs/MultipleInsertsIn1dbTrip.aspx#_Toc196622241
http://www.codeproject.com/KB/linq/BulkOperations_LinqToSQL.aspx
第一种方式使用 2 个 ado.net 2.0 功能。批量插入和批量复制。第二个使用 linq to sql 和 OpenXML。
这种类型对我很有吸引力,因为我已经在使用 linq to sql 并且比 ado.net 更喜欢它。然而,正如一个人在帖子中指出的那样,他只是以性能为代价来解决这个问题(我认为这没有错)
首先我会讲第一个教程中的2种方式
我正在使用 VS2010 Express(为了测试我使用 VS2008 的教程,但不确定我刚刚加载了示例文件并运行了哪个 .net 版本)、.net 4.0、MVC 2.0、SQl Server 2005
CREATE TABLE [dbo].[TBL_TEST_TEST]
(
ID INT IDENTITY(1,1) PRIMARY KEY,
[NAME] [varchar](50)
)
USE [Test]
GO
/****** Object: StoredProcedure [dbo].[sp_BatchInsert] Script Date: 05/19/2010 15:12:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_BatchInsert] (@Name VARCHAR(50) )
AS
BEGIN
INSERT INTO TBL_TEST_TEST VALUES (@Name);
END
/// <summary>
/// Another ado.net 2.0 way that uses a stored procedure to do a bulk insert.
/// Seems slower then "BatchBulkCopy" way and it crashes when you try to insert 500,000 records in one go.
/// http://www.codeproject.com/KB/cs/MultipleInsertsIn1dbTrip.aspx#_Toc196622241
/// </summary>
private static void BatchInsert()
{
// Get the DataTable with Rows State as RowState.Added
DataTable dtInsertRows = GetDataTable();
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("sp_BatchInsert", connection);
command.CommandType = CommandType.StoredProcedure;
command.UpdatedRowSource = UpdateRowSource.None;
// Set the Parameter with appropriate Source Column Name
command.Parameters.Add("@Name", SqlDbType.VarChar, 50, dtInsertRows.Columns[0].ColumnName);
SqlDataAdapter adpt = new SqlDataAdapter();
adpt.InsertCommand = command;
// Specify the number of records to be Inserted/Updated in one go. Default is 1.
adpt.UpdateBatchSize = 1000;
connection.Open();
int recordsInserted = adpt.Update(dtInsertRows);
connection.Close();
}
System.Data.SqlClient.SqlException was unhandled
Message="A transport-level error has occurred when sending the request to the server. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)"
Source=".Net SqlClient Data Provider"
ErrorCode=-2146232060
Class=20
LineNumber=0
Number=233
Server=""
State=0
StackTrace:
at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at TestIQueryable.Program.BatchInsert() in C:\Users\a\Downloads\TestIQueryable\TestIQueryable\TestIQueryable\Program.cs:line 124
at TestIQueryable.Program.Main(String[] args) in C:\Users\a\Downloads\TestIQueryable\TestIQueryable\TestIQueryable\Program.cs:line 16
InnerException:
/// <summary>
/// An ado.net 2.0 way to mass insert records. This seems to be the fastest.
/// http://www.codeproject.com/KB/cs/MultipleInsertsIn1dbTrip.aspx#_Toc196622241
/// </summary>
private static void BatchBulkCopy()
{
// Get the DataTable
DataTable dtInsertRows = GetDataTable();
using (SqlBulkCopy sbc = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))
{
sbc.DestinationTableName = "TBL_TEST_TEST";
// Number of records to be processed in one go
sbc.BatchSize = 500000;
// Map the Source Column from DataTabel to the Destination Columns in SQL Server 2005 Person Table
// sbc.ColumnMappings.Add("ID", "ID");
sbc.ColumnMappings.Add("NAME", "NAME");
// Number of records after which client has to be notified about its status
sbc.NotifyAfter = dtInsertRows.Rows.Count;
// Event that gets fired when NotifyAfter number of records are processed.
sbc.SqlRowsCopied += new SqlRowsCopiedEventHandler(sbc_SqlRowsCopied);
// Finally write to server
sbc.WriteToServer(dtInsertRows);
sbc.Close();
}
}
USE [Test]
GO
/****** Object: StoredProcedure [dbo].[spTEST_InsertXMLTEST_TEST] Script Date: 05/19/2010 15:39:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spTEST_InsertXMLTEST_TEST](@UpdatedProdData nText)
AS
DECLARE @hDoc int
exec sp_xml_preparedocument @hDoc OUTPUT,@UpdatedProdData
INSERT INTO TBL_TEST_TEST(NAME)
SELECT XMLProdTable.NAME
FROM OPENXML(@hDoc, 'ArrayOfTBL_TEST_TEST/TBL_TEST_TEST', 2)
WITH (
ID Int,
NAME varchar(100)
) XMLProdTable
EXEC sp_xml_removedocument @hDoc
/// <summary>
/// This is using linq to sql to make the table objects.
/// It is then serailzed to to an xml document and sent to a stored proedure
/// that then does a bulk insert(I think with OpenXML)
/// http://www.codeproject.com/KB/linq/BulkOperations_LinqToSQL.aspx
/// </summary>
private static void LinqInsertXMLBatch()
{
using (TestDataContext db = new TestDataContext())
{
TBL_TEST_TEST[] testRecords = new TBL_TEST_TEST[500000];
for (int count = 0; count < 500000; count++)
{
TBL_TEST_TEST testRecord = new TBL_TEST_TEST();
testRecord.NAME = "Name : " + count;
testRecords[count] = testRecord;
}
StringBuilder sBuilder = new StringBuilder();
System.IO.StringWriter sWriter = new System.IO.StringWriter(sBuilder);
XmlSerializer serializer = new XmlSerializer(typeof(TBL_TEST_TEST[]));
serializer.Serialize(sWriter, testRecords);
db.insertTestData(sBuilder.ToString());
}
}
/// <summary>
/// This is using linq to sql to to insert lots of records.
/// This way is slow as it uses no mass insert.
/// Only tried to insert 50,000 records as I did not want to sit around till it did 500,000 records.
/// http://www.codeproject.com/KB/linq/BulkOperations_LinqToSQL.aspx
/// </summary>
private static void LinqInsertAll()
{
using (TestDataContext db = new TestDataContext())
{
db.CommandTimeout = 600;
for (int count = 0; count < 50000; count++)
{
TBL_TEST_TEST testRecord = new TBL_TEST_TEST();
testRecord.NAME = "Name : " + count;
db.TBL_TEST_TESTs.InsertOnSubmit(testRecord);
}
db.SubmitChanges();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Data;
using System.Data.SqlClient;
namespace TestIQueryable
{
class Program
{
private static string connectionString = "";
static void Main(string[] args)
{
BatchInsert();
Console.WriteLine("done");
}
/// <summary>
/// This is using linq to sql to to insert lots of records.
/// This way is slow as it uses no mass insert.
/// Only tried to insert 50,000 records as I did not want to sit around till it did 500,000 records.
/// http://www.codeproject.com/KB/linq/BulkOperations_LinqToSQL.aspx
/// </summary>
private static void LinqInsertAll()
{
using (TestDataContext db = new TestDataContext())
{
db.CommandTimeout = 600;
for (int count = 0; count < 50000; count++)
{
TBL_TEST_TEST testRecord = new TBL_TEST_TEST();
testRecord.NAME = "Name : " + count;
db.TBL_TEST_TESTs.InsertOnSubmit(testRecord);
}
db.SubmitChanges();
}
}
/// <summary>
/// This is using linq to sql to make the table objects.
/// It is then serailzed to to an xml document and sent to a stored proedure
/// that then does a bulk insert(I think with OpenXML)
/// http://www.codeproject.com/KB/linq/BulkOperations_LinqToSQL.aspx
/// </summary>
private static void LinqInsertXMLBatch()
{
using (TestDataContext db = new TestDataContext())
{
TBL_TEST_TEST[] testRecords = new TBL_TEST_TEST[500000];
for (int count = 0; count < 500000; count++)
{
TBL_TEST_TEST testRecord = new TBL_TEST_TEST();
testRecord.NAME = "Name : " + count;
testRecords[count] = testRecord;
}
StringBuilder sBuilder = new StringBuilder();
System.IO.StringWriter sWriter = new System.IO.StringWriter(sBuilder);
XmlSerializer serializer = new XmlSerializer(typeof(TBL_TEST_TEST[]));
serializer.Serialize(sWriter, testRecords);
db.insertTestData(sBuilder.ToString());
}
}
/// <summary>
/// An ado.net 2.0 way to mass insert records. This seems to be the fastest.
/// http://www.codeproject.com/KB/cs/MultipleInsertsIn1dbTrip.aspx#_Toc196622241
/// </summary>
private static void BatchBulkCopy()
{
// Get the DataTable
DataTable dtInsertRows = GetDataTable();
using (SqlBulkCopy sbc = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))
{
sbc.DestinationTableName = "TBL_TEST_TEST";
// Number of records to be processed in one go
sbc.BatchSize = 500000;
// Map the Source Column from DataTabel to the Destination Columns in SQL Server 2005 Person Table
// sbc.ColumnMappings.Add("ID", "ID");
sbc.ColumnMappings.Add("NAME", "NAME");
// Number of records after which client has to be notified about its status
sbc.NotifyAfter = dtInsertRows.Rows.Count;
// Event that gets fired when NotifyAfter number of records are processed.
sbc.SqlRowsCopied += new SqlRowsCopiedEventHandler(sbc_SqlRowsCopied);
// Finally write to server
sbc.WriteToServer(dtInsertRows);
sbc.Close();
}
}
/// <summary>
/// Another ado.net 2.0 way that uses a stored procedure to do a bulk insert.
/// Seems slower then "BatchBulkCopy" way and it crashes when you try to insert 500,000 records in one go.
/// http://www.codeproject.com/KB/cs/MultipleInsertsIn1dbTrip.aspx#_Toc196622241
/// </summary>
private static void BatchInsert()
{
// Get the DataTable with Rows State as RowState.Added
DataTable dtInsertRows = GetDataTable();
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("sp_BatchInsert", connection);
command.CommandType = CommandType.StoredProcedure;
command.UpdatedRowSource = UpdateRowSource.None;
// Set the Parameter with appropriate Source Column Name
command.Parameters.Add("@Name", SqlDbType.VarChar, 50, dtInsertRows.Columns[0].ColumnName);
SqlDataAdapter adpt = new SqlDataAdapter();
adpt.InsertCommand = command;
// Specify the number of records to be Inserted/Updated in one go. Default is 1.
adpt.UpdateBatchSize = 500000;
connection.Open();
int recordsInserted = adpt.Update(dtInsertRows);
connection.Close();
}
private static DataTable GetDataTable()
{
// You First need a DataTable and have all the insert values in it
DataTable dtInsertRows = new DataTable();
dtInsertRows.Columns.Add("NAME");
for (int i = 0; i < 500000; i++)
{
DataRow drInsertRow = dtInsertRows.NewRow();
string name = "Name : " + i;
drInsertRow["NAME"] = name;
dtInsertRows.Rows.Add(drInsertRow);
}
return dtInsertRows;
}
static void sbc_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
{
Console.WriteLine("Number of records affected : " + e.RowsCopied.ToString());
}
}
}
最佳答案
批量大小是为了减少网络延迟的影响。它不需要超过几千。多条语句收集在一起并作为一个单元发送,因此您每 N 条语句获得一次网络旅行的命中率,而不是每条语句一次。
关于c# - 批量插入最好的办法是什么? + 帮助我完全理解我目前的发现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2870022/
我希望任何人都可以在这个主题上为我提供帮助,即使这不是特定的编程问题。 我正在写一个单例论文,将MySQL与MongoDB进行比较,并且我想写一些有关Youtube的东西,因为该平台必须处理大量数据负
使用W3C validator验证HTML标记中的音频和视频时出现错误。 Validation Output: 1 Error Error Line 28, Column
我正在使用的功能是 function joinslots(freeTimings){ joined=[]; if(freeTimings.length==1){ joi
我的 Play 商店应用遇到问题。目前,一些日常应用统计数据出现延迟。我们希望尽快解决该问题。 最佳答案 请稍等,几天或更快后即可使用。有时他们的服务器重载,这就是原因。 关于google-play
使用外连接合并两个表。让我们说 df1 = ['产品ID', '名称'] df2 = ['用户ID', '产品ID', '使用情况'] 我尝试在 pandas 中使用带有合并功能的外连接。 pd.me
我正在做我的第一个表格计算,这是我在过去一天研究并想出的 jQuery。 顺便说一下,这只是我发现的让一切正常运行的方法,不一定是最好的方法。我的老板希望我使用 .blur() 而不是 .on(),但
Scalamock 拒绝了我的 mock 尝试,说它不支持超过 22 种方法。 原因是因为在我试图模拟的类中总共有超过 22 个方法(2 个是我的,20 多个是混合的(来自 Akka Json 支持)
应用商店里有一个叫 Touchpad 的应用,最后一次更新是在 11 月 29 日,其中包含一个新功能,支持“使用设备键盘上的 Siri 键向电脑发送文本”,我想知道是否有开放的 API Siri现在
目前(2009 年年中)GCJ 的现状如何? 最新消息是2007年的,所以我想知道是否有任何形式的进展?我记得不久前有一个可用的 lucene 编译版本,它使用 gcj 从 java 源代码编译它。目
我是第一次开始使用 OpenCL,我正在尝试优化缩减内核。内核采用大小为宽度像素的 float 正方形网格(数据表示灰度图像的亮度值)。内核对每一列求和并将每一列的总和返回到输出数组。 /* inpu
有一个正在运行的 github 线程(似乎已关闭并且已经完成了一堆提交/合并)。 尽管 VirtualBox 和 Vagrant 还不到 2 周大:*编辑:我仍然看到与/root/.my.cnf 相关
在 Android Studio 的 androidTest 文件夹中,我有几个测试用例,如下所示: Screenshot : Android Studio每个测试类执行后,应用程序退出并重新启动以进
目前正在开发一个包含 google maps 的应用程序,我愿意让它也适用于中国。 我知道中国的情况在过去几年发生了一些变化。所以我想知道:是否可以在中国的 Android 应用程序中使用谷歌地图?
我正在尝试使用 Cosmos DB RestAPI 列出本地(模拟器)实例上的数据库,但进展不够。有谁知道我在这里做错了什么...... var crypto = require("crypto");
有没有办法自定义 HTML 并以 html 格式发送通知电子邮件? 最佳答案 只需调整您的模板,例如 > https://github.com/pinax/django-notification/bl
我正在寻找一种方法来在类似于 Youtube 视频显示的 html5 视频上创建加载动画(引用: https://www.youtube.com/watch?v=5vcCBHVyG50 ) 我用 ca
我正在关注 Google Codelabs for instant app ,我正在尝试创建 topeka-ui(即时应用程序的 UI 功能模块)。 它告诉我为该 UI 模块启用这样的数据绑定(bin
我在 iPad 上使用 APSplitViewController 来获得两侧。在右侧工作时,我有一个位于 UINavigationController 内的 viewController。 当我以模
当前(但不常见),如果您调用以下 url,您会收到内部服务器错误: https://api.linkedin.com/v1/people/~/connections:(id,headline,pict
def cat_latin_word(text): """ convert the string in another form """ constant = "bcdfghj
我是一名优秀的程序员,十分优秀!