0
using System.Data.OleDb;

namespace ConnectingToMS_Access1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        OleDbDataAdapter da;
        DataSet ds;

        private void Form1_Load(object sender, EventArgs e)
        {
            da = new OleDbDataAdapter("Select * from Record", "Provider=Microsoft.Ace.oledb.12.0;data source='D:\\mydb.accdb'");
            ds = new DataSet();
            da.Fill(ds);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataRow dr = ds.Tables[0].NewRow();
            dr[0] = "abc"; 
            dr[1] = "def";
            dr[2] = "ghi";
            dr[3] = "jkl";
            ds.Tables[0].Rows.Add(dr);
        }

        private void button3_Click(object sender, EventArgs e)
        {
             OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
             da.Update(ds.Tables[0]);

             MessageBox.Show("Saved in Database");


        }
    }
}
4

1 回答 1

0

您需要在查询中明确定义命名列。

问题是您无法根据列位置更新列。期望调用 Fill 方法将返回记录和列名可能是合理的,但它可能不会。毕竟,运行该选择查询将只返回数据,而不是模式。

于 2012-07-22T03:39:16.710 回答