DropDownList控件的用法

从数据库获取数据:
private void droppn()//连接Oracle数据库
{
        OracleConnection myconn=new OracleConnection(Rsofunction.GetOracleconn());
        myconn.Open();
        string str="select ftp_materiel from ftatable where ftp_workorder='"+drpWorkOrder.SelectedItem.Text.Trim()+"'";
        OracleCommand mycommand=new OracleCommand(str,myconn);
        OracleDataReader oqlr=null;
        try
        {
                oqlr=mycommand.ExecuteReader();
                drplist.Items.Clear();
                drplist.Items.Add("请选择料号");
                while(oqlr.Read())
                {
                        drplist.Items.Add(oqlr["ftp_materiel"].ToString ());
                }
        }
        catch(Exception err)
        {
                msg.Text=err.Message;
        }
        finally
        {
                myconn.Close();
                myconn.Dispose();
                mycommand.Dispose();
        }
}
 
private void BindDropDownList1()//连接SQL数据库,也可以连接Oracle数据库,数据库连接已封装在类中
{
        myConnection.Open();
        string strSQL = "";
        if (DropDownList1.Items.Count > 0)
                DropDownList1.Items.Clear();
                DropDownList1.Items.Add("Please Select");
                strSQL = "select distinct type from pe_moto_client";
                SqlCommand cmd = new SqlCommand(strSQL,myConnection);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                        DropDownList1.Items.Add(rd["type"].ToString().Trim()); 
                }
                rd.Close();
        myConnection.Close();                  
}
原文地址:https://www.cnblogs.com/lovewife/p/1397210.html