DropDownList 一个完整 实例

cs:
    DataTable dt = new CategoryManager().SelectAll();
                DropDownList1.DataSource = dt;
                DropDownList1.DataTextField = "name"; 
                //将categor表的name字段绑上。
                DropDownList1.DataValueField = "id";
                DropDownList1.DataBind();

bll:
    private CategoryDAO cdao = null;
        public CategoryManager()
       {
           cdao = new CategoryDAO();
       }
     ///按照新闻类别名,取出这个“类别名”的所有新闻
        public DataTable SelectAll()
        {
            return cdao.SelectAll();
        }


DAO:
        ///按照新闻类别名,取出这个“类别名”的所有新闻
        public DataTable SelectAll()
        {
            DataTable dt = new DataTable();
            string sql = "select * from category";
            dt = sqlhelper.ExecuteQuery(sql, CommandType.Text);
            return dt;
        }

DAO中的sqlhelp:      
        /// 方法执行传入的SQL语句(查询)
        public DataTable ExecuteQuery(string cmdText, CommandType ct)
        {
            DataTable dt = new DataTable();
            cmd = new SqlCommand(cmdText, GetConn());
            cmd.CommandType = ct;
      using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
             {
                 dt.Load(sdr);
             }
             return dt;
        }


获得DropDownList1中“ DropDownList1.DataValueField = "id";”的值:
string caid = DropDownList1.SelectedValue;

你可能感兴趣的:(DAO,sql)