日期:2014-05-19  浏览次数:20519 次

怎样向控件中插入数据?
假如在窗体中引用了一个下拉控件,我想用sql向这个下拉控件中填入一些数据,比如:1,2,3,4,5,但是这些数据不是从数据库中查询得来的,而是要用sql语句直接添加,请问改怎么做?

------解决方案--------------------
SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
------解决方案--------------------
我用C#写的~基本是CONNECTION对象来连接数据库,dataAdapter对象来执行查询,dataset来装结果集,再把控件的datasource属性与DS对象绑定就可以了~~

SqlConnection sqlcon = new SqlConnection();

SqlDataAdapter sqlda = new SqlDataAdapter();

DataSet ds = new DataSet();

sqlcon.ConnectionString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=true ";

sqlcon.Open();

SqlDataAdapter sqlda = new SqlDataAdapter( "select * from products ", sqlcon);

sqlda.Fill(ds);

dataGrid1.CaptionText = "Table-Products ";

dataGrid1.DataSource = ds.Tables[0];
------解决方案--------------------
SqlDataAdapter sqlda = new SqlDataAdapter( "select * from products ", sqlcon);
把( "select * from products ",)改了就可以执行不同的查询,
比如选择了一个COMBO的值就可以有对应的事件发生,捕获事件后将查询语句赋给一个字符串变量,在把上面的DA对象给的查询条件改成新的查询OK了~~