SqlBulkCopy 使用案例

using DDTek.Oracle;
using System.Data.Common;
using System.Data.SqlClient;


        private static bool DownLoadEmail()
        {
            OracleConnection conMiddle = null;
            SqlConnection connSQL = null;
            SqlCommand commSQL = null;

            try
            {
                conMiddle = new OracleConnection("Host=192.168.**.**;Port=1521;User ID=****;Password=****;SID=orcl;Pooling=False");
                OracleCommand cmdMiddle = conMiddle.CreateCommand();
                cmdMiddle.CommandType = CommandType.Text;
                if (conMiddle.State == ConnectionState.Closed) //打开oracle
                {
                    conMiddle.Open();
                }
                //查询资料是否存在
                cmdMiddle.CommandText = @"select ID,COMPANY,EMAIL,NAME,OPUSER,OPDATE from email";
                OracleDataAdapter sda = new OracleDataAdapter(cmdMiddle);
                DataTable dtEmail = new DataTable();
                sda.Fill(dtEmail);
                if (dtEmail != null && dtEmail.Rows.Count > 0)
                {
                    connSQL = new SqlConnection("Data Source=192.168.**.***;Database=****;User=**;Password=****;Application Name=ws");
                    if (connSQL.State == ConnectionState.Closed)//打开sqlserver
                        connSQL.Open();

                    commSQL = new SqlCommand();
                    commSQL.Connection = connSQL;

                    commSQL.CommandText = "delete from email";//刪除表,如果不刪除,会报错“主键重复”
                    commSQL.ExecuteNonQuery();

                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connSQL))
                    {
                        //插入数据的表名
                        bulkCopy.DestinationTableName = "email";
                        //插入的资料
                        bulkCopy.WriteToServer(dtEmail);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                if (conMiddle.State == ConnectionState.Open)
                {
                    conMiddle.Close();
                    conMiddle.Dispose();
                }
                if (connSQL.State == ConnectionState.Open)
                {
                    connSQL.Close();
                    connSQL.Dispose();
                }
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面的控制台应用程序演示如何使用 SqlBulkCopy 类加载数据。在此示例中,使用 SqlDataReader 将数据从 SQL Server 2005 AdventureWorks 数据库中的 Production.Product 表复制到同一数据库中的相似表。 重要说明: 在您按照批量复制示例设置 (ADO.NET) 中的描述创建工作表之后,此示例才会运行。提供此代码仅为了演示使用 SqlBulkCopy 的语法。如果源表和目标表都在同一个 SQL Server 实例中,则使用 Transact-SQL INSERT … SELECT 语句复制数据会更方便快捷。 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;"; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值