简体   繁体   English

为什么我的适配器更新不起作用

[英]Why my update with adapter doesn't work

On form close I call update but I got error message: update requires InsertCommand whereas I inspired from this tut http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database在表单关闭时我调用更新但我收到错误消息:更新需要 InsertCommand 而我的灵感来自这个 tut http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace datatablemsaccess
{
    partial class Form1
    {
        OleDbDataAdapter dataAdapter;

        private string getSQL() {
            string sql = "SELECT * from tPerson";
            return sql;
        }

        private string getConnectionString() {

            string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @"App_Data\person.mdb";
            return connectionString;
        }

        private DataTable getDataTable(string sql)
        {
            DataTable dataTable;

            string connectionString = getConnectionString();

            using (dataAdapter = new OleDbDataAdapter(sql, connectionString))
            {
                dataTable = new DataTable();
                dataAdapter.Fill(dataTable);
            }

            return dataTable;
        }
    }
}



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;


namespace datatablemsaccess
{
    public partial class Form1 : Form
    {
        BindingSource bindingSource;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string sql = getSQL();

            bindingSource = new BindingSource();

            bindingSource.DataSource = getDataTable(sql);
            dataGridView1.DataSource = bindingSource;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            dataAdapter.Update((DataTable)bindingSource.DataSource);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM