[英]Update statement conflicted with foreign key
这是我的表格,我可以更新广告,soyad和电子邮件,但是当我尝试更新telefon时,它与外键[userFk]冲突,我已经在级联上进行了外键更新,因此,我无法在此处解决问题。提前致谢
CREATE TABLE [dbo].[ogrenci] (
[ogrenciNo] INT NOT NULL,
[ad] NVARCHAR (20) NOT NULL,
[soyad] NVARCHAR (20) NOT NULL,
[email] NVARCHAR (50) NOT NULL,
[fakulte_no] INT NOT NULL,
[bolum_ad] NVARCHAR (30) NOT NULL,
[bolum_no] INT DEFAULT ((1)) NOT NULL,
[telefon] NVARCHAR (50) DEFAULT ((1)) NOT NULL,
PRIMARY KEY CLUSTERED ([ogrenciNo] ASC),
UNIQUE NONCLUSTERED ([ogrenciNo] ASC),
UNIQUE NONCLUSTERED ([email] ASC),
CONSTRAINT [bolumFk] FOREIGN KEY ([bolum_no]) REFERENCES [dbo].[bolum] ([bolumNo]) ON DELETE CASCADE,
CONSTRAINT [fakulteFk1] FOREIGN KEY ([fakulte_no]) REFERENCES [dbo].[fakulte] ([fakulteId]) ON DELETE CASCADE,
CONSTRAINT [userFk] FOREIGN KEY ([telefon]) REFERENCES [dbo].[loginusers] ([upassword]) ON DELETE CASCADE ON UPDATE CASCADE
);
第二个
CREATE TABLE [dbo].[loginusers] (
[username] NVARCHAR (50) NOT NULL,
[upassword] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([upassword] ASC)
);
这是更新按钮,
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
if (txtOgrenciNo.Text.Length != 0 && txtAd.Text.Length != 0 && txtSoyad.Text.Length != 0 && txtEmail.Text.Length != 0 && txtTelefon.Text.Length != 0)
{
string query = "UPDATE ogrenci SET ogrenciNo=@ogrenciNoVal,ad=@adVal,soyad=@soyadVal,email=@emailVal,telefon=@telefonVal WHERE ogrenciNo=@ogrenciNoVal";
string query1 = "UPDATE loginusers SET username=@emailVal,upassword=@telefonVal WHERE username=@telefonVal";
using (connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
using (SqlCommand cmd = new SqlCommand(query1, connection))
{
connection.Open();
command.Parameters.AddWithValue("@ogrenciNoVal", txtOgrenciNo.Text);
command.Parameters.AddWithValue("@adVal", txtAd.Text);
command.Parameters.AddWithValue("@soyadVal", txtSoyad.Text);
command.Parameters.AddWithValue("@emailVal", txtEmail.Text);
command.Parameters.AddWithValue("@telefonVal", txtTelefon.Text);
cmd.Parameters.AddWithValue("@emailVal", txtEmail.Text);
cmd.Parameters.AddWithValue("@telefonVal", txtTelefon.Text);
command.ExecuteNonQuery();
cmd.ExecuteNonQuery();
gridDoldur();
}
}
else
{
MessageBox.Show("Öğrenci bilgileri boş girilemez.", "Bilgilendirme", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
由于upassword
列是你的PrimaryKey loginusers
表,如果要更新telefon
上ogrenci
与update cascade property
,所以你需要删除,telefon=@telefonVal
从代码update ogrenci
查询,像这样
string query = "UPDATE ogrenci SET ogrenciNo=@ogrenciNoVal,ad=@adVal,soyad=@soyadVal,email=@emailVal WHERE ogrenciNo=@ogrenciNoVal";
您的second query
也会更新表ogrenci
警告:如果某些学生会这样想,那就不好了-“如果我的密码是我的电话,那么让我们尝试用另一个学生的登录名并用他/她的电话号码作为密码,然后执行一些操作” :)
编辑:
我认为您的second query
where clause
错误,
string query1 = "UPDATE loginusers SET username=@emailVal,upassword=@telefonVal WHERE username=@telefonVal";
它应该变成这个
string query1 = "UPDATE loginusers SET username=@emailVal,upassword=@telefonVal WHERE upassword=@oldtelefonVal ";
要解决您的问题,您需要在loginusers中插入行(而不是update),之后需要更新表ogrenci,最后可以取消您的loginusers row。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.