C# 在ASP/C中循环数据库以从连接表中提取所有数据

C# 在ASP/C中循环数据库以从连接表中提取所有数据,c#,asp.net,C#,Asp.net,这与我上一篇关于从连接表中提取数据的文章有关。我知道了如何获取数据,但它并没有拉取所有的实例。以下是设置: 我有一个MS Access数据库,它有三个表:学生、课程、学生到课程。StudentToCourse表示与学生相关的课程,学生可以注册多个课程 我的代码只调出一个实例,这意味着它只显示学生绑定到的一个课程,即使学生在StudentToCourse表中可能有更多实例,但课程不同 我知道我需要循环并连接字符串,以便它显示其余的信息,但我不确定使用什么作为循环的条件,也不确定连接到哪里 <

这与我上一篇关于从连接表中提取数据的文章有关。我知道了如何获取数据,但它并没有拉取所有的实例。以下是设置:

我有一个MS Access数据库,它有三个表:学生、课程、学生到课程。StudentToCourse表示与学生相关的课程,学生可以注册多个课程

我的代码只调出一个实例,这意味着它只显示学生绑定到的一个课程,即使学生在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>
我知道我需要循环并连接字符串,以便它显示其余的信息


不,你没有。您使用的是WebForms,在使用WebForms时,最好的方法是将DataReader设置为支持的控件的数据源。

我现在正在研究这个问题,但没有了解到这一点,因此我将尝试解决这个问题。谢谢你的建议。