2010-12-29 20 views
0

私の持っているものはForm1.csの[デザイン]のコンボボックスです。SQLの操作を行うSQLOperationsという別のクラスを作成しました。 ?このコンボボックスのアイテムを別のクラスに追加するためのC#winforms

public static System.Collections.Generic.List<string> SQLServerPull() 
    { 
     System.Collections.Generic.List<string> Items = new System.Collections.Generic.List<string>(); 
     string server = "p"; 
     string database = "IBpiopalog"; 
     string security = "SSPI"; 
     string sql = "select server from dbo.ServerList"; 
     using(SqlConnection con = new SqlConnection("Data Source=" + server + ";Initial Catalog=" + database + ";Integrated Security=" + security)) 
     { 
      con.Open(); 
      SqlCommand cmd = new SqlCommand(sql, con); 
      SqlDataReader dr = cmd.ExecuteReader(); 
      while(dr.Read()) 
      { 
       //Add the items to the list. 
       Items.Add(dr[0].ToString()); 
       //this below doesnt work because it can't find the comboBox    
       //comboBox1.Items.Add(dr[0].ToString()); 
       //the rest of the code works fine 
      } 
     } 
     //Return the list of items. 
     return Items; 
    } 

をするか、または代わりに、あなたのUIとデータを結合するのDataTable

public static System.Data.DataTable SQLServerPull() 
    { 
     System.Data.DataTable dt = new System.Data.DataTable(); 
     string server = "p"; 
     string database = "IBpiopalog"; 
     string security = "SSPI"; 
     string sql = "select server from dbo.ServerList"; 
     using(SqlConnection con = new SqlConnection("Data Source=" + server + ";Initial Catalog=" + database + ";Integrated Security=" + security)) 
     { 
      con.Open(); 
      using(SqlCommand cmd = new SqlCommand(sql, con)) 
      { 
       using(SqlDataReader dr = cmd.ExecuteReader()) 
       { 
        dt.Load(dr); 
       } 
      } 
     } 
     return dt; 
    } 
+0

あなたのメソッドがリストまたは文字列配列を返さないようにして、それからコンボボックスを作成してみませんか? – Tester101

+0

私はできると思いますが、実際にそれを直接渡す方法はありませんか? –

+2

もちろん、データアクセスレイヤーをUIタイプに結合したいのですか? – Oded

答えて

-1

"SQLServerPull"メソッド? UIは、ComboBoxを塗りつぶすなど、どのような方法でもその生データを使用できます。

2

を返さない、なぜあなたは単にあなたからのデータのセットを返さないのはなぜ

public static void SQLServerPull() 
    { 
     string server = "p"; 
     string database = "IBpiopalog"; 
     string security = "SSPI"; 
     string sql = "select server from dbo.ServerList"; 
     SqlConnection con = new SqlConnection("Data Source=" + server + ";Initial Catalog=" + database + ";Integrated Security=" + security); 
     con.Open(); 
     SqlCommand cmd = new SqlCommand(sql, con); 
     SqlDataReader dr = cmd.ExecuteReader(); 
     while(dr.Read()) 
     { 
//this below doesnt work because it can't find the comboBox 
      comboBox1.Items.Add(dr[0].ToString()); 
//the rest of the code works fine 
     } 
     con.Close(); 
    } 
関連する問題