0

这与我上一篇关于从联结表中提取数据的帖子有关。我想出了如何获取数据,但它并没有提取所有实例。这是设置:

我有一个 MS Access 数据库,它有 3 个表:学生、课程、StudentToCourse。StudentToCourse 表示哪个课程与哪个学生相关联,其中一个学生可以注册到许多课程。

我的代码只提取了一个实例,这意味着它只显示与学生相关的 1 门课程,即使学生可能在 StudentToCourse 表中有更多实例但具有不同的课程。

我知道我需要循环和连接字符串,以便它显示其余信息,但我不确定我使用什么作为循环的条件,也不知道在哪里连接。

<%@Page Language="C#"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.Common"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Import Namespace="System.Configuration"%>
<%@ Import Namespace="System.Collections.Generic"%>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>ADO Basic Example</title>
    <link type="text/css" rel="stylesheet" href="../style/style.css" />
    <script runat="server" language="C#">

            void Page_Load()
            {
                if(Session["LoggedInId"]==null)
                {
                    Response.Redirect("Login.aspx?error=1");
                }

                lblUserFirstName.Text = Session["FirstName"].ToString();
                Load();

            }

            void LogOut(object sender, EventArgs e)
            {
                Session["LoggedInId"]=null;
                Session["FirstName"]=null;
                Session["LastName"]=null;
                Response.Redirect("login.aspx");
            }



            void Load()
            {
                String provider = ConfigurationManager.ConnectionStrings["studentConnString"].ProviderName;

                DbProviderFactory factory = DbProviderFactories.GetFactory(provider);

                //Open a Connection
                DbConnection conn = factory.CreateConnection();

                //Assign a Connection String
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["studentConnString"].ConnectionString;

                //Connection Open
                conn.Open();

                //Initialize a Command
                DbCommand comm = conn.CreateCommand();

                //Tell the command which connection it will use
                comm.Connection = conn;

                //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 StudentToCourse.Studentid = ?";

                DbParameter param;
                param = comm.CreateParameter();
                param.DbType = DbType.Int32;
                param.Direction = ParameterDirection.Input;
                param.Value = Session["LoggedInId"];

                comm.Parameters.Add(param);

                //Execute the command and get back the results via a reader
                DbDataReader reader = comm.ExecuteReader();

                //While we get results from the DB, add a row to the Table
                while (reader.Read())
                {
                    lblCourseSubject.Text = reader["CourseSubject"].ToString();
                    lblCourseNumber.Text = reader["CourseNumber"].ToString();
                    lblCourseName.Text = reader["CourseName"].ToString();
                    lblCourseDesc.Text = reader["CourseDescription"].ToString();
                }

                //Free up the connection
                conn.Close();

            }


    </script>
</head>
<body>
<form runat="server">
    <div id="mainBody">
        <header>
            <hgroup>
                <h1><asp:Label ID="lblUserFirstName" runat="server"></asp:Label>'s Enrolled Courses</h1>
            </hgroup>
        </header>
        <section>

            <p>Course Enrollment Summary</p>

            <div class="courseInfo">
                <div class="courseId"><span><asp:Label ID="lblCourseSubject" runat="server"></asp:Label></span><span><asp:Label ID="lblCourseNumber" runat="server"></asp:Label></span></div>
                <div class="courseName"><asp:Label runat="server" ID="lblCourseName"></asp:Label></div>
                <div class="courseDesc"><asp:Label runat="server" ID="lblCourseDesc"></asp:Label></div>
            </div>

            </br>
            <asp:Button ID="btnLogout" runat="server" Text="LogOut" OnClick="LogOut"></asp:Button>

        </section>
        <footer>
        </footer>
    </div>
</form>
</body>
</html>
4

1 回答 1

1

我知道我需要循环和连接字符串,以便显示其余信息

不,你没有。您正在使用 WebForms,并且在使用 WebForms 时,最好的方法是将 DataReader 设置为支持Data Binding的控件的 DataSource 。

于 2013-05-16T04:01:55.877 回答