andrew4582

Copy Table Data

Aug 15th, 2010
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Text;
  7. using System.Threading;
  8. using System.IO;
  9. using System.Net;
  10. using System.Diagnostics;
  11. using System.Xml;
  12. using System.Xml.Serialization;
  13. using System.Configuration;
  14. using System.Globalization;
  15. using System.Drawing;
  16. using System.Data.SqlClient;
  17. using System.Data.Common;
  18. using System.Windows.Forms;
  19.  
  20. namespace DatabaseHelper
  21. {
  22.     public class CopyTableData
  23.     {
  24.         private string m_SourceConnectionString;
  25.         private string m_DestinationConnectionString;
  26.         private string m_SourceDataProviderType = "SQLSERVER";
  27.         private string m_DestinationDataProviderType = "SQLSERVER";
  28.      
  29.  
  30.  
  31.  
  32.         public string SourceConnectionString
  33.         {
  34.             get { return m_SourceConnectionString; }
  35.             set { m_SourceConnectionString = value; }
  36.         }
  37.  
  38.         public string DestinationConnectionString
  39.         {
  40.             get { return m_DestinationConnectionString; }
  41.             set { m_DestinationConnectionString = value; }
  42.         }
  43.  
  44.         public string SourceDataProviderType
  45.         {
  46.             get { return m_SourceDataProviderType; }
  47.             set { m_SourceDataProviderType = value; }
  48.         }
  49.  
  50.         public string DestinationDataProviderType
  51.         {
  52.             get { return m_DestinationDataProviderType; }
  53.             set { m_DestinationDataProviderType = value; }
  54.         }
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.         private IDbConnection GetOpenConnection(string connectionString,string providerType)
  62.         {
  63.             IDbConnection connection = null;
  64.  
  65.             providerType = "SQLSERVER";
  66.             if (providerType == "SQLSERVER")
  67.             {
  68.                 connection = new SqlConnection(connectionString);
  69.                 connection.Open();
  70.             }
  71.             if (connection == null)
  72.                 throw new Exception("Invalid DataProvider got " + providerType);
  73.             return connection;
  74.         }
  75.         private IDbCommand GetCommand(string providerType)
  76.         {
  77.             if (providerType == "SQLSERVER")
  78.                 return new SqlCommand();
  79.             throw new Exception("Invalid DataProvider got " + providerType);
  80.         }
  81.         public string[] GetTables(string filter)
  82.         {
  83.             List<string> list = new List<string>();
  84.  
  85.             SqlConnection cn = (SqlConnection)GetOpenConnection(m_SourceConnectionString,m_SourceDataProviderType);
  86.             SqlCommand cmd = new SqlCommand("SELECT NAME FROM SYSOBJECTS WHERE TYPE = 'U' ORDER BY NAME",cn);
  87.             SqlDataReader reader = cmd.ExecuteReader();
  88.             while (reader.Read())
  89.             {
  90.                 list.Add(reader.GetString(0));
  91.             }
  92.             return list.ToArray();
  93.         }
  94.         public int CopyData(string tableName, int batchSize)
  95.         {
  96.             try
  97.             {
  98.                 IDbConnection sourceCn = GetOpenConnection(m_SourceConnectionString, m_SourceDataProviderType);
  99.                 IDbConnection destCn = GetOpenConnection(m_DestinationConnectionString, m_DestinationDataProviderType);
  100.  
  101.                 IDbCommand sourceCmd = GetCommand(m_SourceDataProviderType);
  102.                 sourceCmd.Connection = (IDbConnection)sourceCn;
  103.                 sourceCmd.CommandText = "SELECT * FROM " + tableName;
  104.  
  105.                 IDbCommand recCountCmd = GetCommand(m_SourceDataProviderType);
  106.                 recCountCmd.Connection = (IDbConnection)sourceCn;
  107.                 recCountCmd.CommandText = "SELECT COUNT(*) FROM " + tableName;
  108.  
  109.                 int sourceRecordCount = Convert.ToInt32(recCountCmd.ExecuteScalar());
  110.  
  111.  
  112.                 IDbCommand destCmd = GetCommand(m_SourceDataProviderType);
  113.                 destCmd.Connection = (IDbConnection)destCn;
  114.                 destCmd.CommandText = "SELECT TOP 1* FROM " + tableName;
  115.  
  116.                 DataSet destDataSet = new DataSet();
  117.  
  118.                 SqlDataAdapter da = new SqlDataAdapter((SqlCommand)destCmd);
  119.  
  120.                 da.Fill(destDataSet, tableName);
  121.  
  122.  
  123.                 DataTable destTable = destDataSet.Tables[0].Copy();
  124.                 IDataReader sourceReader = sourceCmd.ExecuteReader(CommandBehavior.Default);
  125.  
  126.                 int recordCount = 0;
  127.                 SqlCommand destDelete = new SqlCommand("DELETE FROM " + tableName, (SqlConnection)destCn);
  128.                 destDelete.ExecuteNonQuery();
  129.                 int saveCount = 0;
  130.                 int batchCount = batchSize;
  131.                 if (sourceRecordCount < batchSize)
  132.                     batchCount = sourceRecordCount;
  133.                 //if (sourceRecordCount == 0)
  134.                 //    Debugger.Break();
  135.  
  136.                 while (sourceReader.Read())
  137.                 {
  138.                     if (Form1.CanceledByUser)
  139.                         return recordCount;
  140.  
  141.                     recordCount++;
  142.                     saveCount++;
  143.  
  144.                     DataRow destRow = destTable.NewRow();
  145.                     foreach (DataColumn sourceCol in destTable.Columns)
  146.                     {
  147.                         destRow[sourceCol.ColumnName] = sourceReader[sourceCol.ColumnName];
  148.                     }
  149.                     destTable.Rows.Add(destRow);
  150.  
  151.                     if (saveCount >= batchCount || recordCount >= sourceRecordCount)
  152.                     {
  153.                         Application.DoEvents();
  154.                         if (tableName == "tblCompanyUser")
  155.                             Debugger.Break();
  156.  
  157.                         SqlDataAdapter destInsert = new SqlDataAdapter((SqlCommand)destCmd);
  158.                         SqlCommandBuilder builder = new SqlCommandBuilder(destInsert);
  159.                         destInsert.InsertCommand = builder.GetInsertCommand();
  160.                         destInsert.UpdateBatchSize = batchCount;
  161.                         destInsert.Update(destTable);
  162.                         Form1.SetProgressText("Saved " + batchCount + " of " + recordCount);
  163.                         destTable = destDataSet.Tables[0].Copy();
  164.                         saveCount = 0;
  165.                         Application.DoEvents();
  166.                     }
  167.  
  168.                 }
  169.  
  170.                 return recordCount;
  171.             }
  172.             catch (Exception ex)
  173.             {
  174.                 string mes = ex.Message;
  175.                 throw;
  176.             }
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment