Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Text;
- using System.Threading;
- using System.IO;
- using System.Net;
- using System.Diagnostics;
- using System.Xml;
- using System.Xml.Serialization;
- using System.Configuration;
- using System.Globalization;
- using System.Drawing;
- using System.Data.SqlClient;
- using System.Data.Common;
- using System.Windows.Forms;
- namespace DatabaseHelper
- {
- public class CopyTableData
- {
- private string m_SourceConnectionString;
- private string m_DestinationConnectionString;
- private string m_SourceDataProviderType = "SQLSERVER";
- private string m_DestinationDataProviderType = "SQLSERVER";
- public string SourceConnectionString
- {
- get { return m_SourceConnectionString; }
- set { m_SourceConnectionString = value; }
- }
- public string DestinationConnectionString
- {
- get { return m_DestinationConnectionString; }
- set { m_DestinationConnectionString = value; }
- }
- public string SourceDataProviderType
- {
- get { return m_SourceDataProviderType; }
- set { m_SourceDataProviderType = value; }
- }
- public string DestinationDataProviderType
- {
- get { return m_DestinationDataProviderType; }
- set { m_DestinationDataProviderType = value; }
- }
- private IDbConnection GetOpenConnection(string connectionString,string providerType)
- {
- IDbConnection connection = null;
- providerType = "SQLSERVER";
- if (providerType == "SQLSERVER")
- {
- connection = new SqlConnection(connectionString);
- connection.Open();
- }
- if (connection == null)
- throw new Exception("Invalid DataProvider got " + providerType);
- return connection;
- }
- private IDbCommand GetCommand(string providerType)
- {
- if (providerType == "SQLSERVER")
- return new SqlCommand();
- throw new Exception("Invalid DataProvider got " + providerType);
- }
- public string[] GetTables(string filter)
- {
- List<string> list = new List<string>();
- SqlConnection cn = (SqlConnection)GetOpenConnection(m_SourceConnectionString,m_SourceDataProviderType);
- SqlCommand cmd = new SqlCommand("SELECT NAME FROM SYSOBJECTS WHERE TYPE = 'U' ORDER BY NAME",cn);
- SqlDataReader reader = cmd.ExecuteReader();
- while (reader.Read())
- {
- list.Add(reader.GetString(0));
- }
- return list.ToArray();
- }
- public int CopyData(string tableName, int batchSize)
- {
- try
- {
- IDbConnection sourceCn = GetOpenConnection(m_SourceConnectionString, m_SourceDataProviderType);
- IDbConnection destCn = GetOpenConnection(m_DestinationConnectionString, m_DestinationDataProviderType);
- IDbCommand sourceCmd = GetCommand(m_SourceDataProviderType);
- sourceCmd.Connection = (IDbConnection)sourceCn;
- sourceCmd.CommandText = "SELECT * FROM " + tableName;
- IDbCommand recCountCmd = GetCommand(m_SourceDataProviderType);
- recCountCmd.Connection = (IDbConnection)sourceCn;
- recCountCmd.CommandText = "SELECT COUNT(*) FROM " + tableName;
- int sourceRecordCount = Convert.ToInt32(recCountCmd.ExecuteScalar());
- IDbCommand destCmd = GetCommand(m_SourceDataProviderType);
- destCmd.Connection = (IDbConnection)destCn;
- destCmd.CommandText = "SELECT TOP 1* FROM " + tableName;
- DataSet destDataSet = new DataSet();
- SqlDataAdapter da = new SqlDataAdapter((SqlCommand)destCmd);
- da.Fill(destDataSet, tableName);
- DataTable destTable = destDataSet.Tables[0].Copy();
- IDataReader sourceReader = sourceCmd.ExecuteReader(CommandBehavior.Default);
- int recordCount = 0;
- SqlCommand destDelete = new SqlCommand("DELETE FROM " + tableName, (SqlConnection)destCn);
- destDelete.ExecuteNonQuery();
- int saveCount = 0;
- int batchCount = batchSize;
- if (sourceRecordCount < batchSize)
- batchCount = sourceRecordCount;
- //if (sourceRecordCount == 0)
- // Debugger.Break();
- while (sourceReader.Read())
- {
- if (Form1.CanceledByUser)
- return recordCount;
- recordCount++;
- saveCount++;
- DataRow destRow = destTable.NewRow();
- foreach (DataColumn sourceCol in destTable.Columns)
- {
- destRow[sourceCol.ColumnName] = sourceReader[sourceCol.ColumnName];
- }
- destTable.Rows.Add(destRow);
- if (saveCount >= batchCount || recordCount >= sourceRecordCount)
- {
- Application.DoEvents();
- if (tableName == "tblCompanyUser")
- Debugger.Break();
- SqlDataAdapter destInsert = new SqlDataAdapter((SqlCommand)destCmd);
- SqlCommandBuilder builder = new SqlCommandBuilder(destInsert);
- destInsert.InsertCommand = builder.GetInsertCommand();
- destInsert.UpdateBatchSize = batchCount;
- destInsert.Update(destTable);
- Form1.SetProgressText("Saved " + batchCount + " of " + recordCount);
- destTable = destDataSet.Tables[0].Copy();
- saveCount = 0;
- Application.DoEvents();
- }
- }
- return recordCount;
- }
- catch (Exception ex)
- {
- string mes = ex.Message;
- throw;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment