如何使用C#表单将数据插入到具有外键约束的SQL数据库表中? [英] How to insert data into a SQL database table with foreign key constraints using C# form?

查看:386
本文介绍了如何使用C#表单将数据插入到具有外键约束的SQL数据库表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我是C#和SQL服务器的初学者。



我在SQL服务器上创建了这些表。



1.主题

-SubjectID(PK)

-SubjectName

-Mark



2.班级

-ClassNo(PK)

-HomeRoomTeacher(FK)

-StudentID(FK)



3. Subject_Class

-SubjectID(PK)

-ClassNo(PK)



4.老师

-TeacherID(PK)

-TeacherName

-ClassNo(只有HomeRoomTeacher具有指定的类)



5.学生

-StudentID(PK)

-StudentName

-ClassNo(FK)



我想将以下数据输入相应的表格。

科目:英语,数学,物理,化学,地理,历史和科学

分类:5A,5B,5C,6A,6B,6C,7A,7B,7C,8A,8B,8C ...... ....

学生:约翰,Mary,Kaito,........

老师:Mark,Jane,Rosy,......





然后我尝试使用C#表单输入数据。但异常对话框表示与外键约束冲突。

如何解决这个问题?



我尝试过的事情:



我被告知尝试将数据输入到没有外键的表中,或者只是在实际项目中不使用外键。但我没有得到它,因为它与维护数据完整性完全相反。而且,所有表在上述数据库中相互链接。有没有我应该首先输入数据的表?

Hi! I am a beginner to C# and SQL server.

I have created these tables in SQL server.

1. Subject
-SubjectID (PK)
-SubjectName
-Mark

2. Class
-ClassNo (PK)
-HomeRoomTeacher(FK)
-StudentID(FK)

3. Subject_Class
-SubjectID (PK)
-ClassNo (PK)

4. Teacher
-TeacherID(PK)
-TeacherName
-ClassNo (only the HomeRoomTeacher has the assigned Class)

5. Student
-StudentID(PK)
-StudentName
-ClassNo(FK)

I want to input the following data to the corresponding tables.
Subjects: English, Maths, Physics, Chemistry, Geography, History & Science
Class : 5A,5B,5C,6A,6B,6C,7A,7B,7C,8A,8B,8C,....
Student : John, Mary, Kaito,........
Teacher : Mark, Jane, Rosy,......


Then I tried to input data using C# form. But the exception dialogbox says conflicts with the foreign key constraint.
How can I solve this problem?

What I have tried:

I have been told to try to input data into a table with no foreign keys first or simply don't use foreign keys in real projects. But I don't get it since it is the complete opposite of maintaining data integrity. Moreover, all the tables are linked to each other in the above database. Is there a table that I should input data first?

推荐答案

想一想:考虑实物发票。
Think about it: consider an physical invoice.
Customer name
Customer address
Date
Invoice numner
Items:
   12     Widgets


10.00


120.00
5 Thingumies
120.00 5 Thingumies


这篇关于如何使用C#表单将数据插入到具有外键约束的SQL数据库表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆