随风而行

^o^ 格言:相信没有做不到的事情,只有想不到的事情.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

DropDownList控件的用法

Posted on 2009-02-24 14:19  随风而行  阅读(439)  评论(0编辑  收藏  举报
从数据库获取数据:
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();                  
}