Najjaf Shuja

Najjaf Shuja

  • NA
  • 31
  • 1.1k

Timeout expired.

Jun 4 2015 4:19 AM
Hello Guys,
 I am developing DataMigration Utility in which I am migrating Data from one database to another by writing simple queries using SQLCommand, Datatable, ExecuteReader etc.
 
I am using TransactionScope which I set timeout for 1 hour and 30 seconds.
 
using (TransactionScope trans = new TransactionScope(TransactionScopeOption.Required, new System.TimeSpan(1, 30, 0)))
Now I am copying data from one table which has 16051721 rows. I am getting data in chunks and inserting it through ExecuteNonQuery().
 
Problem is after sometime it is throwing exception,
 
 Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated.
 
Sample code is below,
 
foreach (string id in idList)
query = "SELECT * FROM table1 where fk_id = " + id;
myCommand = new SqlCommand(query, source);

dt = null;
dt = new DataTable();

dt.Load(myCommand.ExecuteReader());
qExpre = string.Empty;

if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{

qExpre += " insert into destinationTable
values('" + row["fk_id"] + "', 'A', '" + row["ID"] + "', '" + Guid.NewGuid() + "', " + row["distance"] + ", " + row["unit"] + ", " + row["name"] + ", 'Migrated Data', '" + DateTime.Now + "')";
}

if (!string.IsNullOrEmpty(qExpre))
{
myCommand = new SqlCommand(qExpre, destination);
myCommand.ExecuteNonQuery();
}
}
}
 

Answers (6)