且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Combobox与C#代码(正确的方式)

更新时间:2023-02-07 20:46:42

没关系,它有效。但是......连接和命令是稀缺资源,当你完成它们时应该是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.