我有一个约会表单,它将根据约会表格在gridview中显示数据。预约表与患者和医疗中心表有关系。我编写了select语句,以便PatientID将更改为患者表中的pFirstName,并且mcID将更改为medicalcentre表中的mcCentre。奇怪的是pFirstName没有问题,但mcCentre在第一行之后没有显示。如果我没有外部连接并将其保留为mcID,则它将在所有行上显示mcID。
外部联接之前的约会表单的MY DATAGRID,显示mcID
外部联接后的约会表单的MY DATAGRID,从mcID更改为mcCentre
我的任命,患者,医疗中心
//外部联接后的MY约会表单,从mcID更改为mcCentre代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class member_viewappointment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
// call BindGridView
bindGridView();
}
}
private void bindGridView()
{
int ID = Convert.ToInt32(Session["ID"].ToString());
//get connection string from web.config
string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "SELECT aStatus, aDate, aTime, aContact, aHeight, aWeight, med.mcCentre, pat.pFirstName from appointment AS app ";
strCommandText += " LEFT OUTER JOIN MEDICALCENTRE as med on app.appointmentid = med.mcid";
strCommandText += " LEFT OUTER JOIN PATIENT as pat on app.patientid = pat.patientid ";
//strCommandText += " WHERE app.patientid = " + ID.ToString();
try
{
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
myConnect.Open();
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
grdViewAppointment.DataSource = dt;
grdViewAppointment.DataBind();
lblResult.Text = "";
reader.Close();
}
catch (SqlException ex)
{
lblResult.Text = "Error:" + ex.Message.ToString();
}
finally
{
myConnect.Close();
}
}
}
答案 0 :(得分:2)
您在MEDICALCENTRE
app.appointmentid = med.mcid
表格
LEFT OUTER JOIN MEDICALCENTRE as med on app.appointmentid = med.mcid
看起来不对。看起来你打算加入app.mcid = med.mcid
LEFT OUTER JOIN MEDICALCENTRE as med on app.mcid = med.mcid