0

Im copying data from a ms access db to sql server like this...

string sSQLTable = table;
string myExcelDataQuery = "Select * from " + sSQLTable;
string sSqlConnectionString = connStr;
string sClearSQL = "DELETE FROM " + sSQLTable;
SqlConnection SqlConn = new SqlConnection(sSqlConnectionString);
SqlCommand SqlCmd = new SqlCommand(sClearSQL, SqlConn);
SqlConn.Open();
SqlCmd.ExecuteNonQuery();
SqlConn.Close();
OleDbConnection OleDbConn = new OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", fileName));
OleDbCommand OleDbCmd = new OleDbCommand(myExcelDataQuery, OleDbConn);
OleDbConn.Open();
OleDbDataReader dr = OleDbCmd.ExecuteReader();
SqlBulkCopy bulkCopy = new SqlBulkCopy(sSqlConnectionString);
bulkCopy.DestinationTableName = sSQLTable;
while (dr.Read())
{
    bulkCopy.WriteToServer(dr);
}
OleDbConn.Close();

But this only does it for one table name...how do i put this in a loop and get each table name and call this function to copy data for each table?

2 Answers 2

1

You may take a look at the following article which illustrates how to retrieve a list of available tables by querying the schema.

Sign up to request clarification or add additional context in comments.

Comments

0

You can run below query but need to have Administrative priviledge.

SELECT Name FROM MSysObjects WHERE Left([Name],1)<>"~" AND Left([Name],4)<>"MSys" AND Type In (1,4,6) ORDER BY Name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.