我吧一条记录读取到dataset中,需要提取几个字段的值,请问怎么写法?

解决方案 »

  1.   

    DataRow dr = ds.Tables[0].Rows[0];string contents=dr["c"].ToString();
      

  2.   


    //Rows[0]行号
    //["ID"]你的字段
    Response.Write(dt.Rows[0]["ID"].ToString());
      

  3.   


    //Tables[0]你的Datatable索引,默认从0开始算,如果有指定DataTable名,也可以换成那个名字
    //Rows[0]行号
    //["ID"]字段
    ds.Tables[0].Rows[0]["ID"]
      

  4.   

     protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            _connstring = WebConfigurationManager.ConnectionStrings["proconnectionstring"].ConnectionString;
            SqlConnection con = new SqlConnection(_connstring);
            //得到配置连接
           
            //得到参数
            int proid = 1;
            if (Request.QueryString["id"] != null)
            {
                proid = int.Parse(Request.QueryString["proid"].ToString());
               
            }
            //传递参数查询数据
           SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select *from pro where proid=@proid";
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@proid", proid);   /*        SqlCommand cmd = new SqlCommand("select * from pro where proid="+proid,con);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet dst = new DataSet();
            sda.Fill(dst);*/     SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet dst = new DataSet();
            using (con)
            {
                sda.Fill(dst);
            }
            FormView1.DataSource = dst;
            FormView1.DataBind();       /* 
            SqlCommand cmand = new SqlCommand("select * from test where id=" + id, con);
            cmand.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmand);
            DataSet ds = new DataSet();
            da.Fill(ds); 
         */     //  this.div11.InnerText = ds.Tables[0].Rows[0]["title"].ToString();
           // this.divcontent.InnerText = ds.Tables[0].Rows[0]["content"].ToString();
            Page.Header.Title = dst.Tables[0].Rows[0]["pagetitle"].ToString();
            HtmlMeta metaDesc = new HtmlMeta();
            metaDesc.Name = "DESCRIPTION";
            metaDesc.Content = dst.Tables[0].Rows[0]["pagedescriptions"].ToString();        HtmlMeta metakeywords = new HtmlMeta();
            metakeywords.Name = "KEYWORDS";
            metakeywords.Content = dst.Tables[0].Rows[0]["pagekeywords"].ToString();        HtmlHead head = (HtmlHead)Page.Header;
            head.Controls.Add(metaDesc);
            head.Controls.Add(metakeywords);
           
        }只是一条记录啊,根据记录的ID,怎么从page_load 事件中读取呢?我上面的写法有错
      

  5.   

    //如果是一条记录的话可以这样DataRow dr = ds.Tables[0].Rows[0];
    string contents=dr["列的名称"].ToString();如果是多条的话,要循环这个DataTable或者是DataSet,取得的方式是一样的
      

  6.   

     private string _connstring;
        protected void Page_Load(object sender, EventArgs e)
        {
            _connstring = WebConfigurationManager.ConnectionStrings["proconnectionstring"].ConnectionString;
            SqlConnection con = new SqlConnection(_connstring);
          
            //取得连接中的proid值
            int proid=-1;
            if (Request.QueryString["proid"] != null)
            {
              proid = int.Parse(Request.QueryString["proid"].ToString());
            }
            else
            {
                Response.Write("ID不能为空!");
                Response.End(); 
            }
          
            //通过proid 值查找对应记录,绑定到dataset        SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select *from pro where proid=@proid";
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@proid", proid);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet dst = new DataSet();
            using (con)
            {
                sda.Fill(dst);
            }        // 读取dataset中的2个字节,赋值给变量,设定dropdown1 和dropdown2 选定值为对应的值
            DataRow dr = dst.Tables[0].Rows[0];
            string dropdown1text = dr["categoryname"].ToString();
            string dropdown2text = dr["brandname"].ToString();        DropDownList drop1 = FormView1.FindControl("DropDownList1") as DropDownList;
                
                drop1.SelectedValue=dropdown1text; // 这个地方怎么做,出问题了
            DropDownList drop2 = FormView1.FindControl("DropDownList2") as DropDownList;
            drop2.SelectedValue = dropdown2text;        // 绑定到formview1 控件        FormView1.DataSource = dst;
            FormView1.DataBind();    
        }
      

  7.   

    datasetds.Tables[0].Rows[0]["列名"].ToString();这样获取