开发者

Transfering data from SQL to Access using C#

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].Ite开发者_JAVA百科mArray[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.


  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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜