zoukankan      html  css  js  c++  java
  • Combox绑定数据/带查询功能

      private void button3_Click(object sender, EventArgs e)
            {
                string sql = "select dname,did from department";

                SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=hem09;User ID=sa;Password=123456");
                try
                {
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    conn.Open();
                    SqlDataReader reader= cmd.ExecuteReader();

                    //将数据绑定到列表框中
                    cboList.Items.Clear();
                    //通过循环读取每一行的数据
                    while (reader.Read())//延迟执行
                    {
                        //使用索引器读取数据
                        //reader[1]
                        //reader["dname"]
                        //使用方法的方式读取,不需要进行类型转换
                        string dname=reader.GetString(0);//参数为列的索引
                        //注意:这里的索引,不是根据表的结构来确定的,而是根据select子句的结果集决定的
                        cboList.Items.Add(dname);

                       cboList.AutoCompleteMode = AutoCompleteMode.SuggestAppend;//查询功能
                           cboList.AutoCompleteSource = AutoCompleteSource.ListItems;//查询功能
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    //cmd.CommandText = "";
                }
                catch
                {
                    //...
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                }
            }

      private void cbTitle_SelectedIndexChanged(object sender, EventArgs e)
            {
                textBox1.Text = cbTitle.SelectedItem.ToString();//下拉框值传入其它组件中
                
            }

            private void button4_Click(object sender, EventArgs e)
            {
                string sql = "select dname,did from department";
                using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=hem09;user id=sa;Password=123456"))
                {
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    conn.Open();//尽量晚的打开连接,尽量早的关闭连接
                    SqlDataReader reader = cmd.ExecuteReader();

                    //将数据绑定到列表
                    cboList.Items.Clear();
                    while (reader.Read())
                    {
                        Department d1 = new Department() {Did=reader.GetInt32(1),Dname=reader.GetString(0)};
                        cboList.Items.Add(d1);
                        cboList.DisplayMember = "Dname";//指定对象的某个属性用于显示
                    }
                    reader.Close();
                    reader.Dispose();
                }
            }

            private void button5_Click(object sender, EventArgs e)
            {
                //1、构造sql语句
                string sql = "select * from department";
                //2建立连接
                using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=hem09;User id=sa;Password=123456"))
                {
                    //3.1构造命令对象
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    //2.1打开连接
                    conn.Open();
                    //3.2执行命令
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        //清空原有数据
                        gvList.Rows.Clear();
                        gvList.Columns.Clear();
                        //新建列
                        gvList.Columns.Add("did","编号");
                        gvList.Columns.Add("dname", "名称");
                        //遍历查询结果集,将数据添加到列表中
                        while (reader.Read())
                        {
                            Department d1 = new Department() { Did = reader.GetInt32(0), Dname = reader.GetString(1) };
                            gvList.Rows.Add(d1.Did, d1.Dname);
                        }
                    }
                    cmd.Dispose();
                }
            }

              
           

  • 相关阅读:
    HTTP断点续传 规格严格
    Java Shutdown 规格严格
    linux 命令源码 规格严格
    JTable调整列宽 规格严格
    linux 多CPU 规格严格
    Hello can not find git path 规格严格
    Kill 规格严格
    拜拜牛人 规格严格
    Swing 规格严格
    Debugging hangs in JVM (on AIX but methodology applicable to other platforms) 规格严格
  • 原文地址:https://www.cnblogs.com/haimingkaifa/p/5361440.html
Copyright © 2011-2022 走看看