简体   繁体   中英

Display the table records of an Access database using table1 primary which is the foreign key in table 2

I have two tables in the same MS Access database, one called Consultant Doctor and the other is Patients . The primary key in Consultant Doctor is DoctorID , which is linked to the DoctorID in Patients .

The user inputs the patients ID and it should show his/her doctors information.

This is my code, I don't know how to link them together

        int anInteger;
        anInteger = Convert.ToInt32(textBox9.Text);
        anInteger = int.Parse(textBox9.Text);

        sConnection = 
        "Provider=Microsoft.ACE.OLEDB.12.0;" +               "DataSource=hospital database.accdb";
        dbConn = new OleDbConnection(sConnection);
        dbConn.Open();

        sql = 
        "SELECT * FROM Consultant Doctor INNERJOIN      Patients ON Consultant Doctor.DoctorID =    Patients.DoctorID";
        dbCmd = new OleDbCommand();
        dbCmd.CommandText = sql;
        dbCmd.Connection = dbConn;
        dbCmd.Parameters.AddWithValue("pID", anInteger);
        dbReader = dbCmd.ExecuteReader();

        if (anInteger == 101)
        {
            listBox3.Items.Add(dbReader["PatientID"]);
            listBox3.Items.Add(dbReader["DoctorName"]);
            listBox3.Items.Add(dbReader["DoctorAddress"]);
            listBox3.Items.Add(dbReader["Specialization"]);
            listBox3.Items.Add(dbReader["WardID"]);
            listBox3.Items.Add(dbReader["TeamID"]);
        }

As you can see, I tried to use inner join which doesn't seem to work

I don't know if this is your only problem, but the syntax for an inner join should be INNER JOIN , not INNERJOIN .

ETA: A couple of other things:

  • You need to perform a .Read() on dbReader before attempting to access the values in the reader. When it opens, a reader is positioned just before the first record, and there is no "current" record. Performing dbReader.Read() will read the first record and make it available.

  • Reading your code, it looks as if you expect anInteger to have a different value than the one you place in it at the top of the code. But your SQL won't set it, and when you add the parameter, you aren't indicating that it's an output parameter.

  • At the point you test anInteger , it's going to be equal to the value of textBox9.Text . So it's only going to be equal to 101 if that's what the user entered. Are you sure this is the logic you have in mind?

Your sql is wrong. You need to spell correctly the INNER JOIN clause, but you also need to add a WHERE statement to identify your patient id

sql = 
    "SELECT * FROM [Consultant Doctor] " + 
    "INNER JOIN Patients ON [Consultant Doctor].DoctorID = Patients.DoctorID " +
    "WHERE Patients.PatientID = @pID";

Of course, as pointed by the other answers, you need to execute a Read method on the OleDbDataReader otherwise you cannot try to read anything from it. Other problems are present here and need to be fixed..

int anInteger;
anInteger = Convert.ToInt32(textBox9.Text);
anInteger = int.Parse(textBox9.Text);

sql = "SELECT * FROM [Consultant Doctor] " + 
       "INNER JOIN Patients ON [Consultant Doctor].DoctorID = Patients.DoctorID " +
       "WHERE Patients.PatientID = @pID";
sConnection =  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=hospital database.accdb";
using(OleDbConnection dbConn = new OleDbConnection(sConnection))
using(OleDbCommand dbCmd = new OleDbCommand(sql dbConn))
{
    dbConn.Open();
    dbCmd.Parameters.AddWithValue("pID", anInteger);
    using(OleDbDataReader dbReader = dbCmd.ExecuteReader())
    {
        if(dbReader.Read())
        {
             listBox3.Items.Add(dbReader["PatientID"]);
             listBox3.Items.Add(dbReader["DoctorName"]);
             listBox3.Items.Add(dbReader["DoctorAddress"]);
             listBox3.Items.Add(dbReader["Specialization"]);
             listBox3.Items.Add(dbReader["WardID"]);
             listBox3.Items.Add(dbReader["TeamID"]);
        }
    }
 }

The using statement will help to close and dispose objects used in the block of code also in case of exceptions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM