如何将数据表插入Microsoft Access? [英] How do I insert a datatable into Microsoft Access?

查看:76
本文介绍了如何将数据表插入Microsoft Access?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数据信息的数据表,我想通过插入查询将其放在Microsoft Access中的表中.有人可以帮助我吗?

I have a datatable with data information and I would like to put it in a table in microsoft access with insert query. Anyone can help me do that?

foreach ( DataRow row in table.Rows)
{
    insertCommand.CommandText = "INSERT INTO [tableName] VALUES (row[a], row[b], row[c])";
    insertCommand.ExecuteNonQuery();
}

推荐答案

您可以使用命令构建器.命令构建器将为您确定命令.

You can use a command builder. A command builder will determine commands for you.

您的数据适配器需要所有不同类型的命令,这些命令可在需要时调用.通过向命令构建器提供SELECT命令,它将确定UPDATE,DELETE和INSERT命令为您提供.

Your Data Adapter requires all of the different types of commands available to call on as and when required. By providing the command builder with the SELECT command, it will determine the UPDATE, DELETE & INSERT command for you.

所有您需要做的,当您尝试更新数据库时,它将DataTable对象传递给适配器.

All you need to do, it pass the DataTable object to the Adapter when you attempt to UPDATE your database.

using (OleDbConnection con = new OleDbConnection("YourConnectionString"))
{
    var adapter = new OleDbDataAdapter();
    adapter.SelectCommand = new OleDbCommand("SELECT * FROM [YourAccessTable]", con);

    var cbr = new OleDbCommandBuilder(adapter);

    cbr.GetDeleteCommand();
    cbr.GetInsertCommand();
    cbr.GetUpdateCommand();

    try
    {
        con.Open();
        adapter.Update(YourDataTable);
    }
    catch (OleDbException ex)
    {
        MessageBox.Show(ex.Message, "OledbException Error");
    }
    catch (Exception x)
    {
        MessageBox.Show(x.Message, "Exception Error");
    }
}

这篇关于如何将数据表插入Microsoft Access?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆