zoukankan      html  css  js  c++  java
  • 下拉框数据绑定两种方式

    1、利用cs包,DataAccess.cs

    #region
    string m_str = @"select sname from service group by sname";
    DataTable m_dt = DataAccess.DBHelper.GetList(m_str);
    DropDownList2.DataValueField = "sname";
    DropDownList2.DataTextField = "sname";
    绑定数据源
    this.DropDownList2.DataSource = m_dt;
    DropDownList2.DataBind();
    
    DropDownList2.Items.Insert(0, new ListItem("请选择", ""));
    #endregion

    而在web.config中

    <appSettings>
        <!--数据库连接字符串-->
        <add key="DBConnString" value="Data Source=localhost;database=manager;uid=sa;pwd=sa;" />
      </appSettings>

    2、

    SqlConnection conn = new SqlConnection("Data Source = localhost; database = manager; uid=sa; pwd=sa;");
            conn.Open();
    
            //任务类型绑定
            #region
            SqlDataAdapter sdt = new SqlDataAdapter("select sname from service group by sname", conn);
    
            DataSet dt = new DataSet();
            sdt.Fill(dt);
            DropDownList2.DataValueField = "sname";
            DropDownList2.DataTextField = "sname";
            this.DropDownList2.DataSource = dt;
            DropDownList2.DataBind();
    
            DropDownList2.Items.Insert(0, new ListItem("请选择", ""));
            #endregion
            conn.Close();

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    linux基础
    sublime、Typora
    Windows cmd命令
    idea打包java可执行jar包
    idea常用快捷键
    Linux入门学习笔记1:VI常用命令
    442. Find All Duplicates in an Array
    566. Reshape the Matrix
    766. Toeplitz Matrix
    561. Array Partition I
  • 原文地址:https://www.cnblogs.com/PearlRan/p/4833063.html
Copyright © 2011-2022 走看看