1

For my current project in c# I need to transfer data from a SQL-Database to an Access-Database. For now I load the data into a DataSet using a SqlDataAdapter. After that I loop through the entries and insert them into the Access-DB using OleDb:

// Load data from SQL
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter("select goes here", sqlConnection);
adapter.Fill(ds);

// Prepare the Insert Command
oleDBCommand = "Insert into...";
oleDBCommand.Parameters.Add(new OleDbParameter(...));

// Insert every row from the DataSet
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
    // Update Parameters and Execute
    oleDBCommand.Parameters[0].Value = ds.Tables[0].Rows[i].ItemArray[0];
    oleDBCommand.ExecuteNonQuery();
}

This approach works fine, however it feels clumsy and slow. So I was wondering if there is another better way to transfer data from one DB to another.

4
  • 2
    You're going from SQL to Access (which isn't supported anymore)... it's maybe a good thing that it feels clumsy and slow ;) Commented May 5, 2011 at 7:20
  • What do you mean that it feels clumsy? Commented May 5, 2011 at 7:31
  • It feels as if there has to be a better solution than just loop through every row and call an insert Statement :s Commented May 5, 2011 at 7:56
  • @Nathan: what does the parenthetical "isn't supported anymore" mean? Access is certainly supported, as is interoperation with SQL Server. Commented May 6, 2011 at 2:18

2 Answers 2

1
  1. Use SqlDataReader: SqlDataReader runs faster than SqlDataAdapter
  2. Use Transaction: use a transaction and bind each command to this transaction. after finishing the insert commands, commit the transaction, which may run faster.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It was helpful and I'll go with it as an answer, although it wasn't exactly what I was looking for...
0

Another idea:
If it's always the same Access database and you import your data always from the same table(s), you can link the SQL Server tables in Access once.

If a SQL Server table is linked, you can use it in the MDB just like a local table.
Then, you can just insert directly from the linked table into the local table by running this query via OleDB in the Access database:

insert into LocalAccessTable (Column1, Column2)
select Column1, Column2
from LinkedSqlServerTable

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.