简体   繁体   English

更新语句与外键冲突

[英]Update statement conflicted with foreign key

Here is my tables,i can update ad,soyad and email but when i try to update telefon it conflicts with the foreign key [userFk],i already have foreign key update on cascade so,i can't figure out the issue here.Thanks in advance 这是我的表格,我可以更新广告,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

); );

and the second one, 第二个

CREATE TABLE [dbo].[loginusers] (
[username]  NVARCHAR (50) NOT NULL,
[upassword] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([upassword] ASC)

); );

and here is the update button, 这是更新按钮,

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);
        }
    }

As upassword column is primarykey in your loginusers table, if you want to update telefon on ogrenci with update cascade property , so you need to remove ,telefon=@telefonVal code from update ogrenci query, like this 由于upassword列是你的PrimaryKey loginusers表,如果要更新telefonogrenciupdate cascade property ,所以你需要删除,telefon=@telefonVal从代码update ogrenci查询,像这样

string query = "UPDATE ogrenci SET ogrenciNo=@ogrenciNoVal,ad=@adVal,soyad=@soyadVal,email=@emailVal WHERE ogrenciNo=@ogrenciNoVal";

Your second query will update table ogrenci too 您的second query也会更新表ogrenci

WARNING: It will not good if some student will think like this - "If my password is my telephone, so lets try login as another student with his/her telephone number as password and do something" :) 警告:如果某些学生会这样想,那就不好了-“如果我的密码是我的电话,那么让我们尝试用另一个学生的登录名并用他/她的电话号码作为密码,然后执行一些操作” :)

EDIT: 编辑:

Your second query where clause is wrong I think, 我认为您的second query where clause错误,

string query1 = "UPDATE loginusers SET username=@emailVal,upassword=@telefonVal WHERE username=@telefonVal";

It should change to this 它应该变成这个

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM