Home >Backend Development >C++ >How Can I Achieve Fast Bulk Inserts into an Access Database Using .NET/C#?

How Can I Achieve Fast Bulk Inserts into an Access Database Using .NET/C#?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-03 22:02:40775browse

How Can I Achieve Fast Bulk Inserts into an Access Database Using .NET/C#?

Writing Large Number of Records (Bulk Insert) to Access in .NET/C#

Problem: Inserting a large dataset into an Access database using ADO.NET is time-consuming.

Solution:

Using DAO offers significant speed advantages over ADO.NET for bulk inserts into Access:

1. DAO Using Field Variables (2.8 Seconds):

DAO.Field[] myFields = new DAO.Field[20];
for (int k = 0; k < 20; k++) myFields[k] = rs.Fields["Field" + (k + 1).ToString()];

for (int i = 0; i < 100000; i++)
{
    rs.AddNew();
    for (int k = 0; k < 20; k++)
    {
        myFields[k].Value = i + k;
    }
    rs.Update();
}

2. DAO Using Column Index (11.0 Seconds):

for (int i = 0; i < 100000; i++)
{
    rs.AddNew();
    for (int k = 0; k < 20; k++)
    {
        rs.Fields[k].Value = i + k;
    }
    rs.Update();
}

3. DAO Using Column Name (17.0 Seconds):

for (int i = 0; i < 100000; i++)
{
    rs.AddNew();
    for (int k = 0; k < 20; k++)
    {
        rs.Fields["Field" + (k + 1).ToString()].Value = i + k;
    }
    rs.Update();
}

4. ADO.NET Using INSERT Statements (79.0 Seconds):

for (int i = 0; i < 100000; i++)
{
    StringBuilder insertSQL = new StringBuilder("INSERT INTO TEMP (")
        .Append(names)
        .Append(") VALUES (");

    for (int k = 0; k < 19; k++)
    {
        insertSQL.Append(i + k).Append(",");
    }
    insertSQL.Append(i + 19).Append(")");
    cmd.CommandText = insertSQL.ToString();
    cmd.ExecuteNonQuery();
}

5. ADO.NET Using DataTable and DataAdapter (86.0 Seconds):

for (int i = 0; i < 100000; i++)
{
    DataRow dr = dt.NewRow();
    for (int k = 0; k < 20; k++)
    {
        dr["Field" + (k + 1).ToString()] = i + k;
    }
    dt.Rows.Add(dr);
}
da.Update(dt);

6. Text File with Automation Import (2.8 Seconds):

Export data to a text file and use Access Automation to import it (not recommended for large datasets):

StreamWriter sw = new StreamWriter(Properties.Settings.Default.TEMPPathLocation);

for (int i = 0; i < 100000; i++)
{
    for (int k = 0; k < 19; k++)
    {
        sw.Write(i + k);
        sw.Write(",");
    }
    sw.WriteLine(i + 19);
}
sw.Close();

ACCESS.Application accApplication = new ACCESS.Application();
accApplication.OpenCurrentDatabase(databaseName, false, "");
accApplication.DoCmd.RunSQL("DELETE FROM TEMP");
accApplication.DoCmd.TransferText(TransferType: ACCESS.AcTextTransferType.acImportDelim,
    TableName: "TEMP",
    FileName: Properties.Settings.Default.TEMPPathLocation,
    HasFieldNames: true);
accApplication.CloseCurrentDatabase();
accApplication.Quit();
accApplication = null;

The above is the detailed content of How Can I Achieve Fast Bulk Inserts into an Access Database Using .NET/C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn