首先在vs2005中引入using System.Data.SqlClient命名空间
/// 查询
/// </summary>
/// <returns></returns>
public DataTable Select()
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESSInitial Catalog=TestIntegrated Security=True")//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
conn.Open()
string sql = "select * from users"
SqlCommand cmd = new SqlCommand(sql, conn)
SqlDataAdapter sda = new SqlDataAdapter(cmd)
DataTable dt = new DataTable()
sda.Fill(dt)
你应该说的是winform中的下拉列表控件吧!如果这些选项是固定的,可以直接在控件的属性面板中找到Items属性中去添加选项;
如果需要代码添加,就可以使用 控件名.Items.Add("选项") 的语句来添加;
如果是数据源中取出来的数据,可以使用数据绑定的方式添加;
控件名.DataSource=数据源 的语句来添加选项。
可以用一个SqlDataReader 将数据库的信息加入到ComboBox控件中:SqlDataReader read=new SqlDataReader("select id from user")
if(read.hasrows)
{
while(read.Read())
commobox.items.add(read["id"].tostring())
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)