下拉列表与SQL数据库绑定 [英] dropdown list binding with sql database

查看:87
本文介绍了下拉列表与SQL数据库绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在断开连接的模式下绑定下拉列表

how to bind dropdownlist in disconnected mode

推荐答案

DataTable table = new DataTable();
           cmd = new SqlCommand();
           cmd.CommandText = "GetEmployee";
           cmd.CommandType = CommandType.StoredProcedure;

           table = dbConn.ExecuteQuery(cmd);
           ddlEmp.DataSource = table;
           ddlEmp.DataValueField = "DeptId";
           ddlEmp.DataTextField = "DeptName";
           ddlEmp.DataBind();


try
{
sqlconnection con = new sqlconnection(数据源= .;初始目录=数据库名称; uid = sa; pwd =密码");
con.Open();
sqlcommand cmd = new OleDbCommand(从购买中选择与众不同(prod_Name)",con);
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
comboBox1.Items.Add(dr [0] .ToString());
}
dr.Close();
con.Close();
}
catch(ex ex例外)
{
MessageBox.Show("Error ...." + ex.Message);
}
try
{
sqlconnection con=new sqlconnection("Data Source=.; Initial Catalog=database name;uid=sa; pwd=password");
con.Open();
sqlcommand cmd = new OleDbCommand("select distinct(prod_Name) from purchase" , con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr[0].ToString());
}
dr.Close();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error...."+ex.Message);
}


SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=cmcdb;Integrated Security=True");
        con.Open();
       SqlCommand cmd = new SqlCommand("select userid from logins ", con);
       SqlDataReader dr = cmd.ExecuteReader();
       while (dr.Read())
       {

           DropDownList1.Items.Add(dr["userid"].ToString());
       }


这篇关于下拉列表与SQL数据库绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆