在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中插入當前日期的最快方法