2017-05-15 10 views
-3

私は私の医者表では3台、PatientDoctor(リンク患者テーブルの主キーとドクター表主キー)、患者と医師SQL - 別のテーブルにリンクマスタテーブルからデータ


を持って、

を見つけます
---------------------------------- 
|ID|Doctor Name|Specialty|Year Ex| 
---------------------------------- 
|1 |Alex  |Transplt | 3  | 
---------------------------------- 
私の患者診察で

------------------------------- 
|ID|Patient Name|Ward|Diseases| 
------------------------------- 
|5 |Berns  |1234| Cancer | 
------------------------------- 
私PatientDoctorで

、今

------------------------ 
|ID|Patient ID|DoctorID| 
------------------------ 
|6 |5   |1  | 
------------------------ 

、私はコンボボックスから「アレックス」を検索した場合と同様に、患者に関連する医師を検索したい場合は、リストボックスが関連付けられているすべての患者が表示されます。これは私がこれまで行っているコードですが、まだエラーを返します。

> if (comboBox1.SelectedIndex==-1) 
>     MessageBox.Show("Nothing to search!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
>    else 
>    { 
>     using (SqlConnection connection = new SqlConnection(connectionString)) 
>     { 
>      //string sql = "SELECT * FROM Patient " + "WHERE [Patient Name] = @name"; 
>      string sql = "SELECT a.[Patient Name] FROM Patient a " + "INNER JOIN PatientDoctor b ON a.ID = b.[DoctorID] " + "INNER 
> JOIN Doctor c ON b.ID = c.[Doctor Name]" + "WHERE c.[Doctor Name] = 
> @DID"; 
>      using (SqlCommand cmd = new SqlCommand(sql, connection)) 
>      { 
>       cmd.Parameters.AddWithValue("@DID", comboBox1.SelectedValue.ToString()); 
> 
>       DataTable dt = new DataTable(); 
>       SqlDataAdapter ad = new SqlDataAdapter(cmd); 
>       ad.Fill(dt); 
> 
>       if (dt.Rows.Count > 0) 
>       { //check if the query returns any data 
>        listBox1.DataSource = dt; 
>        //dg1.DataBind(); 
>       } 
>       else 
>       { 
>        MessageBox.Show("Record not found!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
>       } 
>       textBox1.Text = ""; 
>      } 
>     } 
>    } 
+0

あなたは、私たちは正しいお使いのコンピュータの前に座っていないことを認識しない「それはまだエラーを返しますか」?あなたは何のエラーがあるのか​​わかりません。あなたは明示的にあなたの問題を述べる必要があります。 – mason

+0

これはあなたのエラーではありませんが、言及する必要があります:アプリケーションの他のレイヤにbussinessコードとデータアクセスコードを入れてみてください。つまり、メソッドを公開し、SOLID https://en.wikipedia.org/wiki/Solidに従うクラス(サービス、リポジトリ)を作成します。フォームコードにsqlコードを入れないでください。 –

答えて

0

あなたがここにc.[Doctor Name]" + "WHERE c.[Doctor Name] =スペースが不足している...それはむしろ

string sql = "SELECT a.[Patient Name] FROM Patient a INNER JOIN PatientDoctor b ON a.ID = b.[DoctorID] INNER JOIN Doctor c ON b.ID = c.[Doctor Name] WHERE c.[Doctor Name] = @DID"; 
+0

識別子が区切られているため、違いはありません。 –

0

あなたのロジックが間違っているようc.[Doctor Name]" + " WHERE c.[Doctor Name] =

はあなたのクエリはなりますする必要があります。あなたはidに名前を参加させています。

SELECT p.[Patient Name] 
FROM Patient p INNER JOIN 
    PatientDoctor pd 
    ON p.ID = pd.PatientID INNER JOIN 
    Doctor d 
    ON pd.DoctorId = d.Id 
WHERE pd.DoctorName = @DID; 

注:

  • 名前の変更@DID@DoctorNameのようなものに私はあなたが意図だと思います。名前で検索するのではなく、IDで検索します。
  • 患者名の結合には、無意味なDoctorIdが使用されます。
  • テーブル名の略語であるテーブルエイリアスを使用すると、クエリの実行が容易になります。
  • AddWithValue()に依存しないでください。明示的に正しいタイプのパラメータを追加します。
+0

これは私に例外エラーを投げます。 –

+0

@KoayHv。 。 。それ以外のエラーは何ですか? –

+0

System.Data.SqlClient.SqlException: '無効な列名' Doctor Name '。' –

0

のC#から、次の試してみてください。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 

namespace ConsoleApplication55 
{ 
    class Program 
    { 

     static void Main(string[] args) 
     { 
      DataTable doctors = new DataTable(); 
      doctors.Columns.Add("ID", typeof(int)); 
      doctors.Columns.Add("Doctor Name", typeof(string)); 
      doctors.Columns.Add("Speciality", typeof(string)); 
      doctors.Columns.Add("Year Ex", typeof(int)); 

      doctors.Rows.Add(new object[] { 1, "Alex", "Transplt", 3}); 

      DataTable patients = new DataTable(); 
      patients.Columns.Add("ID", typeof(int)); 
      patients.Columns.Add("Patient Name", typeof(string)); 
      patients.Columns.Add("Ward", typeof(int)); 
      patients.Columns.Add("Diseases", typeof(string)); 

      patients.Rows.Add(new object[] { 5, "Berns", 1234, "Cancer"}); 

      DataTable patientDoctor = new DataTable(); 
      patientDoctor.Columns.Add("ID", typeof(int)); 
      patientDoctor.Columns.Add("Patient ID", typeof(int)); 
      patientDoctor.Columns.Add("DoctorID", typeof(int)); 

      patientDoctor.Rows.Add(new object[] { 6,5,1}); 

      string doctorName = "Alex"; 
      int doctID = doctors.AsEnumerable().Where(x => x.Field<string>("Doctor Name") == doctorName).Select(x => x.Field<int>("ID")).FirstOrDefault(); 

      var results = (from patDr in patientDoctor.AsEnumerable() 
          join pat in patients.AsEnumerable() on patDr.Field<int>("Patient ID") equals pat.Field<int>("ID") 
          select new { doctorID = patDr.Field<int>("DoctorID"), patient = pat.Field<string>("Patient Name") }) 
          .Where(x => x.doctorID == doctID).Select(x => x.patient).ToList(); 

     } 


    } 

} 
関連する問題