如何使用DropDownList绑定Sql server数据库

如何使用dropdownlist绑定数据库?首先我们要了解一些dropdownlist的一些属性。

DataSource:为数据源

DataBind:数据源绑定

DataTextField:要显示的文本值

DataValueField:显示文本值的编号

其中DataTextField主要是给用户看的,我们程序员要用的是DataValueField。

下面是代码展示:

这里我就建立了一个部门表插入了三条数据:

 go
 create table Department
 (
DepId    int primary key identity(1,1),
DepName    nvarchar(50)
 )
 insert into Department values('人事部')
 insert into Department values('开发部')

 insert into Department values('行政部')

这是个人的DBHelper类:(要引用两个命名空间:using System.Data;
using System.Data.SqlClient;)

public class DBHelper
    {
        static string Conns = "database=EmployeeSys;uid=sa;pwd=123456;server=.";
        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable Table(string sql)
        {
            using (SqlConnection conn = new SqlConnection(Conns))
            {
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
        }
        /// <summary>
        /// 执行增删改
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static bool InsertOrDelete(string sql)
        {
            using (SqlConnection conn = new SqlConnection(Conns))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                return cmd.ExecuteNonQuery() > 0;
            }
        }
    }

下面是进行dropdownlist数据绑定:

 

 string sql = string.Format(" select * from Department");
                DropDownList1.DataSource = DBHelper.Table(sql);
                DropDownList1.DataTextField = "DepName";
                DropDownList1.DataValueField = "DepId";
                DropDownList1.DataBind();

 

运行结果

如何获得我选中的那一项的ID值呢?

  int id=int.Parse(this.DropDownList1.SelectedItem.Value);

好了,这就是我对dropdownlist一些小小的见解,如果不喜欢的看看就好,再次谢谢各位了,希望能给你带来一些帮助。

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要为dropdownlist数据库中的数据,可以按照以下步骤操作: 1. 在页面中添加一个dropdownlist控件。 2. 在页面的代码-behind中编写代码来连接数据库并查询需要的数据。比如使用ADO.NET中的SqlConnection和SqlCommand类来连接数据库并执行查询操作。 3. 将查询结果dropdownlist控件上。可以使用dropdownlist控件的DataSource属性来指数据源,使用DataTextField和DataValueField属性来指要显示的文本和值的字段名,最后调用DataBind方法来完成。 以下是一个简单的示例代码: ```c# protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string connectionString = "Data Source=your_server;Initial Catalog=your_database;User ID=your_username;Password=your_password"; string query = "SELECT id, name FROM your_table"; SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(query, connection); SqlDataAdapter adapter = new SqlDataAdapter(command); DataSet dataSet = new DataSet(); adapter.Fill(dataSet, "your_table"); dropdownlist.DataSource = dataSet.Tables["your_table"]; dropdownlist.DataTextField = "name"; dropdownlist.DataValueField = "id"; dropdownlist.DataBind(); } } ``` 在这个示例中,我们从名为your_table的表中查询了id和name两个字段的数据,并将其到了名为dropdownlistdropdownlist控件上。其中,数据库连接字符串、查询语句、数据源字段名和值字段名需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值