How to jump to table to table for 2 times to get the field I want, using outer join

StackOverflow https://stackoverflow.com/questions/21353914

  •  02-10-2022
  •  | 
  •  

Domanda

I have three tables, which is prescription, appointment and payment. Prescription have relationship with appointment and appointment have relationship with payment. Currently I only manage to jump from prescription table to appointment table and get the aDate field and display it in the dategridview. Now I need to jump from prescription table to appointment table and then to payment table to get the amount field. How do i write it?

MY DATEGRIDVIEW enter image description here

MY TABLES enter image description here

private void prescription_Load(object sender, EventArgs e)
        {
            LoadPrescriptionRecords();
        }

        private void LoadPrescriptionRecords()
        {

            //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 prescriptionID, app.aDate FROM PRESCRIPTION AS pres";
            string strCommandText = "SELECT prescriptionID, app.aDate FROM PRESCRIPTION AS pres";
            strCommandText += " LEFT OUTER JOIN appointment as app on pres.appointmentid = app.appointmentid";

            myConnect.Open();

            PrescriptionAdapter = new SqlDataAdapter(strCommandText, myConnect);

            //readMEDICATION.Close();
            myConnect.Close();      

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

            // if there are records, bind to Grid view & display
            if (Prescription.Rows.Count > 0)
                grdPrescription.DataSource = Prescription;         
        }
È stato utile?

Soluzione

In your strCommandText just do one more join i.e. join appointment to payment.

string strCommandText = "SELECT prescriptionID, app.aDate, p.amount FROM PRESCRIPTION AS pres ";
strCommandText += "LEFT OUTER JOIN appointment as app on pres.appointmentid = app.appointmentid ";
strCommandText += "LEFT OUTER JOIN payment p on app.appointmentid = p.appointmentid ";

Altri suggerimenti

Just join it with payment table as you did with the other one.

SELECT prescriptionID, app.aDate,pay.Amount FROM PRESCRIPTION AS pres
 LEFT OUTER JOIN appointment as app on pres.appointmentid = app.appointmentid
 Left outer join Payment as pay on app.appointmentid = pay.appointmentid
string strCommandText = "SELECT prescriptionID, app.aDate, P.Amount 
 FROM PRESCRIPTION AS pres 
 LEFT OUTER JOIN appointment as app on pres.appointmentid = app.appointmentid 
 LEFT OUTER JOIN Payment as P on P.appointmentid = pres.appointmentid";

EDIT:This was an easy one, that's why 3 of us are hitting for the fence. You need to so a little more learning in SQL. I would suggest finding a SQL tool like SSMS to run your queries through before adding the complexities of C# and all the connection classes. SQL Server Management Studio

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top