0

DataGrid からデータベースを更新するために使用したこのコードがあります

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace datagrid
{
public partial class Form1 : Form
{
    private MySqlConnection conn;
    private DataTable data;
    private MySqlDataAdapter da;
    private MySqlCommandBuilder cb;
    public Form1()
    {
        InitializeComponent();
    }

    private void btnshow_Click(object sender, EventArgs e)
    {
        string c = "server=localhost;database=std;uid=root;password=";
        conn = new MySqlConnection(c);
        conn.Open();
        data = new DataTable();

        da = new MySqlDataAdapter("SELECT * FROM general",conn);
        cb = new MySqlCommandBuilder(da);

        da.Fill(data);

        dataGridView1.DataSource = data;
    }

    private void btnupdate_Click(object sender, EventArgs e)
    {
        DataTable changes = data.GetChanges();
        da.Update(changes);
        data.AcceptChanges();
    }
 }
}

更新ボタンを押すと、次の例外が表示されます。

{"UpdateCommand の動的 SQL 生成は、キー列情報を返さない SelectCommand に対してはサポートされていません。"}

今、私は何をすべきか教えてください。

このような例外

4

2 に答える 2

0
    Try
        If con.State = ConnectionState.Open Then con.Close()
        con.Open()
        global_command = New SqlCommand("UPDATE products_tbl set running_no = '" & txt_running.Text & "' where template_code = 'n'and prod_no = '" & txt_product.Text & "'", con)
        global_command.ExecuteNonQuery()
        global_command.Dispose()

        MsgBox("Successfully updated!", MsgBoxStyle.Information, "Message")
        where = vbNullString

    Catch ex As Exception
        MsgBox("Trace No 4: System Error or Data Error!" + Chr(13) + ex.Message + Chr(13) + "Please Contact Your System Administrator!", vbInformation, "Message")
    End Try

    Try
        If con.State = ConnectionState.Open Then con.Close()
        con.Open()
        Dim dset As New DataSet
        Dim dbind As New BindingSource
        Dim strquery As String
        strquery = "SELECT prod_code, prod_no, prod_suffix, prod_lvl, customer, model, family, company_code, company_name, company_address, running_no, template_code FROM products_tbl where template_code = 'n' and prod_no = '" & txt_product.Text & "'  and prod_suffix = '" & txt_suffix.Text & "' and prod_lvl = '" & txt_level.Text & "'"
        Dim adap As New SqlDataAdapter(strquery, con)
        dset = New DataSet
        adap.Fill(dset)
        dbind.DataSource = dset.Tables(0)
        dg.DataSource = dbind
    Catch ex As Exception
        MsgBox("Trace No 3: System Error or Data Error!" + Chr(13) + ex.Message + Chr(13) + "Please Contact Your System Administrator!", vbInformation, "Message")
    End Try
End Sub
于 2014-01-09T07:47:43.433 に答える