C # winforms добавляет элементы из выпадающего списка в отдельный класс - PullRequest
0 голосов
/ 29 декабря 2010

У меня есть поле со списком в Form1.cs [Design], и я создал отдельный класс под названием SQLOperations для работы со своими SQL-компонентами. Как мне перейти в поле со списком?

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

Ответы [ 2 ]

2 голосов
/ 29 декабря 2010

Вместо того, чтобы связывать ваш пользовательский интерфейс и данные, почему бы вам просто не вернуть набор данных из вашего метода "SQLServerPull"? Пользовательский интерфейс может использовать эти необработанные данные любым удобным для него способом, например, заполняя ComboBox.

0 голосов
/ 29 декабря 2010

Почему бы не сделать это

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

Или вернуть 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;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...