SQL Server 2005 使用 SqlBulkCopy 和 TransactionScope 一例

SqlBulkCopy 类就像名字所描述的一样是专门用于批量复制的。相对于其他将数据加载到SQL Server表中的方式,使用SqlBulCopy具有明显的性能优势。这个类只能用于向SQL Server表中写入数据。但是,可以使用任何数据源,主要数据可以加载到DataTable或使用DataReader实例读取即可。

下面是一个演示的例子,关于SqlBulkCopy注意NotifyAfter属性的含义, 这个属性设定的是Copy多少行后触发SqlRowsCopied  事件,事件的加载就不多说了。

这个例子
同样使用了专门处理分布式事务的TransactionScope的类,TransactionScope是ADO.net 2.0中提供了一个轻量级的事务容器,可以允许我们方便的创建分布式事务, 用户根本不需要考虑是简单事务还是分布式事务. TransactionScope会自动根据事务中涉及的对象资源判断使用何种事务管理器.。

以下例子来源于邹建的书,不过我用C#改写了。

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Transactions;
None.gif
using  System.Data;
None.gif
None.gif
namespace  ConsoleApplication2
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private static string m_ConnStr = "Data Source=tantan\\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=True";
InBlock.gif
InBlock.gif        
public static void InitTest(SqlConnection sConn)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (SqlCommand iCmd = new SqlCommand())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                iCmd.Connection 
= sConn;
InBlock.gif                iCmd.CommandText 
= "SELECT TOP 0 * INTO dbo.tb_bulk_test_objects FROM sys.objects";
InBlock.gif                iCmd.ExecuteNonQuery();
InBlock.gif
InBlock.gif                iCmd.CommandText 
= "SELECT TOP 0 * INTO dbo.tb_bulk_test_columns FROM sys.columns";
InBlock.gif                iCmd.ExecuteNonQuery();
InBlock.gif            
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void OnsqlRowsCopied(object sender, SqlRowsCopiedEventArgs args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(String.Format(
"Copied {0} so fardot.gif.", args.RowsCopied.ToString()));       
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private static void Bulk(SqlConnection sConn, string sSourceSQL, string sDestinationTable)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (SqlCommand iCmd = new SqlCommand(sSourceSQL,sConn))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (SqlBulkCopy iBcp = new SqlBulkCopy(sConn))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    iBcp.NotifyAfter 
= 1000;
InBlock.gif                    iBcp.DestinationTableName 
= sDestinationTable;
InBlock.gif                    iBcp.SqlRowsCopied 
+= OnsqlRowsCopied;
InBlock.gif                    
using (DataTable iDataTable = new DataTable())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        iDataTable.Load(iCmd.ExecuteReader());
InBlock.gif                        iBcp.WriteToServer(iDataTable);                    
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    iBcp.SqlRowsCopied 
-= OnsqlRowsCopied;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void BulkTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (TransactionScope iTs = new TransactionScope())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif               
InBlock.gif                
using (SqlConnection iConn=new SqlConnection (m_ConnStr))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    iConn.Open();
InBlock.gif                    InitTest(iConn);
InBlock.gif                    DateTime iDate;
InBlock.gif                    
string iSQL;
InBlock.gif                    iDate 
= DateTime.Now;
InBlock.gif                    Console .WriteLine (
string.Format ("{0} start 1 batch processing",iDate));
InBlock.gif                    iSQL 
= "SELECT o.* FROM sys.objects o, sys.columns c";
InBlock.gif                    Bulk(iConn, iSQL, 
"dbo.tb_bulk_test_objects");
InBlock.gif                    Console.WriteLine(
string.Format("{0} finish 2 batch processing", iDate));
InBlock.gif                    
InBlock.gif                    iDate 
= DateTime.Now;
InBlock.gif                    Console.WriteLine(
string.Format("{0} start 2 batch processing", iDate));
InBlock.gif                    iSQL 
= "SELECT c.* FROM sys.objects o,sys.columns c";
InBlock.gif                    Bulk(iConn, iSQL, 
"dbo.tb_bulk_test_columns");
InBlock.gif                    Console.WriteLine(
string.Format("{0} finish 2 batch processing", iDate));
InBlock.gif                
ExpandedSubBlockEnd.gif                }

InBlock.gif                iTs.Dispose();
InBlock.gif            
ExpandedSubBlockEnd.gif            }
       
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private 
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            BulkTest();
InBlock.gif            Console.WriteLine(
"Done");
InBlock.gif            Console.ReadKey(); 
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif 
None.gif

转载于:https://www.cnblogs.com/waitrabbit/archive/2008/05/08/1188613.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值