# combox绑定oracle查询的数据集(列)
上一篇 /下一篇 2012-02-28 19:03:17 / 个人分类:c#
private void Form1_Load(object sender, EventArgs e)
{
OracleConnection con1 = new OracleConnection(" Data Source=orcl;User id=scott; Password=system;");//oracleconnection类隶属于命名空间system.data.oracleclient
OracleCommand oc1 = new OracleCommand("select loc as newloc from dept", con1);
OracleDataAdapter da = new OracleDataAdapter(oc1);
DataSet ds1 = new DataSet();
oda.Fill(ds1);
//dataGridView1.DataSource = ds1.Tables[0];
统计出列表中记录条数 tables.rows.count
//this.Text = ds1.Tables[0].Rows.Count.ToString();
//for (int i=0; i < ds1.Tables[0].Rows.Count; i++)
//{
// //向 combox添加子项items.add,dataset.tables.rows[行索引][列名字],此列名字对应oraclecommand中 sql的列名
// comboBox1.Items.Add(ds1.Tables[0].Rows[i]["newloc"]);
//}
//comboBox1.DataSource = ds1.Tables[0].Columns["newloc"];
//combox绑定 数据库表查询数据集不用定位到每个列,和操作datagridview一样,定位到tables即可
comboBox1.DataSource = ds1.Tables[0];
//displaymember是展现给客户看的
comboBox1.DisplayMember = "居住地";
//valuemember为真正数据库中的列(或者sql的列或别名)
comboBox1.ValueMember = "newloc";
}
{
OracleConnection con1 = new OracleConnection(" Data Source=orcl;User id=scott; Password=system;");//oracleconnection类隶属于命名空间system.data.oracleclient
OracleCommand oc1 = new OracleCommand("select loc as newloc from dept", con1);
OracleDataAdapter da = new OracleDataAdapter(oc1);
DataSet ds1 = new DataSet();
oda.Fill(ds1);
//dataGridView1.DataSource = ds1.Tables[0];
统计出列表中记录条数 tables.rows.count
//this.Text = ds1.Tables[0].Rows.Count.ToString();
//for (int i=0; i < ds1.Tables[0].Rows.Count; i++)
//{
// //向 combox添加子项items.add,dataset.tables.rows[行索引][列名字],此列名字对应oraclecommand中 sql的列名
// comboBox1.Items.Add(ds1.Tables[0].Rows[i]["newloc"]);
//}
//comboBox1.DataSource = ds1.Tables[0].Columns["newloc"];
//combox绑定 数据库表查询数据集不用定位到每个列,和操作datagridview一样,定位到tables即可
comboBox1.DataSource = ds1.Tables[0];
//displaymember是展现给客户看的
comboBox1.DisplayMember = "居住地";
//valuemember为真正数据库中的列(或者sql的列或别名)
comboBox1.ValueMember = "newloc";
}
private void loadns()
{
try
{
SqlConnection con = Db.DbLink();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = selns;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
this.comboBox3.Items.AddRange(new object[] { reader["nsmc"] });
}
reader.Close();
con.Close();
}
catch
{
}
finally {
}
}
//加载数据到datagrivew
private void ref3()
{
SqlConnection con = Db.DbLink();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(selnr, con);
adp.Fill(ds, "dt");
BindingSource bds = new BindingSource(ds, "dt");
this.dataGridView1.DataSource = bds;
}