Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#winforms将组合框中的项目添加到单独类中_C#_Winforms - Fatal编程技术网

C#winforms将组合框中的项目添加到单独类中

C#winforms将组合框中的项目添加到单独类中,c#,winforms,C#,Winforms,我拥有的是Form1.cs[Design]上的combobox,我创建了一个名为SQLOperations的单独类来操作我的SQL内容,如何传递到combobox public static void SQLServerPull() { string server = "p"; string database = "IBpiopalog"; string security = "SSPI"; string sql = "s

我拥有的是Form1.cs[Design]上的combobox,我创建了一个名为SQLOperations的单独类来操作我的SQL内容,如何传递到combobox

 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();
    }
为什么不这样做呢

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和数据,不如直接从“SQLServerPull”方法返回一组数据?UI可以以它认为合适的任何方式使用原始数据,例如,填充组合框。

为什么不让您的方法返回列表或字符串数组,然后从中填充组合框?我想我可以,但确实没有直接传递它的方法?当然有,但您真的想将数据访问层与UI类型耦合起来吗?
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;
    }