在sql server 2012中插入15.000条记录或更多的最佳方式是什么?存储过程?纯插入?顺便说一句,我在C#中使用.NET应用程序。在sql server 2012上插入15.000记录的最佳方法是什么?
1
A
回答
1
最佳方法是使用table-valued parameters(TVP)批量传递数据作为SQL Server存储过程的参数。
次要的是在.NET中使用SQlBulkCopy class,它也可以接受DataTable作为参数。
0
大容量复制将是一个不错的选择。
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = GetConnectionString();
// Open a sourceConnection to the AdventureWorks database.
using (SqlConnection sourceConnection =
new SqlConnection(connectionString))
{
sourceConnection.Open();
// Perform an initial count on the destination table.
SqlCommand commandRowCount = new SqlCommand(
"SELECT COUNT(*) FROM " +
"dbo.BulkCopyDemoMatchingColumns;",
sourceConnection);
long countStart = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Starting row count = {0}", countStart);
// Get data from the source table as a SqlDataReader.
SqlCommand commandSourceData = new SqlCommand(
"SELECT ProductID, Name, " +
"ProductNumber " +
"FROM Production.Product;", sourceConnection);
SqlDataReader reader =
commandSourceData.ExecuteReader();
// Open the destination connection. In the real world you would
// not use SqlBulkCopy to move data from one table to the other
// in the same database. This is for demonstration purposes only.
using (SqlConnection destinationConnection =
new SqlConnection(connectionString))
{
destinationConnection.Open();
// Set up the bulk copy object.
// Note that the column positions in the source
// data reader match the column positions in
// the destination table so there is no need to
// map columns.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(destinationConnection))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns";
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Close the SqlDataReader. The SqlBulkCopy
// object is automatically closed at the end
// of the using block.
reader.Close();
}
}
// Perform a final count on the destination
// table to see how many rows were added.
long countEnd = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Ending row count = {0}", countEnd);
Console.WriteLine("{0} rows were added.", countEnd - countStart);
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
}
}
private static string GetConnectionString()
// To avoid storing the sourceConnection string in your code,
// you can retrieve it from a configuration file.
{
return "Data Source=(local); " +
" Integrated Security=true;" +
"Initial Catalog=AdventureWorks;";
}
}
相关问题
- 1. 验证记录是否成功插入的最佳方法是什么?
- 2. 将大量记录插入SQL Server数据库的最快方法是什么?
- 3. 插入记录使用SQL Server 2012
- 4. 在SQL Server上插入多行的最佳方法
- 5. 什么是为SQL插入生成ID的最佳方法?
- 6. 什么是插入大量的记录在SQL Server 2008 R2
- 7. 什么是转换在SQL Server中插入的JSON对象的最佳方式
- 8. 在SQL Server中分区大表的最佳方法是什么?
- 9. 在SQL Server中截断日期的最佳方法是什么?
- 10. 将100000条记录插入SQL Server的最快方法
- 11. 什么是将60m记录导入SQL的最快方法
- 12. 将标记存储在sql server表中的最佳方法是什么?
- 13. 什么是记录方法调用的最佳方式?
- 14. 什么是插入日志/历史记录表中的记录在Django的最佳方法
- 15. 什么是从SQL Server批量提取记录的最佳方式
- 16. 将数据导入SQL Server Express的最佳方式是什么?
- 17. 将RegEx嵌入到Oracle或SQL Server 2005 SQL中的最佳方法是什么?
- 18. 在rethinkdb中插入python datetime的最佳方法是什么?
- 19. 什么是在VB.NET中更新或插入SQL记录的最佳(简单)方法
- 20. SQL Server 2008:什么是插入大块数据的最佳方式?
- 21. 在VB.NET中嵌入SQL的最佳方法是什么
- 22. SQL Server,每秒钟导入1M记录的最佳方式?
- 23. 什么是记录最好的方法?
- 24. 撤消记录更新的最佳方法是什么?
- 25. 用NLog集中记录日志的最佳方法是什么?
- 26. 什么是Hadoop MapReduce java类中的最佳记录方法?
- 27. 什么是制作活动记录器的最佳方法
- 28. SQL:如果不存在记录插入记录的最佳做法
- 29. 将SQL Server记录转换为MongoDB的JSON插入的最简单方法是什么?
- 30. 什么是在sql server中插入当前日期的最快方法