使用table1 primary(表2中的外键)显示Access数据库的表记录

时间:2013-12-08 19:18:59

标签: c# sql ms-access-2010

我在同一个MS Access数据库中有两个表,一个名为Consultant Doctor,另一个名为PatientsConsultant Doctor中的主键是DoctorID,与DoctorID中的Patients相关联。

用户输入患者ID,并应显示他/她的医生信息。

这是我的代码,我不知道如何将它们链接在一起

        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"]);
        }

正如你所看到的,我试图使用内部连接似乎不起作用

2 个答案:

答案 0 :(得分:2)

我不知道这是否是您唯一的问题,但内部联接的语法应该是INNER JOIN,而不是INNERJOIN

ETA:其他几件事:

  • 在尝试访问阅读器中的值之前,您需要在.Read()上执行dbReader。当它打开时,读取器位于第一个记录之前,并且没有“当前”记录。执行dbReader.Read()将读取第一条记录并使其可用。

  • 阅读您的代码,看起来您希望anInteger的值与您在代码顶部放置的值不同。但是你的SQL不会设置它,当你添加参数时,你并没有指出它是一个输出参数。

  • 在您测试anInteger时,它将等于textBox9.Text的值。因此,如果这是用户输入的,那么它将仅等于101。你确定这是你想到的逻辑吗?

答案 1 :(得分:1)

你的sql错了。您需要正确拼写INNER JOIN子句,但您还需要添加WHERE语句来标识您的患者ID

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

当然,正如其他答案所指出的,您需要在OleDbDataReader上执行Read方法,否则您无法尝试从中读取任何内容。这里还存在其他问题,需要修复。

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"]);
        }
    }
 }

如果出现异常,using statement将有助于关闭和处理代码块中使用的对象