外键错误添加新记录时

时间:2014-01-21 20:15:23

标签: c# mysql sql visual-studio-2012

我正在制作健身房管理系统,我在桌子上使用外键。一切都工作得很好,但在我添加外键后,我收到以下错误:

SQLException was Unhandled
The INSERT statement conflicted with the FOREIGN KEY constraint 
"FK_member_info_instructor_info". The conflict occurred in database "D:\GYM MANAGEMENT 
SYSTEM\GYM MANAGEMENT SYSTEM\BIN\DEBUG\GMSDATABASE.MDF", table "dbo.instructor_info", 
column 'InstructorID'.

The statement has been terminated.

此错误指向此代码:

public void UpdateDatabase(System.Data.DataSet ds)
{
    System.Data.SqlClient.SqlCommandBuilder cb = new 
             System.Data.SqlClient.SqlCommandBuilder(da_1);
    cb.DataAdapter.Update(ds.Tables[0]);
}

上面的代码在我的数据库集合类中,如下所示:

class GMSDConnectionClass
{
    System.Data.SqlClient.SqlDataAdapter da_1;
    System.Data.SqlClient.SqlConnection con;

    public string sql_string;
    public string strCon;

    public string Sql
    {
        set { sql_string = value; }
    }

    public string connection_string
    {
        set { strCon = value; }
    }

    public System.Data.DataSet GetConnection
    {
        get { return MyDataSet(); }
    }

    public System.Data.DataSet MyDataSet()
    {
        con = new System.Data.SqlClient.SqlConnection(strCon);
        con.Open();
        da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
        System.Data.DataSet dat_set = new System.Data.DataSet();
        da_1.Fill(dat_set, "Table_data_1");
        con.Close();
        return dat_set;
    }

    public void UpdateDatabase(System.Data.DataSet ds)
    {
        System.Data.SqlClient.SqlCommandBuilder cb = new 
         System.Data.SqlClient.SqlCommandBuilder(da_1);
        cb.DataAdapter.Update(ds.Tables[0]);
    }
}

这些是在抛出错误时使用的表:

instructor_info表:

CREATE TABLE [dbo].[instructor_info] (
[InstructorID]      INT            IDENTITY (1, 1) NOT NULL,
[instructor]        NVARCHAR (50)  NOT NULL,
[father_name]       NVARCHAR (50)  NULL,
[age]               NCHAR (10)     NULL,
[address]           NVARCHAR (MAX) NULL,
[contact]           NVARCHAR (50)  NULL,
[height]            NCHAR (10)     NULL,
[weight]            NCHAR (10)     NULL,
[chest]             NCHAR (10)     NULL,
[triceps_biceps]    NCHAR (10)     NULL,
[waist]             NCHAR (10)     NULL,
[shoulders]         NCHAR (10)     NULL,
[thighs]            NCHAR (10)     NULL,
[calves]            NCHAR (10)     NULL,
[memberID]          INT            NULL,
[date_of_admission] DATE           NULL,
[photo]             IMAGE          NULL,
PRIMARY KEY CLUSTERED ([InstructorID] ASC),
CONSTRAINT [FK_instructor_info_member_info] FOREIGN KEY ([memberID]) REFERENCES [dbo].
[member_info] ([memberID])
);

member_info表:

CREATE TABLE [dbo].[member_info] (
[memberID]          INT           IDENTITY (1, 1) NOT NULL,
[memberName]        NVARCHAR (50) NULL,
[father_name]       NVARCHAR (50) NULL,
[age]               NCHAR (10)    NULL,
[address]           NVARCHAR (50) NULL,
[contact]           NVARCHAR (50) NULL,
[height]            NVARCHAR (50) NULL,
[weight]            NVARCHAR (50) NULL,
[chest]             NVARCHAR (50) NULL,
[triceps_biceps]    NVARCHAR (50) NULL,
[waist]             NVARCHAR (50) NULL,
[shoulders]         NVARCHAR (50) NULL,
[thighs]            NVARCHAR (50) NULL,
[calves]            NVARCHAR (50) NULL,
[instructorID]      INT           NULL,
[date_of_admission] DATE          NULL,
[photo]             IMAGE         NULL,
PRIMARY KEY CLUSTERED ([memberID] ASC),
CONSTRAINT [FK_member_info_instructor_info] FOREIGN KEY ([instructorID]) REFERENCES 
[dbo].[instructor_info] ([InstructorID])
);

这是我表单上保存按钮的代码:

private void saveBtn_Click(object sender, EventArgs e)
    {
        DataRow row = memberDataS.Tables[0].NewRow();

        row[0] = maxRowsMember + 1;
        row[1] = memberName.Text;
        row[2] = fatherName.Text;
        row[3] = age.Text;
        row[4] = address.Text;
        row[5] = contact.Text;
        row[6] = height.Text;
        row[7] = weight.Text;
        row[8] = chest.Text;
        row[9] = tricepBicep.Text;
        row[10] = waist.Text;
        row[11] = shoulders.Text;
        row[12] = thighs.Text;
        row[13] = calves.Text;
        row[14] = int.Parse(instructor.Text);
        row[15] = dateTimePicker1.Text;

        memberDataS.Tables[0].Rows.Add(row);

        memberString.UpdateDatabase(memberDataS);

        maxRowsMember += 1;
        inc = maxRowsMember - 1;

        MessageBox.Show("Database updated");

        cancelBtn.Enabled = false;
        saveBtn.Enabled = false;
        addNewMemberBtn.Enabled = true;
    }

所有帮助将不胜感激。非常感谢。

1 个答案:

答案 0 :(得分:0)

我相信您的错误可能是由第14行引起的:

  

row [14] = int.Parse(instructor.Text);

如果我对您的代码的假设是正确的,那么讲师变量包含VARCHAR(50)讲师名称,而不是讲师ID的INT IDENTITY值。