2010-12-29 4 views
0

내가 가지고있는 것은 Form1.cs의 [디자인]에 내 콤보 상자입니다. SQL 물건을 조작하기 위해 SQLOperations라고하는 별도의 클래스를 만들었는데 어떻게 콤보 상자에 전달합니까? ?C# winforms 콤보 상자의 항목을 별도의 클래스에서 추가하려면

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

왜 메서드를 사용하여 List 또는 문자열 배열을 반환하지 않았습니까? 그런 다음 콤보 상자를 채 웁니다. – Tester101

+0

내가 할 수있을 것 같긴하지만 실제로 직접 전달할 수있는 방법이 없다. –

+2

물론 데이터 액세스 레이어를 UI 유형에 연결하고 싶습니까? – Oded

답변

-1

왜이

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

을 대신 반환하지, 왜 당신은 단순히 데이터의 집합을 반환하지 않는 당신의 "SQLServerPull"방법? UI는 ComboBox를 채우는 것과 같이 필 요한 방식으로 원시 데이터를 사용할 수 있습니다.

관련 문제