【问题标题】:Query a Junction Table from MS Access Database in a ASP / C# Webpage在 ASP/C# 网页中从 MS Access 数据库中查询连接表
【发布时间】:2013-05-16 01:03:02
【问题描述】:

我正在编写一个 ASP 页面,其中:学生登录 -> 学生到达欢迎页面,带有链接以显示他们已注册的课程 -> 链接页面显示课程信息、描述等。

我的 MS Access 数据库有 3 个表:学生(包含学生信息)、课程(课程信息,如 ID、描述等)和 EnrolledCourses(其中 EnrolledCourses 是一个连接表 - 我认为这是正确的term - 学生和课程代表学生已注册的课程)。

我需要显示他们注册了哪些课程以及课程信息,因此来自 EnrolledCourses 的数据告诉了已注册的课程,而来自 Courses 的数据则提供了课程的详细信息。我在弄清楚查询将是什么来提取该信息时遇到了麻烦。到目前为止,这是我想出的:

{               
        //Give the command SQL to execute   
    comm.CommandText = "SELECT Courses.Id, Courses.CourseSubject, Courses.CourseName, Courses.CourseNumber, Courses.CourseDescription FROM Courses, StudentToCourse, Students WHERE Courses.Id = StudentToCourse.Courseid AND Session["id"] = StudentToCourse.Studentid";

}

只是想知道这是否在正确的轨道上?我不断收到以下编译错误:编译器错误消息:CS1002:;预计

【问题讨论】:

    标签: c# ms-access asp-classic junction-table


    【解决方案1】:

    你能试试这个吗:

        comm.CommandText = "SELECT Courses.Id, Courses.CourseSubject, Courses.CourseName, Courses.CourseNumber, Courses.CourseDescription
    FROM Courses, StudentToCourse, Students WHERE Courses.Id = StudentToCourse.Courseid AND StudentToCourse.Studentid = " + Session["id"];
    

    但它有漏洞。您应该添加如下参数:

    comm.CommandText = "... StudentToCourse.Studentid=@studentID";
    comm.Parameters.AddWithValue("@studentID", Session["id"]);
    

    这样更好/更安全。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多