c#链接sql,如何获取一部分列名,并讲其中三列的数据存放到三个combox中啊????急,求各位帮帮忙,最好给些代码让我看一下,这些东西还不太熟悉

解决方案 »

  1.   

    combox可以直接添加数据源,然后将他们一一对应就可以了。
      

  2.   

    select [username],[password],[age],[score] from [MyTable] 
               0          1        2      3 
    索引里可以改
    然后 用OleDbDataReader 读出来就OK了! 
      

  3.   

    LZ的意思是做个三级联动comboBox?
      

  4.   


     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            SqlDataAdapter da;
            DataSet ds;
            SqlConnection cn = new SqlConnection(@"Data Source=gundam-pc;Initial Catalog=图书管理系统;User ID=sa;Password=");
            private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.Items.Clear();
                comboBox2.Items.Clear();
                comboBox3.Items.Clear();
                SqlConnection cn = new SqlConnection(@"Data Source=gundam-pc;Initial Catalog=图书管理系统;User ID=sa;Password=");
                cn.Open();
                string strsql="select * from 图书信息表 ";
               // SqlCommand cmd = new SqlCommand(strsql, cn);
                SqlDataAdapter da= new SqlDataAdapter(strsql,cn);
                 ds = new DataSet();
                da.Fill(ds, "Books");
                for (int i = 0; i < ds.Tables["Books"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["Books"].Rows[i];
                    comboBox1.Items.Add(dr["类型"].ToString());
                }
              
                  
               
               
            }
     //第一个 comboBox 触发第二个 comboBox        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                cn.Open();
                string strsql1 = "select * from 图书信息表 where  类型= '" + comboBox1.SelectedItem.ToString() + "'  ";
                da = new SqlDataAdapter(strsql1, cn);
                ds = new DataSet();
                da.Fill(ds, "BookName");
                for (int i = 0; i < ds.Tables["BookName"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["BookName"].Rows[i];
                    comboBox2.Items.Add(dr["书名"].ToString());
                }
                cn.Close();
            }
      //原理同上
            private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
            {
                cn.Open();
                string strsql2 = "select * from 图书信息表 where  书名= '" + comboBox2.SelectedItem.ToString() + "'  ";
                da = new SqlDataAdapter(strsql2, cn);
                ds = new DataSet();
                da.Fill(ds, "Price");
                for (int i = 0; i < ds.Tables["Price"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["Price"].Rows[i];
                    comboBox3.Items.Add(dr["定价"].ToString());
                }
                cn.Close();
            }       
        }看看是否合适LZ
      

  5.   

    用数据阅读器可以获取列名,占用资源极小:
    SqlConnection cn=new SqlConnection("连接字符串");
    SqlCommand cmd=new SqlCommand("select * from 学生信息",cn);
    cn.Open();
    SqlDataReader dr=cmd.ExecuteReader();
    for (int i = 0; i < dr.FieldCount; i++)
    Response.Write(dr.GetName(i) + "<br/>");
    dr.Close();
    cn.Close();