数据库里三个数据表:Attending        列:Id,UCAS
Applications     列:UCAS,Code
Applicant        列:UCAS,页面上输入一个已经存在表中的Id,比如Id为2 那么Attending中所有Id为2的的UCAS都能显示在页面上,比如 340017, 340018,340021,...   
同理,再将所有显示出来的UCAS所在Applications和Applicant两个表中的相关信息也显示出来,应该如何做呢,下面的代码应该怎么改呢?using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace CourseWork3
{
/// <summary>

/// </summary>
public class WebForm10 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DataGrid DataGrid2;
protected System.Web.UI.WebControls.DataGrid DataGrid3;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Label Label2;
protected System.Data.SqlClient.SqlConnection VisitConnection;

private void Page_Load(object sender, System.EventArgs e)
{
VisitConnection.Open();
SqlCommand command = new SqlCommand("select * from OpenDay", VisitConnection);
SqlDataReader reader = command.ExecuteReader();
DataGrid1.DataSource = reader;
DataGrid1.DataBind();
reader.Close();
VisitConnection.Close();
} #region
override protected void OnInit(EventArgs e)
{
//

//
InitializeComponent();
base.OnInit(e);
}

/// <summary>

/// </summary>
private void InitializeComponent()
{    
this.VisitConnection = new System.Data.SqlClient.SqlConnection();
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
// 
// VisitConnection
// 
this.VisitConnection.ConnectionString = "workstation id=erthrc;packet size=4096;integrated security=SSPI;data source=erthr" +
"c;persist security info=False;initial catalog=VisitDays";
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
String Id = TextBox1.Text;
try
{
VisitConnection.Open();
                SqlCommand command = new SqlCommand("select * from Attending where Id = @Id", VisitConnection);
command.Parameters.Add("@Id",SqlDbType.VarChar);
command.Parameters["@Id"].Value = Id;
                SqlDataReader reader0 = command.ExecuteReader();
if(reader0.Read() )
{
Session["UCASNum"] = reader0["UCAS"];
Label1.Text = ""+Session["UCASNum"];
}
reader0.Close(); SqlCommand commands = new SqlCommand("select * from Applicant where UCAS = @UCAS", VisitConnection);
commands.Parameters.Add("@UCAS",SqlDbType.VarChar);
commands.Parameters["@UCAS"].Value = Session["UCASNum"] +"";
                SqlDataReader reader1 = commands.ExecuteReader();

reader1.Read();
DataGrid2.DataSource = reader1;
DataGrid2.DataBind();
reader1.Close();

SqlCommand command1 = new SqlCommand("select * from Applications where UCAS = @UCAS", VisitConnection);
command1.Parameters.Add("@UCAS",SqlDbType.VarChar);
command1.Parameters["@UCAS"].Value = Session["UCASNum"]+"";
SqlDataReader reader2 = command1.ExecuteReader();

reader2.Read();
DataGrid3.DataSource = reader2;
DataGrid3.DataBind();
reader2.Close();

}

finally
{
VisitConnection.Close();

} } private void Button2_Click(object sender, System.EventArgs e)
{
Response.Redirect("Admin.aspx");
}
}
}

解决方案 »

  1.   

    建议用inner join 或outer join 就好了
      

  2.   

    select a.*.,b.*,c.* from Attending a inner join Applications b on a.id=b.code inner join
    Applicant c on a.id=c.code where a.id=id号
      

  3.   

    为什么我这段代码运行只显示一条记录,明明Attending表里,Id为2的UCAS有两个 340026,和340027
    可是显示出来的只有一个340026呢???
    String Id = TextBox1.Text;
    try
    {
    VisitConnection.Open();
             SqlCommand command = new SqlCommand("select * from Attending where Id = @Id", VisitConnection);
    command.Parameters.Add("@Id",SqlDbType.VarChar);
    command.Parameters["@Id"].Value = Id;
             SqlDataReader reader0 = command.ExecuteReader();
    if(reader0.Read() )
    {
      Session["UCASNum"] = reader0["UCAS"];
      Label1.Text = ""+Session["UCASNum"];
     }
     reader0.Close();
      

  4.   

    如果把If换成While,那么就只显示340017,到底这个循环应该怎么写啊?String Id = TextBox1.Text;
    try
    {
    VisitConnection.Open();
             SqlCommand command = new SqlCommand("select * from Attending where Id = @Id", VisitConnection);
    command.Parameters.Add("@Id",SqlDbType.VarChar);
    command.Parameters["@Id"].Value = Id;
             SqlDataReader reader0 = command.ExecuteReader();
    while(reader0.Read())
    {
      Session["UCASNum"] = reader0["UCAS"];
      Label1.Text = ""+Session["UCASNum"];
     }
     reader0.Close();