当前位置:编程学习 > C#/ASP.NET >>

下拉框刷数据源 DAtatable

 private void Form1_Load(object sender, EventArgs e)
        {
            string connstr = "server=(local);uid=sa;pwd=ccxx;database=master";
            string sql = "select id,班级 from 班级1";
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                try
                {
                    SqlCommand com = new SqlCommand();
                    SqlDataAdapter da = new SqlDataAdapter(sql, conn);

                    DataTable dt = new DataTable();
                    da.SelectCommand = com;
                    da.SelectCommand.CommandType = CommandType.Text;
                    da.SelectCommand.CommandText = sql;
                    da.Fill(dt);    comboBox1.DataSource = dt;
                    comboBox1.DisplayMember ="班级";
                    comboBox1.ValueMember = "id";


                }
                catch (Exception ex)
                {

                    //throw (new SqlException(ex.Message));
                }

数据刷不出来 不知道怎么了  高人指点  要求必须用datatable因为我练习这个  dataset我已经会了
--------------------编程问答--------------------

        private void Form1_Load(object sender, EventArgs e) 
        { 
            string connstr = "server=(local);uid=sa;pwd=ccxx;database=master"; 
            string sql = "select id,班级 from 班级1"; 
            using (SqlConnection conn = new SqlConnection(connstr)) 
            { 
                //conn.Open(); 
                try 
                { 
                    //SqlCommand com = new SqlCommand(); 
                    SqlDataAdapter da = new SqlDataAdapter(sql, conn); 

                    DataTable dt = new DataTable(); 
                    //da.SelectCommand = com; 
                    //da.SelectCommand.CommandType = CommandType.Text; 
                    //da.SelectCommand.CommandText = sql; 
                    da.Fill(dt);    comboBox1.DataSource = dt; 
                    comboBox1.DisplayMember ="班级"; 
                    comboBox1.ValueMember = "id"; 


                } 
                catch (Exception ex) 
                { 

                    //throw (new SqlException(ex.Message)); 
                } 


改成上面的,你再试试看?
--------------------编程问答--------------------
                    conn.Open();
                    SqlCommand com = new SqlCommand();
                    SqlDataAdapter da = new SqlDataAdapter();

                    DataTable dt = new DataTable();
               
                    com.CommandType = CommandType.Text;
                    com.CommandText = sql;
                    com.Connection = conn;
                    da.SelectCommand = com;
                  
                    da.Fill(dt);
                 
                   // DataSet ds = new DataSet();

                    //da.Fill(ds, "table");
                    
                    comboBox1.DataSource = dt;
                    comboBox1.DisplayMember ="班级";
                    comboBox1.ValueMember = "id";


这样也可以 你的也可以 谢谢啦  结贴
--------------------编程问答-------------------- 学习~ --------------------编程问答-------------------- 如果你是用在后面刷新的话,建议你把他封装成一个方法好点,这样你要用的话直接调用就可以了!
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,