gpt4 book ai didi

c# - 数据网格中的更新值在带有外键的表中给出错误

转载 作者:太空宇宙 更新时间:2023-11-03 16:10:41 25 4
gpt4 key购买 nike

我正在尝试在我有 3 个表的 visual studio c# 中编写代码customer , product , order 三者是相关的

现在我已经创建了 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];
}




}
}

其次,我只能通过更新操作来更新订单表,其他两个表不会发生这种情况

素丹书

最佳答案

我最近遇到了同样的问题。尝试更改 Visual Studio 中“数据库”.xsd 文件中的外键。您必须在 .xsd 文件(实际数据库)中更新它们,然后在 .xsd 文件中重建更新查询,然后它才能工作。这很痛苦,但这是我不得不做很多次的事情。

关于c# - 数据网格中的更新值在带有外键的表中给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17580427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com