Hello everyone,
I have two databases called DB1 and DB2 on the same database server. I have Table1 in DB2 and Table2 in DB2. Currently, I am using insert into select * to transfer all data from Table2 into Table1 (Table1 is empty, my purpose is to make a copy of data from Table2 to Table1). The table structure is clustered ID column (of type GUID) and an XML binary (varbinary) data column.
My current issue is, the memory consumption is very high. Are there any good ideas to reduce the memory consumption? My rough idea is I can initialize a couple of small transactions and selct insert partial data from each transaction.
I am using VSTS 2008 + C# + ADO.Net + SQL Server 2008 Enterprise. Any good solutions or reference samples?
Here is my current code which causes out of memory exception. I am using ADO.Net SQLBulkCopy feature.
namespace BulkCopyTable
{
public class CopyData
{
string _sourceConnectionString;
string _destinationConnectionString;
public CopyData(string sourceConnectionString,
string destinationConnectionString)
{
_sourceConnectionString =
sourceConnectionString;
_destinationConnectionString =
destinationConnectionString;
}
public void CopyTable(string table)
{
using (SqlConnection source =
new SqlConnection(_sourceConnectionString))
{
string sql = string.Format("SELECT * FROM [{0}]", table);
SqlCommand command = new SqlCommand(sql, source);
source.Open();
IDataReader dr = command.ExecuteReader();
using (SqlBulkCopy copy =
new SqlBulkCopy(_destinationConnectionString))
{
copy.DestinationTableName = table;
copy.WriteToServer(dr);
}
}
}
}
class Program
{
static void Main(string[] args)
{
CopyData copier = new CopyData(ConfigurationSettings.AppSettings["source"], ConfigurationSettings.AppSettings["destination"]);
Console.WriteLine("Begin Copy");
copier.CopyTable(ConfigurationSettings.AppSettings["Table"]);
Console.WriteLine("End Copy");
return;
}
}
}
thanks in advance, George