2010-12-29 15 views
0

Was ich habe, ist meine Combobox auf Form1.cs [Design], und ich schuf eine separate Klasse namens SQLOperations meine SQL-Zeug zu betreiben, wie übergebe ich an die Combobox ?C# Winforms Hinzufügen von Elementen aus einer Combobox zu einer separaten Klasse

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(); 
    } 
+0

Warum gibt Ihre Methode kein List- oder String-Array zurück und füllt die Combobox daraus? – Tester101

+0

Ich denke, ich könnte, aber es gibt wirklich keine Möglichkeit, es direkt zu übergeben? –

+2

Natürlich gibt es das, aber möchten Sie Ihre Datenzugriffsebene wirklich an UI-Typen koppeln? – Oded

Antwort

-1

, warum dies nicht tun

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; 
    } 

Oder Rückkehr eine 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; 
    } 
2

Statt Ihre UI und Daten zu koppeln, warum Sie nicht einfach eine Reihe von Daten zurückgeben von Ihrem "SQLServerPull" -Methode? Die Benutzungsoberfläche kann diese Rohdaten in einer beliebigen Weise verwenden, wie zum Beispiel das Füllen einer ComboBox.

Verwandte Themen