2

私は、顧客、製品、注文の3つのテーブルがすべて関連しているビジュアルスタジオc#でコードを記述しようとしています

今、私はdatagridviewを作成し、データベースから値を取得しました.datagridを編集すると、データベースも更新されますが、製品の主キーと注文の外部キーであるProduct_idを更新しようとすると.

エラーが発生します

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 System.Data.SqlClient;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        SqlConnection conn;
        SqlDataAdapter da;
        DataSet ds;
        public Form1()
        {
            InitializeComponent();
            conn = new SqlConnection("user id=sa;" +
                                       "password=sa@123;server=sudhanshu-lappy;" +
                                       "Trusted_Connection=yes;" +
                                       "database=alpha; " +
                                       "connection timeout=30");
            try
            {
                conn.Open();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {





            //DataTable dt = null;

            //SqlCommand cmd = new SqlCommand("SELECT * FROM orders", conn);
            //SqlDataReader reader = cmd.ExecuteReader();

            //dt = new DataTable();

            //dt.Load(reader);

            //dataGridView1.DataSource = dt;


            //DataGridViewButtonColumn clbt_delet = new DataGridViewButtonColumn();
            //clbt_delet.HeaderText = "DELETE";

            //clbt_delet.Text = "Delete";
            //clbt_delet.UseColumnTextForButtonValue = true;
            //dataGridView1.Columns.Add(clbt_delet);


            //da = new SqlDataAdapter("select * from orders", conn);
            //SqlCommandBuilder cb = new SqlCommandBuilder(da);
            //ds = new DataSet();
            //da.Fill(ds);
            //dataGridView1.DataSource = ds.Tables[0];

            //now u can save changes to back end with


        }

        private void button1_Click(object sender, EventArgs e)
        {
            da.Update(ds);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            da.Update(ds);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            da.Update(ds);
        }

        private void ordersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from orders", conn);
            SqlCommandBuilder cb = new SqlCommandBuilder(da);
            ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void productToolStripMenuItem_Click(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from product", conn);
            SqlCommandBuilder cb1 = new SqlCommandBuilder(da);
            ds = new DataSet();
            da.Fill(ds);
            dataGridView3.DataSource = ds.Tables[0];
        }

        private void customerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from customer", conn);
            SqlCommandBuilder cb2 = new SqlCommandBuilder(da);
            ds = new DataSet();
            da.Fill(ds);
            dataGridView2.DataSource = ds.Tables[0];
        }




    }
}

次に、更新操作で注文テーブルを更新することしかできません。他の2つのテーブルでは発生しません

スーダンシュ

4

1 に答える 1