导航数据集中的多对多表关系(优化)

发布于 2024-09-10 12:17:07 字数 1362 浏览 4 评论 0原文

我有一个数据集,其中包含大学数据库中的表, 这些表中的两个(实际上更多)包含多对多关系, 具体来说,我有一个

学生表(包含有关学生的信息) 课程表(包含有关课程的信息) TakesCourses 表(这是 StudentsCourses 表之间的中间表,每个学生可以有很多课程)

我想输入一个“学生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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2024-09-17 12:17:07

如果您正在使用(或能够根据项目使用)的 .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 :¬(.

蓝眼睛不忧郁 2024-09-17 12:17:07

我完全同意 JDoig 的观点。下面是使用 LINQ to SQL 时代码的示例:

string stdInfo = string.Empty;
string studentId = txtStudentID.Text;

using (var db = new CourseDataContext())
{
    Student student = (
        from student in db.Students
        where student.StudentID == studentId
        select student).Single();

    stdInfo += string.Format("Student {0} {1}:\nTaking Courses:",
        student.FirstName, student.LastName);

    var courseNames =
        from taken in student.TakesCourses
        select taken.Course.CourseName;

    foreach (string courseName in courseNames)
    {
        stdInfo += string.Format("\nCourse: {0}", courseNames);
    }
}

MessageBox.Show(stdInfo);

正如您将看到的,它需要的代码少得多,并且在显示代码意图方面做得更好。

I totally agree with JDoig. Here is an example of how your code might look like while using LINQ to SQL:

string stdInfo = string.Empty;
string studentId = txtStudentID.Text;

using (var db = new CourseDataContext())
{
    Student student = (
        from student in db.Students
        where student.StudentID == studentId
        select student).Single();

    stdInfo += string.Format("Student {0} {1}:\nTaking Courses:",
        student.FirstName, student.LastName);

    var courseNames =
        from taken in student.TakesCourses
        select taken.Course.CourseName;

    foreach (string courseName in courseNames)
    {
        stdInfo += string.Format("\nCourse: {0}", courseNames);
    }
}

MessageBox.Show(stdInfo);

As you will see it takes far less code and does a much better job in showing the intent of the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文