2016-05-03 7 views
0

プログラミングには新しく、現在はC#で作業中ですが、今は何かを試しています。私は行と列のデータを含むmsアクセスでデータベースを作成しました。私はまた、テキストボックスを含むウィンドウフォームアプリケーションを作成しました。私が今したいのは、データベーステーブル内の単一の列と行から各データを収集し、それをWindowsフォームの各テキストボックスに挿入するコードを記述することです。 これは私がやったことです:C#でデータベースからデータを取得する

// Parameterize the query 
command.CommandText = "SELECT matricule, name, department, specialty, session FROM Table1 WHERE matricule = ?"; 

してから使用して、実行前に、あなたのパラメータを設定します。

try 
{ 
    //... 
    command.CommandText = "SELECT (Table1.matricule, Table1.name, Table1.department, Table1.specialty, Table1.session) FROM Table1 WHERE (((Table1.matricule)=[textBox6.Text]))"; 
    command.CommandType = CommandType.Text; 
    connection.Open(); 

    OleDbDataReader reader = command.ExecuteReader(); 

    while (reader.Read()) 
    { 
     Person p = new Person(); 

     textBox1.Text = reader["Table1.matricule"]; 
     textBox2.Text = reader["Table1.name"]; 
     textBox3.Text = reader["Table1.department"]; 
     textBox4.Text = reader["Table1.specialty"]; 
     textBox5.Text = reader["Table1.session"]; 

     personsList.Add(p); 
    } 
    return personsList; 
} 
catch (Exception) 
{ 
    throw; 
} 
+1

あなたが期待する結果が何であるかを、追加するのを忘れ。 –

+0

どこが失敗していますか?スローされた例外がありますか(そしてメッセージは何ですか?)接続文字列を正しく設定しましたか? – Neil

+0

パラメータを使用したMSDNドキュメントの例を見てください。https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx –

答えて

1

あなたはあなたの文字列の中でそれらを指定するとは対照的に、実際のコードでは、あなたのパラメータ値を設定する必要がありますAddWithValue()方法:

connection.Open(); 
// Set your parameter prior to executing the query 
command.Parameters.AddWithValue("@matricule",textBox6.Text); 
// Now execute your query 
OleDbDataReader reader = command.ExecuteReader(); 

最後に、あなたはおそらく、あなたのようPersonオブジェクトのプロパティを移入する必要がありますテキストボックスの値を設定することに反対(Personオブジェクトとしては空になります):

// Populate person properties within your loop 
Person p = new Person() 
{ 
     Matricule = reader["matricule"], 
     Name = reader["name"], 
     Department = reader["department"], 
     Specialty = reader["specialty"], 
} 

考慮リファクタリング

あなたは using文の使用を含む、いくつかの他の人と一緒にこれらの変更を(実装することができ

)オブジェクトが適切に閉じられ、処分されていることを確認します。

// Define your list of people 
var people = new List<Person>(); 
// Create your connection 
using(var connection = new OleDbConnection("{your-connection-string}")) 
{ 
    try 
    { 
     // Define your query (and parameters) 
     var query = "SELECT matricule, name, department, specialty, session FROM Table1 WHERE matricule = ?"; 
     // Define a using statement 
     using(var command = new OleDbCommand(query, connection)) 
     { 
      connection.Open(); 
      // Set your parameter prior to executing the query 
      command.Parameters.AddWithValue("@matricule",textBox6.Text); 
      // Execute your query 
      using(var reader = command.ExecuteReader()) 
      { 
        // While you have rows, read them 
        while(reader.Read()) 
        { 
         people.Add(new Person() 
         { 
          Matricule = reader["matricule"], 
          Name = reader["name"], 
          Department = reader["department"], 
          Specialty = reader["specialty"], 
         }); 
        } 
        // Return your collection 
        return people; 
      } 
     } 
    } 
    catch(Exception ex) 
    { 
     // Something blew up, handle accordingly 
    } 
} 
0

いくつかのことは、与えられたコードで間違っている:

1.Useは、プレーンテキストの代わりにクエリをパラメータ化: 期待されるクエリ結果の意志が複数の行が含まれている場合は、あなただけ取得する:あなたがここにpersonsListを使用している理由

command.CommandText = "SELECT matricule, name, department, specialty, session FROM Table1 WHERE matricule = ?"; 
connection.Open(); 
command.Parameters.Add("@matricule",OleDbType.VarChar).Value= textBox6.Text; 
OleDbDataReader reader = command.ExecuteReader(); 

2.Iは把握することはできません最後の値がテキストボックスに表示されます。複数の行でない場合は、personオブジェクトに詳細を追加する必要があります。

personsList.Add(new Person() 
{ 
     Matricule = reader["matricule"], 
     Name = reader["name"], 
     Department = reader["department"], 
     Specialty = reader["specialty"], 
}); 

3.Youは、リーダーオブジェクトから値にアクセスしているときに毎回Table1.を使用する必要はありません。単にあなたが使用してそれらを読むことができます:

textBox1.Text = reader["matricule"]; 
// load rest of textboxes 

4。全体のことをします、次のようになります。

command.Parameters.Add("@matricule",OleDbType.VarChar).Value= textBox6.Text; 
connection.Open(); 
OleDbDataReader reader = command.ExecuteReader(); 
while (reader.Read()) 
{ 
    textBox1.Text = reader["matricule"]; 
    textBox2.Text = reader["name"]; 
    textBox3.Text = reader["department"]; 
    textBox4.Text = reader["specialty"]; 
    textBox5.Text = reader["session"]; 
    personsList.Add(new Person() 
    { 
      Matricule = reader["matricule"], 
      Name = reader["name"], 
      Department = reader["department"], 
      Specialty = reader["specialty"], 
    }); 
} 
関連する問題