在c#中将数据从dataGridView更新/编辑到ms访问数据库

时间:2013-11-17 01:11:41

标签: c# sql ms-access datagridview

我有一个问题,我试图解决几个小时。它早上2点,甚至在读完文章的音调后我仍然无法解决问题。 我有c#windows app将数据从ms访问数据库加载到dataGridView中。 我可以成功加载它,添加新记录并保存。但是,当我尝试编辑/删除行然后保存它时,我得到错误: 对于不返回任何键列信息的SelectCommand,不支持DeleteCommand的动态SQL生成。

我不知道该怎么做。我真的很感激一些帮助!

public class UserInterfaceForm: Form
{
    //db connection
    String connString;
    OleDbDataAdapter dAdapter;
    OleDbCommandBuilder cBuilder;
    DataTable dTable = new DataTable();
    BindingSource bSource;

    //buttons
    private Button SavePermitButton;
    private Button loadPermitButton;
    private Button exitButton;
    private Button deletePermitButton;

    //GridView
    private DataGridView allPermitsDataGridView;

    public UserInterfaceForm()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        this.SavePermitButton = new System.Windows.Forms.Button();
        this.loadPermitButton = new System.Windows.Forms.Button();
        this.allPermitsDataGridView = new System.Windows.Forms.DataGridView();
        this.exitButton = new System.Windows.Forms.Button();
        this.deletePermitButton = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.allPermitsDataGridView)).BeginInit();
        this.SuspendLayout();
        // 
        // SavePermitButton
        // 
        this.SavePermitButton.Location = new System.Drawing.Point(115, 275);
        this.SavePermitButton.Name = "SavePermitButton";
        this.SavePermitButton.Size = new System.Drawing.Size(97, 25);
        this.SavePermitButton.TabIndex = 3;
        this.SavePermitButton.Text = "Save permit";
        this.SavePermitButton.UseVisualStyleBackColor = true;
        this.SavePermitButton.Click += new System.EventHandler(this.SavePermitButton_Click);
        // 
        // loadPermitButton
        // 
        this.loadPermitButton.Location = new System.Drawing.Point(12, 275);
        this.loadPermitButton.Name = "loadPermitButton";
        this.loadPermitButton.Size = new System.Drawing.Size(97, 25);
        this.loadPermitButton.TabIndex = 4;
        this.loadPermitButton.Text = "Load permit";
        this.loadPermitButton.UseVisualStyleBackColor = true;
        this.loadPermitButton.Click += new System.EventHandler(this.readPermitButton_Click);
        // 
        // allPermitsDataGridView
        // 
        this.allPermitsDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.allPermitsDataGridView.Location = new System.Drawing.Point(12, 12);
        this.allPermitsDataGridView.Name = "allPermitsDataGridView";
        this.allPermitsDataGridView.Size = new System.Drawing.Size(839, 239);
        this.allPermitsDataGridView.TabIndex = 8;
        // 
        // exitButton
        // 
        this.exitButton.Location = new System.Drawing.Point(752, 275);
        this.exitButton.Name = "exitButton";
        this.exitButton.Size = new System.Drawing.Size(99, 25);
        this.exitButton.TabIndex = 0;
        this.exitButton.Text = "Exit";
        this.exitButton.Click += new System.EventHandler(this.cancelButton_click);
        // 
        // deletePermitButton
        // 
        this.deletePermitButton.Location = new System.Drawing.Point(218, 275);
        this.deletePermitButton.Name = "deletePermitButton";
        this.deletePermitButton.Size = new System.Drawing.Size(97, 25);
        this.deletePermitButton.TabIndex = 9;
        this.deletePermitButton.Text = "Delete permit";
        this.deletePermitButton.UseVisualStyleBackColor = true;
        this.deletePermitButton.Click += new System.EventHandler(this.deletePermitButton_Click);
        // 
        // UserInterfaceForm
        // 
        this.ClientSize = new System.Drawing.Size(886, 312);
        this.Controls.Add(this.deletePermitButton);
        this.Controls.Add(this.allPermitsDataGridView);
        this.Controls.Add(this.loadPermitButton);
        this.Controls.Add(this.SavePermitButton);
        this.Controls.Add(this.exitButton);
        this.Name = "UserInterfaceForm";
        this.Text = "Parking Permit Application";
        ((System.ComponentModel.ISupportInitialize)(this.allPermitsDataGridView)).EndInit();
        this.ResumeLayout(false);
    }

    protected void cancelButton_click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void readPermitButton_Click(object sender, EventArgs e)
    {
        connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Arek\Desktop\Parking Permit Application - Assignment 3\Assignment_3_OOPARDGM.accdb; 
        Persist Security Info=False;";
        dAdapter = new OleDbDataAdapter("Select * From Permits", connString);
        cBuilder = new OleDbCommandBuilder(dAdapter);

        dAdapter.Fill(dTable);

        bSource = new BindingSource();            
        bSource.DataSource = dTable;            
        allPermitsDataGridView.DataSource = bSource;
    }

    private void SavePermitButton_Click(object sender, EventArgs e)
    {
        dAdapter.Update(dTable);
        MessageBox.Show("Database successfully saved!");
    }

    private void deletePermitButton_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow item in this.allPermitsDataGridView.SelectedRows)
        {
            allPermitsDataGridView.Rows.RemoveAt(item.Index);
        } 

    }       
}//class

1 个答案:

答案 0 :(得分:1)

您的Permits表看起来没有关键字段