国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
SQL Bulk Copy with C#.NET

Introduction

Programmers usually need to transfer production data for testing or analyzing. The simplest way to copy lots of data from any resources to SQL Server is BulkCopying. .NET Framework 2.0 contains a class in ADO.NET "System.Data.SqlClient" namespace: SqlBulkCopy. The bulk copy operation usually has two separated phases.

In the first phase, you get the source data. The source could be various data platforms such as Access, Excel, SQL.. You must get the source data in your code wrapping it in a DataTable, or any DataReader class which implements IDataReader. After that, in the second phase, you must connect the target SQL database and perform the bulk copy operation.

The bulk copy operation in .NET is a very fast way to copy large amount of data somewhere to SQL Server. The reason for that is the Bulkcopy SQL Server mechanism. Inserting all data row by row, one after the other is a very time and system resources consuming. But the bulkcopy mechanism process all data at once. So the data inserting becomes very fast.

Solution Walkthrough

While you are programming for bulk copy, first open a connection for the source data. In this sample, we are connecting a SQL Server named SQLProduction. We are using SqlConnectionStringBuilder to build our connection string.

Hide   Copy Code
// Establishing connectionSqlConnectionStringBuilder cb = new SqlConnectionStringBuilder(); cb.DataSource = "SQLProduction"; cb.InitialCatalog = "Sales"; cb.IntegratedSecurity = true;SqlConnection cnn = new SqlConnection(cb.ConnectionString);  

Then we are retrieving data from the source with SqlCommand and SqlDataReader classes. 

Hide   Copy Code
// Getting source dataSqlCommand cmd = new SqlCommand("SELECT * FROM PendingOrders",cnn); cnn.Open(); SqlDataReader rdr = cmd.ExecuteReader(); 

Now we have a data in rdr variable. It's time to initialize a SqlBulkCopy object and copy the data. The SqlBulkCopy class needs a connection to copy data into a SQL server. You can establish a second connection explicitly or the class will do it for you. We are using the second alternative with creating a SqlBulkCopy object. We are passing a connection string as a parameter in constructor method.

Hide   Copy Code
// Initializing an SqlBulkCopy objectSqlBulkCopy sbc = new SqlBulkCopy("server=.;database=ProductionTest;" +                                  "Integrated Security=SSPI"); 

OK. The sbc object is ready to copy. Now you must tell the object the destination table name, start the copying process calling WriteToServer method and pass the method the SqlDataReader variable rdr as parameter.

Hide   Copy Code
// Copying data to destinationsbc.DestinationTableName = "Temp"; sbc.WriteToServer(rdr); 

At the end, close all SqlConnection, SqlDataReader and SqlBulkCopy objects.

Hide   Copy Code
// Closing connection and the otherssbc.Close(); rdr.Close(); cnn.Close(); 

That's all. Just a few lines and in a few seconds...

Hide   Copy Code
// Establishing connectionSqlConnectionStringBuilder cb = new SqlConnectionStringBuilder(); cb.DataSource = "SQLProduction"; cb.InitialCatalog = "Sales"; cb.IntegratedSecurity = true;SqlConnection cnn = new SqlConnection(cb.ConnectionString);  // Getting source dataSqlCommand cmd = new SqlCommand("SELECT * FROM PendingOrders",cnn); cnn.Open(); SqlDataReader rdr = cmd.ExecuteReader(); // Initializing an SqlBulkCopy objectSqlBulkCopy sbc = new SqlBulkCopy("server=.;database=ProductionTest;" +                                  "Integrated Security=SSPI"); // Copying data to destinationsbc.DestinationTableName = "Temp"; sbc.WriteToServer(rdr); // Closing connection and the otherssbc.Close(); rdr.Close(); cnn.Close(); 

History

  • 25th January, 2007: Initial version

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
C#進(jìn)階ADO.NET基礎(chǔ)一 基本概念、數(shù)據(jù)庫(kù)連接操作基礎(chǔ)
剖析SQL Server 2005查詢(xún)通知之基礎(chǔ)篇 - 戈氏的日志 - 網(wǎng)易博客
ADO.NET的記憶碎片(一)
使用ADO.NET下的SqlBulkCopy類(lèi)執(zhí)行批量復(fù)制操作
.net使用SqlDataReader
詳解如何挑戰(zhàn)4秒內(nèi)百萬(wàn)級(jí)數(shù)據(jù)導(dǎo)入SQL Server
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服