导航数据集中的多对多表关系(优化)
我有一个数据集,其中包含大学数据库中的表, 这些表中的两个(实际上更多)包含多对多关系, 具体来说,我有一个
学生表(包含有关学生的信息) 课程表(包含有关课程的信息) TakesCourses 表(这是 Students 和 Courses 表之间的中间表,每个学生可以有很多课程)
我想输入一个“学生ID”,并根据TakesCourses表中找到的记录,从Courses表中获取所有记录。
我的代码WORKS如下:
string stdInfo = string.Empty;
DataRow[] drStudent = null;
DataRow drCourses = null;
DataRow[] drStdCrs = null;
drStudent = universityDS.Tables["Students"]
.Select(string.Format("StudentID='{0}'", txtStudentID.Text));
stdInfo += string.Format("Student {0} {1}:\nTaking Courses:",
drStudent[0]["FirstName"].ToString().Trim(),
drStudent[0]["LastName"].ToString().Trim());
drStdCrs = drStudent[0].GetChildRows(
universityDS.Relations["FK_TakesCourses_Students"]);
//Can I optimize here? Is there a better way to code this
if (drStdCrs.Length > 0)
{
for (int i = 0; i < drStdCrs.Length; i++)
{
drCourses = drStdCrs[i].GetParentRow(
universityDS.Relations["FK_TakesCourses_Courses"]);
stdInfo += string.Format("\nCourse: {0}",
drCourses["CourseName"]);
}
}
MessageBox.Show(stdInfo);
我的问题是,如何优化注释后的代码? 有更好的方法来写这个吗?
(注:我是一名正在康复的开发人员,正在努力刷新我的技能)
I have a Dataset that contains tables from a university database,
two(actually more) of these tables contain a many to many relationship,
specifically I have a,
Students table (Contains information about students)
Courses table (Contains information about the courses)
TakesCourses table (Which is an intermediary table between the Students and Courses tables, every student can have many courses)
I want to input a "student ID" and fetch all the records from the Courses table according to the records found in TakesCourses table.
My code WORKS and it is as follows:
string stdInfo = string.Empty;
DataRow[] drStudent = null;
DataRow drCourses = null;
DataRow[] drStdCrs = null;
drStudent = universityDS.Tables["Students"]
.Select(string.Format("StudentID='{0}'", txtStudentID.Text));
stdInfo += string.Format("Student {0} {1}:\nTaking Courses:",
drStudent[0]["FirstName"].ToString().Trim(),
drStudent[0]["LastName"].ToString().Trim());
drStdCrs = drStudent[0].GetChildRows(
universityDS.Relations["FK_TakesCourses_Students"]);
//Can I optimize here? Is there a better way to code this
if (drStdCrs.Length > 0)
{
for (int i = 0; i < drStdCrs.Length; i++)
{
drCourses = drStdCrs[i].GetParentRow(
universityDS.Relations["FK_TakesCourses_Courses"]);
stdInfo += string.Format("\nCourse: {0}",
drCourses["CourseName"]);
}
}
MessageBox.Show(stdInfo);
My question is, how can I optimize the code after the comments?
is there a better way to write this?
(Note: I am a recovering developer, trying to refresh my skills)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在使用(或能够根据项目使用)的 .NET 框架版本支持 LINQ2SQL,我强烈建议您花时间学习 LINQ,因为它会让您的生活轻松一千倍无论是在处理 SQL 时还是在使用 .NET 进行日常编码时。
在当今这个时代,我会避免使用内联 SQL,除非它是你唯一的手段,因为在大多数机构中它通常会被判处死刑:Ø(。
If the version of the .NET framework you are using (or able to use based on the project) supports LINQ2SQL I HIGHLY recommend you take the time to learn LINQ as it will make your life a thousand times easier both when dealing with SQL and in every day coding with .NET.
In this day and age I would avoid in-line SQL unless it is your only resort, as it's often punishable by death in most establishments :¬(.
我完全同意 JDoig 的观点。下面是使用 LINQ to SQL 时代码的示例:
正如您将看到的,它需要的代码少得多,并且在显示代码意图方面做得更好。
I totally agree with JDoig. Here is an example of how your code might look like while using LINQ to SQL:
As you will see it takes far less code and does a much better job in showing the intent of the code.