Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的适配器更新没有';行不通_C#_Ms Access_Ado.net - Fatal编程技术网

C# 为什么我的适配器更新没有';行不通

C# 为什么我的适配器更新没有';行不通,c#,ms-access,ado.net,C#,Ms Access,Ado.net,在表单关闭时,我调用update,但收到错误消息:update需要InsertCommand,而我的灵感来自于这个tut 您需要定义InsertCommand 请参见错误消息是自我解释的。。。您没有定义insertcommand。添加类似于dataAdapter.InsertCommand=connection.CreateCommand()…的内容,或者,我的建议是,使用Visual STudio向导生成85%的数据访问代码我只有一个没有数据集的数据表,我不希望向导生成完整的数据集。由于有点习

在表单关闭时,我调用update,但收到错误消息:update需要InsertCommand,而我的灵感来自于这个tut


您需要定义InsertCommand


请参见

错误消息是自我解释的。。。您没有定义insertcommand。添加类似于
dataAdapter.InsertCommand=connection.CreateCommand()…
的内容,或者,我的建议是,使用Visual STudio向导生成85%的数据访问代码我只有一个没有数据集的数据表,我不希望向导生成完整的数据集。由于有点习惯,它已经过度使用了,您可以使用设计器并仅生成所需的内容。
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);
        }
    }
}