将表加载到组合框 [英] load table to combobox

查看:56
本文介绍了将表加载到组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..

i知道如何将数据从sql加载到组合框架我没有问题但是我的问题是我觉得我的方法不正确你必须做很多每个组合的公共类*方法* ...

hi everyone ..
i know how to load data from sql to combobox i haven't problem with that but my problem is i don't feel mine is right way that u have to make many public class *method* for every combobx...

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
             load1();
            load2();
        }
        SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=datatest;Integrated Security=True");

public void load()
        {
            string ss = @"select personnam from tblcustomres ";
            //string sb = @"select items from tblitems";
            //SqlCommand cmd = new SqlCommand(@"select personnam from tblcustomres ", cn);
            SqlCommand cmd = new SqlCommand(ss,cn); 
            SqlDataReader dr;
            cn.Open();
            dr = cmd.ExecuteReader();
            while(dr.Read())
            {
                      string smd1 = (string)dr["personnam"].ToString();
                comboBox1.Items.Add(smd1);
            }
            cn.Close();
        }
        public void load1()
        {
            SqlCommand cmd = new SqlCommand(@"select items from tblitems", cn);
            SqlDataReader dr;
            cn.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {

                string smd1 = (string)dr["items"].ToString();
                comboBox2.Items.Add(smd1);

            }
            cn.Close();
public void load2()
        {
            SqlCommand cmd = new SqlCommand(@"select location from tblloc", cn);
            SqlDataReader dr;
            cn.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {

                string smd1 = (string)dr["location"].ToString();
                comboBox3.Items.Add(smd1);

            }
            cn.Close();
        }
        }



任何答案都非常值得赞赏......


any answers well be so appreciated...

推荐答案

你的假设是正确的,有可能以更好的方式做到这一点,而不必为每个组合框重复几乎相同的代码。您必须确定它们共有哪些部件以及哪些部件不同。然后,您可以使用随后使用方法参数提供的变量替换不同的部分。我在这里为你做过这件事;我还改进了其他一些方面:

- 本地使用块中的SqlConnection,而非作为类成员

- 删除了重复的打开和关闭连接

- 使用块中的SqlCommand和SqlDataReader

- 删除了行单元的不必要的转换



You're right with your assumption, it's possible to do this in a better way, without repeating almost the same code for each combobox. You have to identify which parts they have in common and which parts are different. Then you replace the differing parts by variables that you then provide with method-parameters instead. I've done this for you here; also I've improved some other aspects:
- the SqlConnection in a local using-block, not as a class-member
- removed the repeated opening and closing of the connection
- the SqlCommand and SqlDataReader in a using-block
- removed unneccessary casting of the row-cell

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=datatest;Integrated Security=True"))
        {
            conn.Open();

            FillComboBox(conn, "tblcustomres", "personnam", comboBox1);
            FillComboBox(conn, "tblitems", "items", comboBox2);
            FillComboBox(conn, "tblloc", "location", comboBox3);

        } // the connection will be closed here automatically
    }

    public void FillComboBox(SqlConnection conn, string table, string column, ComboBox comboBox)
    {
        string query = String.Format("SELECT {0} FROM {1};", column, table);

        using (SqlCommand cmd = new SqlCommand(query, conn))
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                string item = dr[column].ToString();
                comboBox.Items.Add(item);
            }
        }
    }
}



我还建议你将你的组合框重命名为cbPerson, cbItems,cbLocation以及同样地,给予任何其他控件和变量有意义的名称,这将使您更容易理解您的代码,即使是对于您自己,尤其是在几周之后再回到它。


I would also recommend you to rename your comboboxes something like "cbPerson", "cbItems", "cbLocation" and likewise, to give any other controls and variables meaningful names which will make it much easier to understand your code, even for yourself, especially when getting back to it after some weeks.


这篇关于将表加载到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆