取得数据库字段的值,并赋给一个变量

SqlConnection myConnection = new SqlConnection("Data Source=ZL4306;Initial Catalog=kk;User ID=sa");//Data Source=这里是数据库服务器的主机名,Initial Catalog=这里是数据库名,ID=这里是登陆用户名
        SqlDataAdapter myCommand = new SqlDataAdapter("select dmlb,dmsm1,dmz from kk_code where dmlb='6' or dmlb='5' or dmlb='9' or dmlb='8' ", myConnection);//连接数据库得到SqlDataAdapter

        DataTable dt = new DataTable();
        myCommand.Fill(dt);
        //DataSet ds = new DataSet();
        //myCommand.Fill(ds, "dmsm1");//填充数据集

        DropDownList1.DataSource = dt;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string p = dt.Rows[i]["dmlb"].ToString();//取得数据库字段的值,并赋给一个变量
            Response.Write(p);
            if(p.Equals("5"))
            {
                Response.Write("OK");
                DropDownList2.DataTextField = "dmz";//设置文本显示字段
                DropDownList2.DataValueField = "dmz";//设置值显示
                DropDownList2.DataBind();
            }
        }

 

黑色头发  http://heisetoufa.iteye.com

你可能感兴趣的:(数据库)