关于用 comboBox控件绑定数据库的问题
如题, 本人用的C#2003连接SQL, 在comboBox上遇到了问题, 在网上搜索了半天未果, 所以只有向各热心人士求助了
以下为本人的代码:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
         {
             if (comboBox1.Text.Trim() != "")
             {    
                 string connectionString = "Data Source=(local);Integrated Security=SSPI;" +
                     "Initial Catalog = test";   //用的是WINDOWS登陆认证,  Initial Catalog=后面的是要连接的数据库
                 SqlConnection myConnection = new SqlConnection(connectionString);
                 myConnection.Open();
                 string mySql = "select AreaName from Area";
                 SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySql, myConnection);    
                 DataSet ds = new DataSet();
                 mySqlDataAdapter.Fill(ds);                
                 comboBox1.DisplayMember = "AreaName";                           //想显示的字段
                 comboBox1.ValueMember = "AreaID";                              //显示字段对应的值,主键
                 comboBox1.DataSource = ds.Tables[0];                //数据集                                     
             }
         }
补充: 由于网上的例子大都是以ACCESS为对象的, 不知道ACCESS和SQL200的绑定方法是否有什么不同之处,  
所以以上的方法也是按照ACCESS的来写的, 但是运行虽然没有出错, 却在下拉后没有显示任何数据, 望热心人士指点
------解决方案--------------------
------解决方案--------------------private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
       {  
           if (comboBox1.Text.Trim() != "")  
           {      
               string connectionString = "Data Source=(local);Integrated Security=SSPI;" +  
                   "Initial Catalog = test";  //用的是WINDOWS登陆认证,  Initial Catalog=后面的是要连接的数据库  
               SqlConnection myConnection = new SqlConnection(connectionString);  
              // myConnection.Open(); 
               string mySql = "select AreaName,AreaID from Area"; 
                //查询二个字段               
              SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySql, myConnection); 
               DataTable ds = new DataTable();  
               //把上面的dataset改成datadatle试看
               mySqlDataAdapter.Fill(ds);              
               comboBox1.DisplayMember = "AreaName";                          //想显示的字段  
               comboBox1.ValueMember = "AreaID";                              //显示字段对应的值,主键  
               comboBox1.DataSource = ds;                //数据集
//直接改成表名                            
           }  
       }  
你用这个方法试试看,如果要是在不行的话,你就调试一下,看表中有没有数据
如果没有数据就是你连接字符串有问题,这个方法我以前有也用
//这是8楼的
private void Form1_Load(object sender, System.EventArgs e)  
{  
string sqlstr = "select dm,mc from table1";  
 //sqlCon.Open();   //sqlda