473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Creating an Access database from a SQL Database

RSH
Hi,

I have a situation where I have created a little application that makes an
Access database from a SQL Database for reporting purposes. it does the job
well, but it seems a bit slow. Is there anything that i can do to speed the
app up a bit?

namespace SQLToAccessBackup

{

class SqlToAccessConvertor

{

// RSH 3/23/2006

// This module creates an Access database from a SQL database

// It requires blank database (will not be destroyed) that has the

// same schema as it's SQL counterpart

// The word 'Blank' should be added to the Access Template databases name
Example: GlobalBlank.mdb

public SqlConnection cnSQL;

public OleDbConnection cnAccess;

public String strError = "";

public long lCounter;

public void RunDataInsert(String SourceServer, String Database, String
Template, String DataPath)

{
int iTotalRecords = 0;

String strDatabasePath = "";

String strSQL = "";

String dStartTime = "";

String dFinishTime = "";

dStartTime = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" +
DateTime.Now.Year + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute;

Console.WriteLine("Process Begun : " + dStartTime);
if (File.Exists(DataPath + Database + ".mdb"))

{

File.Copy(DataPath + Database + ".mdb", DataPath + Database + "-BAK-" +
DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year +
DateTime.Now.Hour + DateTime.Now.Minute + ".mdb");

File.Delete(DataPath + Database + ".mdb");

}

File.Copy(DataPath + Template + ".mdb", DataPath + Database + ".mdb");

strDatabasePath = DataPath + Database + ".mdb";

cnAccess = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data
Source=" + strDatabasePath + ";Persist Security Info=False");

cnAccess.Open();

cnSQL = new SqlConnection("Data Source=" + SourceServer + "; Integrated
Security=SSPI; Initial Catalog=" + Database);

cnSQL.Open();

// START LOOP OF ALL SQL TABLES HERE

strSQL = "USE [" + Database + "]";

SqlCommand myCommand = new SqlCommand(strSQL, cnSQL);

myCommand.ExecuteNonQuery();

strSQL = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES";

SqlDataAdapter daSQLTables = new SqlDataAdapter(strSQL, cnSQL);

DataSet dsSQLTables = new DataSet();

daSQLTables.Fill(dsSQLTables);

int iTotalTables = dsSQLTables.Tables[0].Rows.Count;

int iCurTableIdx = 0;

lCounter = 0;

string strCurTable = "";

Console.WriteLine("Processing Table: " + strCurTable + " Table " +
(iCurTableIdx + 1) + " of " + dsSQLTables.Tables[0].Rows.Count.ToString());

foreach (DataRow drSQLTables in dsSQLTables.Tables[0].Rows)

{

strCurTable = drSQLTables[0].ToString();

iCurTableIdx++;

// ACCESS

String strAccessSQL = "SELECT * FROM " + strCurTable;

OleDbDataAdapter daAccess = new OleDbDataAdapter(strAccessSQL, cnAccess);

DataSet dsAccess = new DataSet();

daAccess.Fill(dsAccess, "DT");

OleDbCommandBuilder cbDevelopment = new OleDbCommandBuilder(daAccess);

cbDevelopment.QuotePrefix = "[";

cbDevelopment.QuoteSuffix = "]";

// SQL

String strSQLServer = "SELECT * FROM [" + strCurTable + "] WITH (NOLOCK)";

SqlDataAdapter daSQLServer = new SqlDataAdapter(strSQLServer, cnSQL);

DataSet dsSQLServer = new DataSet();

daSQLServer.Fill(dsSQLServer);

int j = 1;

int maxRecords = dsSQLServer.Tables[0].Rows.Count;

foreach (DataRow dr in dsSQLServer.Tables[0].Rows)

{

DataRow drAccessAdd;

drAccessAdd = dsAccess.Tables["DT"].NewRow();

j++;
if (lCounter == 1000)

{

Console.WriteLine("Processing Table: " + strCurTable + "(" + (iCurTableIdx)
+ ")" + " Row " + (j - 2) + " of " + maxRecords);

lCounter = 0;

}

lCounter++;

for (Int16 i = 0; i < dr.ItemArray.Length; i++)

{

iTotalRecords++;

if (dsSQLServer.Tables[0].Columns[i].ColumnName.ToString() != "ID")

{

drAccessAdd[dsSQLServer.Tables[0].Columns[i].ColumnName.ToString()] =
dr[dsSQLServer.Tables[0].Columns[i].ColumnName.ToString()];

}

}

dsAccess.Tables["DT"].Rows.Add(drAccessAdd);

if (drAccessAdd.HasErrors == false)

{

daAccess.Update(dsAccess, "DT");

}

else

{

// Write to error log

strError = drAccessAdd.RowError.ToString() + "\\n";

}

}

}

dFinishTime = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" +
DateTime.Now.Year + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute;

MailMessage objMsg = new MailMessage();

objMsg.To = "rh******@timeplus.com";

objMsg.From = "rh******@timeplus.com";

objMsg.Subject = DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year +
" -- Sql to Access backup conversion job results.";

objMsg.Body = "The SQL to Access conversion job scheduled to run at: " +
dStartTime + " ran to completion at: " + dFinishTime + ".\n\n" +
String.Format("{0:###,###,###,###,###}", iTotalRecords - 1) + " Records were
written to: " + DataPath + Database + ".mdb \n\n The following errors
occurred during processing:\n" + strError;

SmtpMail.SmtpServer = "SMTPSERVER1";

SmtpMail.Send(objMsg);

cnAccess.Close();

cnAccess.Dispose();

cnSQL.Close();

cnSQL.Dispose();

if (strError.Length 1)

{

String strFilename = AppDomain.CurrentDomain.BaseDirectory +
"DataImportErrorLog-" + ".txt";

TextWriter tw = new StreamWriter(strFilename);

tw.WriteLine(strError);

tw.Close();

}

}

}

}

Thanks for any help you might be able to offer!

RSH
Jul 31 '06 #1
4 2384
Hi,

How much data are you talking about?

You could have an empty access created already and then just import the
data.
If you have a considerable amount of data then creating a DTS is a good
idea, you can run the DTS from the code without any problem
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"RSH" <wa*************@yahoo.comwrote in message
news:uY**************@TK2MSFTNGP05.phx.gbl...
Hi,

I have a situation where I have created a little application that makes an
Access database from a SQL Database for reporting purposes. it does the
job well, but it seems a bit slow. Is there anything that i can do to
speed the app up a bit?

namespace SQLToAccessBackup

{

class SqlToAccessConvertor

{

// RSH 3/23/2006

// This module creates an Access database from a SQL database

// It requires blank database (will not be destroyed) that has the

// same schema as it's SQL counterpart

// The word 'Blank' should be added to the Access Template databases name
Example: GlobalBlank.mdb

public SqlConnection cnSQL;

public OleDbConnection cnAccess;

public String strError = "";

public long lCounter;

public void RunDataInsert(String SourceServer, String Database, String
Template, String DataPath)

{
int iTotalRecords = 0;

String strDatabasePath = "";

String strSQL = "";

String dStartTime = "";

String dFinishTime = "";

dStartTime = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" +
DateTime.Now.Year + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute;

Console.WriteLine("Process Begun : " + dStartTime);
if (File.Exists(DataPath + Database + ".mdb"))

{

File.Copy(DataPath + Database + ".mdb", DataPath + Database + "-BAK-" +
DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year +
DateTime.Now.Hour + DateTime.Now.Minute + ".mdb");

File.Delete(DataPath + Database + ".mdb");

}

File.Copy(DataPath + Template + ".mdb", DataPath + Database + ".mdb");

strDatabasePath = DataPath + Database + ".mdb";

cnAccess = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data
Source=" + strDatabasePath + ";Persist Security Info=False");

cnAccess.Open();

cnSQL = new SqlConnection("Data Source=" + SourceServer + "; Integrated
Security=SSPI; Initial Catalog=" + Database);

cnSQL.Open();

// START LOOP OF ALL SQL TABLES HERE

strSQL = "USE [" + Database + "]";

SqlCommand myCommand = new SqlCommand(strSQL, cnSQL);

myCommand.ExecuteNonQuery();

strSQL = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES";

SqlDataAdapter daSQLTables = new SqlDataAdapter(strSQL, cnSQL);

DataSet dsSQLTables = new DataSet();

daSQLTables.Fill(dsSQLTables);

int iTotalTables = dsSQLTables.Tables[0].Rows.Count;

int iCurTableIdx = 0;

lCounter = 0;

string strCurTable = "";

Console.WriteLine("Processing Table: " + strCurTable + " Table " +
(iCurTableIdx + 1) + " of " +
dsSQLTables.Tables[0].Rows.Count.ToString());

foreach (DataRow drSQLTables in dsSQLTables.Tables[0].Rows)

{

strCurTable = drSQLTables[0].ToString();

iCurTableIdx++;

// ACCESS

String strAccessSQL = "SELECT * FROM " + strCurTable;

OleDbDataAdapter daAccess = new OleDbDataAdapter(strAccessSQL, cnAccess);

DataSet dsAccess = new DataSet();

daAccess.Fill(dsAccess, "DT");

OleDbCommandBuilder cbDevelopment = new OleDbCommandBuilder(daAccess);

cbDevelopment.QuotePrefix = "[";

cbDevelopment.QuoteSuffix = "]";

// SQL

String strSQLServer = "SELECT * FROM [" + strCurTable + "] WITH (NOLOCK)";

SqlDataAdapter daSQLServer = new SqlDataAdapter(strSQLServer, cnSQL);

DataSet dsSQLServer = new DataSet();

daSQLServer.Fill(dsSQLServer);

int j = 1;

int maxRecords = dsSQLServer.Tables[0].Rows.Count;

foreach (DataRow dr in dsSQLServer.Tables[0].Rows)

{

DataRow drAccessAdd;

drAccessAdd = dsAccess.Tables["DT"].NewRow();

j++;
if (lCounter == 1000)

{

Console.WriteLine("Processing Table: " + strCurTable + "(" +
(iCurTableIdx) + ")" + " Row " + (j - 2) + " of " + maxRecords);

lCounter = 0;

}

lCounter++;

for (Int16 i = 0; i < dr.ItemArray.Length; i++)

{

iTotalRecords++;

if (dsSQLServer.Tables[0].Columns[i].ColumnName.ToString() != "ID")

{

drAccessAdd[dsSQLServer.Tables[0].Columns[i].ColumnName.ToString()] =
dr[dsSQLServer.Tables[0].Columns[i].ColumnName.ToString()];

}

}

dsAccess.Tables["DT"].Rows.Add(drAccessAdd);

if (drAccessAdd.HasErrors == false)

{

daAccess.Update(dsAccess, "DT");

}

else

{

// Write to error log

strError = drAccessAdd.RowError.ToString() + "\\n";

}

}

}

dFinishTime = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" +
DateTime.Now.Year + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute;

MailMessage objMsg = new MailMessage();

objMsg.To = "rh******@timeplus.com";

objMsg.From = "rh******@timeplus.com";

objMsg.Subject = DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year
+ " -- Sql to Access backup conversion job results.";

objMsg.Body = "The SQL to Access conversion job scheduled to run at: " +
dStartTime + " ran to completion at: " + dFinishTime + ".\n\n" +
String.Format("{0:###,###,###,###,###}", iTotalRecords - 1) + " Records
were written to: " + DataPath + Database + ".mdb \n\n The following errors
occurred during processing:\n" + strError;

SmtpMail.SmtpServer = "SMTPSERVER1";

SmtpMail.Send(objMsg);

cnAccess.Close();

cnAccess.Dispose();

cnSQL.Close();

cnSQL.Dispose();

if (strError.Length 1)

{

String strFilename = AppDomain.CurrentDomain.BaseDirectory +
"DataImportErrorLog-" + ".txt";

TextWriter tw = new StreamWriter(strFilename);

tw.WriteLine(strError);

tw.Close();

}

}

}

}

Thanks for any help you might be able to offer!

RSH


Jul 31 '06 #2

Isn't there a way to ...

Insert Into (mdb)Customers Select * from (sqlserver) Customers

?

I used to do this in DAO....

I think your code is doing a row by row. Which may be where the slowness is
coming from. (aka, the default dataAdapter behavior)

Sorry I don't know more, but I ran into this issue back in 1998, and using
DAO was able to wholesale copy the 3 or 4 tables I needed.



"RSH" <wa*************@yahoo.comwrote in message
news:uY**************@TK2MSFTNGP05.phx.gbl...
Hi,

I have a situation where I have created a little application that makes an
Access database from a SQL Database for reporting purposes. it does the
job
well, but it seems a bit slow. Is there anything that i can do to speed
the
app up a bit?

namespace SQLToAccessBackup

{

class SqlToAccessConvertor

{

// RSH 3/23/2006

// This module creates an Access database from a SQL database

// It requires blank database (will not be destroyed) that has the

// same schema as it's SQL counterpart

// The word 'Blank' should be added to the Access Template databases name
Example: GlobalBlank.mdb

public SqlConnection cnSQL;

public OleDbConnection cnAccess;

public String strError = "";

public long lCounter;

public void RunDataInsert(String SourceServer, String Database, String
Template, String DataPath)

{
int iTotalRecords = 0;

String strDatabasePath = "";

String strSQL = "";

String dStartTime = "";

String dFinishTime = "";

dStartTime = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" +
DateTime.Now.Year + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute;

Console.WriteLine("Process Begun : " + dStartTime);
if (File.Exists(DataPath + Database + ".mdb"))

{

File.Copy(DataPath + Database + ".mdb", DataPath + Database + "-BAK-" +
DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year +
DateTime.Now.Hour + DateTime.Now.Minute + ".mdb");

File.Delete(DataPath + Database + ".mdb");

}

File.Copy(DataPath + Template + ".mdb", DataPath + Database + ".mdb");

strDatabasePath = DataPath + Database + ".mdb";

cnAccess = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data
Source=" + strDatabasePath + ";Persist Security Info=False");

cnAccess.Open();

cnSQL = new SqlConnection("Data Source=" + SourceServer + "; Integrated
Security=SSPI; Initial Catalog=" + Database);

cnSQL.Open();

// START LOOP OF ALL SQL TABLES HERE

strSQL = "USE [" + Database + "]";

SqlCommand myCommand = new SqlCommand(strSQL, cnSQL);

myCommand.ExecuteNonQuery();

strSQL = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES";

SqlDataAdapter daSQLTables = new SqlDataAdapter(strSQL, cnSQL);

DataSet dsSQLTables = new DataSet();

daSQLTables.Fill(dsSQLTables);

int iTotalTables = dsSQLTables.Tables[0].Rows.Count;

int iCurTableIdx = 0;

lCounter = 0;

string strCurTable = "";

Console.WriteLine("Processing Table: " + strCurTable + " Table " +
(iCurTableIdx + 1) + " of " +
dsSQLTables.Tables[0].Rows.Count.ToString());
>


foreach (DataRow drSQLTables in dsSQLTables.Tables[0].Rows)

{

strCurTable = drSQLTables[0].ToString();

iCurTableIdx++;

// ACCESS

String strAccessSQL = "SELECT * FROM " + strCurTable;

OleDbDataAdapter daAccess = new OleDbDataAdapter(strAccessSQL, cnAccess);

DataSet dsAccess = new DataSet();

daAccess.Fill(dsAccess, "DT");

OleDbCommandBuilder cbDevelopment = new OleDbCommandBuilder(daAccess);

cbDevelopment.QuotePrefix = "[";

cbDevelopment.QuoteSuffix = "]";

// SQL

String strSQLServer = "SELECT * FROM [" + strCurTable + "] WITH (NOLOCK)";

SqlDataAdapter daSQLServer = new SqlDataAdapter(strSQLServer, cnSQL);

DataSet dsSQLServer = new DataSet();

daSQLServer.Fill(dsSQLServer);

int j = 1;

int maxRecords = dsSQLServer.Tables[0].Rows.Count;

foreach (DataRow dr in dsSQLServer.Tables[0].Rows)

{

DataRow drAccessAdd;

drAccessAdd = dsAccess.Tables["DT"].NewRow();

j++;
if (lCounter == 1000)

{

Console.WriteLine("Processing Table: " + strCurTable + "(" +
(iCurTableIdx)
+ ")" + " Row " + (j - 2) + " of " + maxRecords);

lCounter = 0;

}

lCounter++;

for (Int16 i = 0; i < dr.ItemArray.Length; i++)

{

iTotalRecords++;

if (dsSQLServer.Tables[0].Columns[i].ColumnName.ToString() != "ID")

{

drAccessAdd[dsSQLServer.Tables[0].Columns[i].ColumnName.ToString()] =
dr[dsSQLServer.Tables[0].Columns[i].ColumnName.ToString()];

}

}

dsAccess.Tables["DT"].Rows.Add(drAccessAdd);

if (drAccessAdd.HasErrors == false)

{

daAccess.Update(dsAccess, "DT");

}

else

{

// Write to error log

strError = drAccessAdd.RowError.ToString() + "\\n";

}

}

}

dFinishTime = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" +
DateTime.Now.Year + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute;

MailMessage objMsg = new MailMessage();

objMsg.To = "rh******@timeplus.com";

objMsg.From = "rh******@timeplus.com";

objMsg.Subject = DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year
+
" -- Sql to Access backup conversion job results.";

objMsg.Body = "The SQL to Access conversion job scheduled to run at: " +
dStartTime + " ran to completion at: " + dFinishTime + ".\n\n" +
String.Format("{0:###,###,###,###,###}", iTotalRecords - 1) + " Records
were
written to: " + DataPath + Database + ".mdb \n\n The following errors
occurred during processing:\n" + strError;

SmtpMail.SmtpServer = "SMTPSERVER1";

SmtpMail.Send(objMsg);

cnAccess.Close();

cnAccess.Dispose();

cnSQL.Close();

cnSQL.Dispose();

if (strError.Length 1)

{

String strFilename = AppDomain.CurrentDomain.BaseDirectory +
"DataImportErrorLog-" + ".txt";

TextWriter tw = new StreamWriter(strFilename);

tw.WriteLine(strError);

tw.Close();

}

}

}

}

Thanks for any help you might be able to offer!

RSH


Jul 31 '06 #3
RSH
It is a significant amount of data (roughly 150 mb).
"RSH" <wa*************@yahoo.comwrote in message
news:uY**************@TK2MSFTNGP05.phx.gbl...
Hi,

I have a situation where I have created a little application that makes an
Access database from a SQL Database for reporting purposes. it does the
job well, but it seems a bit slow. Is there anything that i can do to
speed the app up a bit?

namespace SQLToAccessBackup

{

class SqlToAccessConvertor

{

// RSH 3/23/2006

// This module creates an Access database from a SQL database

// It requires blank database (will not be destroyed) that has the

// same schema as it's SQL counterpart

// The word 'Blank' should be added to the Access Template databases name
Example: GlobalBlank.mdb

public SqlConnection cnSQL;

public OleDbConnection cnAccess;

public String strError = "";

public long lCounter;

public void RunDataInsert(String SourceServer, String Database, String
Template, String DataPath)

{
int iTotalRecords = 0;

String strDatabasePath = "";

String strSQL = "";

String dStartTime = "";

String dFinishTime = "";

dStartTime = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" +
DateTime.Now.Year + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute;

Console.WriteLine("Process Begun : " + dStartTime);
if (File.Exists(DataPath + Database + ".mdb"))

{

File.Copy(DataPath + Database + ".mdb", DataPath + Database + "-BAK-" +
DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year +
DateTime.Now.Hour + DateTime.Now.Minute + ".mdb");

File.Delete(DataPath + Database + ".mdb");

}

File.Copy(DataPath + Template + ".mdb", DataPath + Database + ".mdb");

strDatabasePath = DataPath + Database + ".mdb";

cnAccess = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data
Source=" + strDatabasePath + ";Persist Security Info=False");

cnAccess.Open();

cnSQL = new SqlConnection("Data Source=" + SourceServer + "; Integrated
Security=SSPI; Initial Catalog=" + Database);

cnSQL.Open();

// START LOOP OF ALL SQL TABLES HERE

strSQL = "USE [" + Database + "]";

SqlCommand myCommand = new SqlCommand(strSQL, cnSQL);

myCommand.ExecuteNonQuery();

strSQL = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES";

SqlDataAdapter daSQLTables = new SqlDataAdapter(strSQL, cnSQL);

DataSet dsSQLTables = new DataSet();

daSQLTables.Fill(dsSQLTables);

int iTotalTables = dsSQLTables.Tables[0].Rows.Count;

int iCurTableIdx = 0;

lCounter = 0;

string strCurTable = "";

Console.WriteLine("Processing Table: " + strCurTable + " Table " +
(iCurTableIdx + 1) + " of " +
dsSQLTables.Tables[0].Rows.Count.ToString());

foreach (DataRow drSQLTables in dsSQLTables.Tables[0].Rows)

{

strCurTable = drSQLTables[0].ToString();

iCurTableIdx++;

// ACCESS

String strAccessSQL = "SELECT * FROM " + strCurTable;

OleDbDataAdapter daAccess = new OleDbDataAdapter(strAccessSQL, cnAccess);

DataSet dsAccess = new DataSet();

daAccess.Fill(dsAccess, "DT");

OleDbCommandBuilder cbDevelopment = new OleDbCommandBuilder(daAccess);

cbDevelopment.QuotePrefix = "[";

cbDevelopment.QuoteSuffix = "]";

// SQL

String strSQLServer = "SELECT * FROM [" + strCurTable + "] WITH (NOLOCK)";

SqlDataAdapter daSQLServer = new SqlDataAdapter(strSQLServer, cnSQL);

DataSet dsSQLServer = new DataSet();

daSQLServer.Fill(dsSQLServer);

int j = 1;

int maxRecords = dsSQLServer.Tables[0].Rows.Count;

foreach (DataRow dr in dsSQLServer.Tables[0].Rows)

{

DataRow drAccessAdd;

drAccessAdd = dsAccess.Tables["DT"].NewRow();

j++;
if (lCounter == 1000)

{

Console.WriteLine("Processing Table: " + strCurTable + "(" +
(iCurTableIdx) + ")" + " Row " + (j - 2) + " of " + maxRecords);

lCounter = 0;

}

lCounter++;

for (Int16 i = 0; i < dr.ItemArray.Length; i++)

{

iTotalRecords++;

if (dsSQLServer.Tables[0].Columns[i].ColumnName.ToString() != "ID")

{

drAccessAdd[dsSQLServer.Tables[0].Columns[i].ColumnName.ToString()] =
dr[dsSQLServer.Tables[0].Columns[i].ColumnName.ToString()];

}

}

dsAccess.Tables["DT"].Rows.Add(drAccessAdd);

if (drAccessAdd.HasErrors == false)

{

daAccess.Update(dsAccess, "DT");

}

else

{

// Write to error log

strError = drAccessAdd.RowError.ToString() + "\\n";

}

}

}

dFinishTime = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" +
DateTime.Now.Year + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute;

MailMessage objMsg = new MailMessage();

objMsg.To = "rh******@timeplus.com";

objMsg.From = "rh******@timeplus.com";

objMsg.Subject = DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year
+ " -- Sql to Access backup conversion job results.";

objMsg.Body = "The SQL to Access conversion job scheduled to run at: " +
dStartTime + " ran to completion at: " + dFinishTime + ".\n\n" +
String.Format("{0:###,###,###,###,###}", iTotalRecords - 1) + " Records
were written to: " + DataPath + Database + ".mdb \n\n The following errors
occurred during processing:\n" + strError;

SmtpMail.SmtpServer = "SMTPSERVER1";

SmtpMail.Send(objMsg);

cnAccess.Close();

cnAccess.Dispose();

cnSQL.Close();

cnSQL.Dispose();

if (strError.Length 1)

{

String strFilename = AppDomain.CurrentDomain.BaseDirectory +
"DataImportErrorLog-" + ".txt";

TextWriter tw = new StreamWriter(strFilename);

tw.WriteLine(strError);

tw.Close();

}

}

}

}

Thanks for any help you might be able to offer!

RSH


Jul 31 '06 #4
Hi,

Use DTS packages

"RSH" <wa*************@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
It is a significant amount of data (roughly 150 mb).
"RSH" <wa*************@yahoo.comwrote in message
news:uY**************@TK2MSFTNGP05.phx.gbl...
>Hi,

I have a situation where I have created a little application that makes
an Access database from a SQL Database for reporting purposes. it does
the job well, but it seems a bit slow. Is there anything that i can do
to speed the app up a bit?

namespace SQLToAccessBackup

{

class SqlToAccessConvertor

{

// RSH 3/23/2006

// This module creates an Access database from a SQL database

// It requires blank database (will not be destroyed) that has the

// same schema as it's SQL counterpart

// The word 'Blank' should be added to the Access Template databases name
Example: GlobalBlank.mdb

public SqlConnection cnSQL;

public OleDbConnection cnAccess;

public String strError = "";

public long lCounter;

public void RunDataInsert(String SourceServer, String Database, String
Template, String DataPath)

{
int iTotalRecords = 0;

String strDatabasePath = "";

String strSQL = "";

String dStartTime = "";

String dFinishTime = "";

dStartTime = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" +
DateTime.Now.Year + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute;

Console.WriteLine("Process Begun : " + dStartTime);
if (File.Exists(DataPath + Database + ".mdb"))

{

File.Copy(DataPath + Database + ".mdb", DataPath + Database + "-BAK-" +
DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year +
DateTime.Now.Hour + DateTime.Now.Minute + ".mdb");

File.Delete(DataPath + Database + ".mdb");

}

File.Copy(DataPath + Template + ".mdb", DataPath + Database + ".mdb");

strDatabasePath = DataPath + Database + ".mdb";

cnAccess = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + strDatabasePath + ";Persist Security Info=False");

cnAccess.Open();

cnSQL = new SqlConnection("Data Source=" + SourceServer + "; Integrated
Security=SSPI; Initial Catalog=" + Database);

cnSQL.Open();

// START LOOP OF ALL SQL TABLES HERE

strSQL = "USE [" + Database + "]";

SqlCommand myCommand = new SqlCommand(strSQL, cnSQL);

myCommand.ExecuteNonQuery();

strSQL = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES";

SqlDataAdapter daSQLTables = new SqlDataAdapter(strSQL, cnSQL);

DataSet dsSQLTables = new DataSet();

daSQLTables.Fill(dsSQLTables);

int iTotalTables = dsSQLTables.Tables[0].Rows.Count;

int iCurTableIdx = 0;

lCounter = 0;

string strCurTable = "";

Console.WriteLine("Processing Table: " + strCurTable + " Table " +
(iCurTableIdx + 1) + " of " +
dsSQLTables.Tables[0].Rows.Count.ToString());

foreach (DataRow drSQLTables in dsSQLTables.Tables[0].Rows)

{

strCurTable = drSQLTables[0].ToString();

iCurTableIdx++;

// ACCESS

String strAccessSQL = "SELECT * FROM " + strCurTable;

OleDbDataAdapter daAccess = new OleDbDataAdapter(strAccessSQL, cnAccess);

DataSet dsAccess = new DataSet();

daAccess.Fill(dsAccess, "DT");

OleDbCommandBuilder cbDevelopment = new OleDbCommandBuilder(daAccess);

cbDevelopment.QuotePrefix = "[";

cbDevelopment.QuoteSuffix = "]";

// SQL

String strSQLServer = "SELECT * FROM [" + strCurTable + "] WITH
(NOLOCK)";

SqlDataAdapter daSQLServer = new SqlDataAdapter(strSQLServer, cnSQL);

DataSet dsSQLServer = new DataSet();

daSQLServer.Fill(dsSQLServer);

int j = 1;

int maxRecords = dsSQLServer.Tables[0].Rows.Count;

foreach (DataRow dr in dsSQLServer.Tables[0].Rows)

{

DataRow drAccessAdd;

drAccessAdd = dsAccess.Tables["DT"].NewRow();

j++;
if (lCounter == 1000)

{

Console.WriteLine("Processing Table: " + strCurTable + "(" +
(iCurTableIdx) + ")" + " Row " + (j - 2) + " of " + maxRecords);

lCounter = 0;

}

lCounter++;

for (Int16 i = 0; i < dr.ItemArray.Length; i++)

{

iTotalRecords++;

if (dsSQLServer.Tables[0].Columns[i].ColumnName.ToString() != "ID")

{

drAccessAdd[dsSQLServer.Tables[0].Columns[i].ColumnName.ToString()] =
dr[dsSQLServer.Tables[0].Columns[i].ColumnName.ToString()];

}

}

dsAccess.Tables["DT"].Rows.Add(drAccessAdd);

if (drAccessAdd.HasErrors == false)

{

daAccess.Update(dsAccess, "DT");

}

else

{

// Write to error log

strError = drAccessAdd.RowError.ToString() + "\\n";

}

}

}

dFinishTime = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" +
DateTime.Now.Year + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute;

MailMessage objMsg = new MailMessage();

objMsg.To = "rh******@timeplus.com";

objMsg.From = "rh******@timeplus.com";

objMsg.Subject = DateTime.Now.Month + DateTime.Now.Day +
DateTime.Now.Year + " -- Sql to Access backup conversion job results.";

objMsg.Body = "The SQL to Access conversion job scheduled to run at: " +
dStartTime + " ran to completion at: " + dFinishTime + ".\n\n" +
String.Format("{0:###,###,###,###,###}", iTotalRecords - 1) + " Records
were written to: " + DataPath + Database + ".mdb \n\n The following
errors occurred during processing:\n" + strError;

SmtpMail.SmtpServer = "SMTPSERVER1";

SmtpMail.Send(objMsg);

cnAccess.Close();

cnAccess.Dispose();

cnSQL.Close();

cnSQL.Dispose();

if (strError.Length 1)

{

String strFilename = AppDomain.CurrentDomain.BaseDirectory +
"DataImportErrorLog-" + ".txt";

TextWriter tw = new StreamWriter(strFilename);

tw.WriteLine(strError);

tw.Close();

}

}

}

}

Thanks for any help you might be able to offer!

RSH



Aug 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dim St Thomas | last post by:
I am a developer working on a database client program. I am testing this program on a Windows XP machine (1.5 GHz AMD chip, 480 Mb RAM, 60 Gb disk) This machine has Oracle 9.2.0.1.0 and RedBrick...
4
by: Tony | last post by:
G'day My attempt to create an MDE file was unsuccessful. The attempt produced an error that effectively said the (MDB) file was to big. The MDB consists of about 30 tables, 50 forms and 3...
7
by: John Baker | last post by:
Hi: I would like to know how to create a temp DB to store the data in a table while I do something else with the table. Specifically, how do I create the temp remove the temp I want to be...
6
by: Jerry Spence1 | last post by:
Why doesn't the following work in my ASP program? I have imported ADOX I am trying to create a temporary database on the user's PC. The example is taken from Microsoft. Dim cat As Catalog =...
2
by: John Dann | last post by:
Apologies if this isn't the best group - maybe someone can suggest a more appropriate one - I can't immediately see anything on ado.net. I need to store a significant amount of data (say 1-10MB)...
9
by: dennist685 | last post by:
Walkthrough: Creating a Web Page to Display Access Database Data I've started the above walkthrough. However, near the beginning it gives the following instructions To set permissions in the...
1
by: dave | last post by:
Hello: In Visual Studio 6.0 there is "VisData" to create Access database files. (file.MDB". Q: Does Visual Studio 2005 have tool(s) that create Access database files (file.MDB) ? thank...
2
by: Andy | last post by:
Hi guys I having a problem creating a report in Access 2003 project talking to a SQL database through and ODBC connect. After hours of trying things from Access Help, MSDN and Google I still...
1
by: sainiamit25 | last post by:
Hi, I want to create a materlised view log in my database A and materlised view in some other database B. I was successful in creating materlised view in database B (after creating a dblink with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.