Moving data from Access to SQL Server using ADO.NET

Generally it is required to copy data from access to sql server. we can do it using sql server services. But here it is performed using .NET.


static void TransferTableData(
String AccessSourcedb,
String DestinationConnectionStr,
String TableName
)
{
//Connection to the destination
SqlConnection DestConnection = new SqlConnection(DestinationConnectionStr);
DestConnection.Open();
SqlCommand Cmd = DestConnection.CreateCommand();
//Connection to source
OleDbConnection OledbSrcConnection = new
System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + AccessSourcedb);
OledbSrcConnection.Open();
// Reader to source
OleDbCommand OleDbCommand = new OleDbCommand("SELECT * FROM " +
TableName);
OleDbCommand.Connection = OledbSrcConnection;
OleDbDataReader OleDbDataReader = OleDbCommand.ExecuteReader();
//bulk upload to destination
SqlBulkCopy bulkCopy = new SqlBulkCopy(DestConnection,
System.Data.SqlClient.SqlBulkCopyOptions.KeepIdentity, null);
bulkCopy.BulkCopyTimeout = 100000000;
bulkCopy.DestinationTableName = TableName;
bulkCopy.WriteToServer(OleDbDataReader);
bulkCopy.Close();
}

No comments:

Post a Comment