Combobox与C#代码(正确的方式) [英] Combobox with C# code (the correct way )

查看:76
本文介绍了Combobox与C#代码(正确的方式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个代码并且它很适合我回合我要求的是这是编码它的好方法(我用它来从表中获取数据以将它与另一个组合框链接以使它们一起工作)。 />


我尝试过:



第一个组合框



i us this code and it work good with me bout what im asking for is this the good way to code it ( I use it to get data from table to link it with another combobox to make them work together).

What I have tried:

the first combobox

public Needform()
    {
        string mainconn = ConfigurationManager.ConnectionStrings["MY"].ConnectionString;
        SqlConnection sqlconn = new SqlConnection(mainconn);
        string sqlquery = "select * from [dbo].[Governorate]";
        SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
        sqlconn.Open();
        SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        comboBoxGovernorate.ValueMember = "id_Governorate";
        comboBoxGovernorate.DisplayMember = "name";
        comboBoxGovernorate.DataSource = dt;
        comboBoxCity.Enabled = false;
  }







另一个






the another one

private void ComboBoxGovernorate_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBoxGovernorate.SelectedValue.ToString() != null)
        {
            string mainconn = ConfigurationManager.ConnectionStrings["MY"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(mainconn);
            string sqlquery = "select * from [dbo].[City]where id_Governorate=@id_Governorate";
            SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
            sqlconn.Open();
            sqlcomm.Parameters.AddWithValue("@id_Governorate", comboBoxGovernorate.SelectedValue.ToString());
            SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            comboBoxCity.ValueMember = "id";
            comboBoxCity.DisplayMember = "city";
            comboBoxCity.DataSource = dt;
            comboBoxCity.Enabled = true;
        }
    }





thx我希望所有人都能从中学习

推荐答案

没关系,它有效。但是......连接和命令是稀缺资源,当你完成它们时应该是Disposed。最简单的方法是使用使用块:

It's ok, it works. But ... connections and commands are scarce resources and should be Disposed when you have finished with them. The easiest way is to use a using block:
public void Needform()
    {
    string mainconn = ConfigurationManager.ConnectionStrings["MY"].ConnectionString;
    using (SqlConnection sqlconn = new SqlConnection(mainconn))
        {
        sqlconn.Open();
        string sqlquery = "SELECT * FROM [dbo].[Governorate]";
        using (SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn))
            {
            using (SqlDataAdapter sda = new SqlDataAdapter(sqlcomm))
                {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                comboBoxGovernorate.ValueMember = "id_Governorate";
                comboBoxGovernorate.DisplayMember = "name";
                comboBoxGovernorate.DataSource = dt;
                comboBoxCity.Enabled = false;
                }
            }
        }
    }

private void ComboBoxGovernorate_SelectedIndexChanged(object sender, EventArgs e)
    {
    if (comboBoxGovernorate.SelectedValue.ToString() != null)
        {
        string mainconn = ConfigurationManager.ConnectionStrings["MY"].ConnectionString;
        using (SqlConnection sqlconn = new SqlConnection(mainconn))
            {
            sqlconn.Open();
            string sqlquery = "SELECT * FROM [dbo].[City] WHERE id_Governorate=@id_Governorate";
            using (SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn))
                {
                sqlcomm.Parameters.AddWithValue("@id_Governorate", comboBoxGovernorate.SelectedValue.ToString());
                using (SqlDataAdapter sda = new SqlDataAdapter(sqlcomm))
                    {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    comboBoxCity.ValueMember = "id";
                    comboBoxCity.DisplayMember = "city";
                    comboBoxCity.DataSource = dt;
                    comboBoxCity.Enabled = true;
                    }
                }
            }
        }
    }

我建议另外两项更改:

1)只是一个样式的东西,但是对SQL关键字使用UPPERCASE - 它可以更容易在命令中找到它们。

2)不要做SELECT * FROM根本 - 总是命名你需要获取的列。这样,您不会浪费带宽获取您不会使用的数据(例如,如果您添加图像可能很重要),并且它有助于保护您免受数据库布局更改,因为您控制了其中的顺序他们被退回。例如,在您稍后更新内容时,这会对您的应用产生重大影响。

There are two other changes I'd recommend:
1) Just a "style" thing, but use UPPERCASE for SQL keywords - it makes it easier to "find" them in a command.
2) Don't do "SELECT * FROM" at all - always name the columns you need to fetch. That way, you don't waste bandwidth fetching data you aren't going to use (and if you add images for example that can be significant), and it helps "protect" you from DB layout changes since you control the order in which they are returned. With tables for example, that can make a big difference to your app when you update things later.


这篇关于Combobox与C#代码(正确的方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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