2014-01-17 119 views
0

我有一個與NURSE表,MEDICALCENTRE表和PATIENT表有關係的APPOINTMENT表。在下面附加的第一個圖像是我的表單,它在數據網格視圖中顯示來自APPOINTMENT表的所有數據。我想要做的是將patientID,mcID和nurseID的字段名稱更改爲其他表中的其他字段。我希望patientID顯示爲PATIENT表中存在的pFirstName。 mcID顯示爲存在於MEDICALCENTRE表中的mcCentre。並且nurseID顯示爲NURSE表中存在的nFirstName。以下是我的page_five表單代碼。我應該在選擇聲明中更改什麼才能實現我想要顯示的內容?如何更改窗體中某些表的字段以顯示其他表中的其他字段?

//我的約會表單代碼

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 護士表 NurseTable MEDICALCENTRE表 MedicalCentreTable 患者臺上 PatientTable

回答

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 
+0

謝謝,但我現在不能使用更新功能來改變我的所有字段的值的值。這個錯誤(動態SQL生成不支持針對多個基表)。@SaUce – Pony

+0

我爲此創建了一個新線程http://stackoverflow.com/questions/21201170/i-changed-some-of-my-fields-to -display-as-other-fields-in-other-table-and-now-i @SaUce – Pony

相關問題