如何在 ASP.NET 中实现和操作多对多关系
假设我们有三个表
Course - table
------------------
ID | CourseName
------------------
1 | C++
2 | Java
------------------
Teacher - table
-----------------------
ID | TeacherName
-----------------------
1 | Professor 1
2 | Professor 2
-----------------------
CourseTeacher - table
----------------------------
ID | CourseID | TeacherID
----------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 2 | 2
----------------------------
现在这就是 AFAK 要做的事情,但是如何选择、插入、更新、删除这种格式的记录!
我的意思是,我会使用简单的“INSERT INTO ... VALUES (..,..,..)”或简单的“SELECT * FROM ...”,
但现在要检索相同的单个信息,我必须以某种方式使用一些包含 3 个表的查询(提示:我已经使用了联接,但仍然如何使用!!)
对于我的情况,选择和插入 SQL 语句将非常有帮助。
我没有使用任何模型和这种复杂的东西,我不知道这样的东西有什么用! 我在 ADO.NET 对象中使用 SQL 查询,到目前为止一切工作正常!
Assume that we've three tables
Course - table
------------------
ID | CourseName
------------------
1 | C++
2 | Java
------------------
Teacher - table
-----------------------
ID | TeacherName
-----------------------
1 | Professor 1
2 | Professor 2
-----------------------
CourseTeacher - table
----------------------------
ID | CourseID | TeacherID
----------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 2 | 2
----------------------------
Now that's what to do as AFAK, but how to select, insert, update, delete records in such a format!
I mean, I'd use a simple "INSERT INTO ... VALUES (..,..,..)" or a simple "SELECT * FROM ..."
but now to retrieve the same single information I've to use some queries that includes the 3 tables somehow (Hint: I already use joins, but still how!!)
A select and insert SQL statement for my case would be very helpful.
I don't use any models and this complicated stuff, I don't know what the use of such a thing!
I'm using SQL Queries in my ADO.NET Objects and everything is working fine so far!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定你的问题是什么......
让我们假设你在每个表中的 PK 都会自动递增。
要添加课程:
INSERT INTO Course VALUES 'My New Course'
要添加教师:
INSERT INTO Teacher VALUES 'My New Teacher'
将现有课程添加到现有教师:
在最坏的情况下,这意味着您只知道教师和课程的名称。
事实上,在您的应用程序中,当您的用户选择要添加到教师的课程时,您应该已经知道 ID 是什么,因为当您得到了包含它们的列表,你当然已经这样做了:
如果我写的不是,则可能有用的链接:
http://www.singingeels.com/Articles/Understanding_SQL_Many_to_Many_Relationships.aspx
I'm not sure to understand what your question is ...
Let's assume that your PK in each table is automatically incremented.
To add a course :
INSERT INTO Course VALUES 'My New Course'
To add a teacher :
INSERT INTO Teacher VALUES 'My New Teacher'
Tu add an existing Course to an existing Teacher :
In the worst case scenario, meaning that you only know the names of the Teacher and the Course
In fact, in your application, when your user selects a Course to add to a Teacher, you should already know what the IDs are, because when you got the list containing them, you certainly have done this :
A link which may be useful if what I wrote is not :
http://www.singingeels.com/Articles/Understanding_SQL_Many_to_Many_Relationships.aspx
它和其他表一样简单。
1 - 我假设,您必须有用于插入、更新、选择课程和教师表的代码。因此继续使用相同的方法。
2-但是对于 CourseTeacher 表(Junction 表),当您在各个表中实际有相应的 TearcherId 和 CourseId 时,您必须插入此表。
因此,当您想在 CourseTeacher 表中添加新记录时,首先确保 courseid 和teacherid 各自的值存在。
当您想在联结表中更新时,请使用 Id 列来更新
it is as simple as for other table.
1 - I assume , you must have code for insert, update , select for course and teacher table. So continue using same methods.
2- but for CourseTeacher table (Junction table) , you must insert in this table when you actually have corrosponding TearcherId and CourseId avaliable in repective tables.
so when you want to add a new record in CourseTeacher table , first ensure respective values of courseid and teacherid presents.
when you wanna update in junction table , use the Id column to updae