如何在表单中更改表的某些字段以显示其他表中的其他字段?

时间:2014-01-17 15:18:22

标签: c# sql-server winforms foreign-keys

我有一个APPOINTMENT表,它与NURSE表,MEDICALCENTRE表和PATIENT表有关系。在下面附带的第一张图片中是我的表单,它在数据网格视图中显示APPOINTMENT表中的所有数据。我想要做的是将patientID,mcID和nurseID的字段名称更改为其他表中的其他字段。我希望patientID显示为PATIENT表中存在的pFirstName。 mcID显示为MEDICALCENTRE表中存在的mcCentre。和nurseID一起显示为NURSE表中存在的nFirstName。以下是我的page_five表单代码。我应该在select语句中更改什么才能实现我想要显示的内容?

//我的预约表格代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace GRP_02_03_SACP
{
    public partial class appointment : Form
    {

        // Data Table to store employee data
        DataTable Appointment = new DataTable();

        // Keeps track of which row in Gridview
        // is selected
        DataGridViewRow currentRow = null;

        SqlDataAdapter AppointmentAdapter;

        public appointment()
        {
            InitializeComponent();
        }

        private void appointment_Load(object sender, EventArgs e)
        {
            LoadMedicalCentreRecords();
        }



        private void LoadMedicalCentreRecords()
        {

            //retrieve connection information info from App.config
            string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
            //STEP 1: Create connection
            SqlConnection myConnect = new SqlConnection(strConnectionString);
            //STEP 2: Create command
            string strCommandText = "SELECT appointmentID, aDate, aTime, aStatus, aContact, aHeight, aWeight, patientID, mcID, nurseID  FROM APPOINTMENT";

            AppointmentAdapter = new SqlDataAdapter(strCommandText, myConnect);

            //command builder generates Select, update, delete and insert SQL
            // statements for MedicalCentreAdapter
            SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(AppointmentAdapter);
            // Empty Employee Table first
            Appointment.Clear();
            // Fill Employee Table with data retrieved by data adapter
            // using SELECT statement
            AppointmentAdapter.Fill(Appointment);

            // if there are records, bind to Grid view & display
            if (Appointment.Rows.Count > 0)
                grdApp.DataSource = Appointment;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int modifiedRows = 0;
            // Get changes
            DataTable UpdatedTable = Appointment.GetChanges();
            if (UpdatedTable != null)
            {
                // there are changes
                // Write modified data to database 
                modifiedRows = AppointmentAdapter.Update(UpdatedTable);
                // accept changes
                Appointment.AcceptChanges();
            }
            else
                MessageBox.Show("there are no changes to update");

            if (modifiedRows > 0)
            {
                MessageBox.Show("There are " + modifiedRows + " records updated");
                LoadMedicalCentreRecords();
            }
        }


    }
}

任命表格 AppointmentForm 任命表 AppointmentTable NURSE TABLE NurseTable MEDICALCENTRE表 MedicalCentreTable 患者表 PatientTable

1 个答案:

答案 0 :(得分:1)

在您的select语句中,您需要加入其他表。并用这些表中的字段替换ID。

它将是这样的

SELECT appointmentID
   ,aDate
   ,aTime
   ,aStatus
   ,aContact
   ,aHeight
   ,aWeight
   ,p.pFirstName
   ,m.mcCentre
   ,n.nFirstName       
FROM APPOINTMENT AS a
LEFT OUTER JOIN Nurse AS n 
ON a.nurseID = n.NurseID
Left outer join Patient as p
on a.patientid = p.patientId
left outer join medicalcentre as m
on a.mcID = m.mcid