How to fill Dataset with multiple tables?

前端 未结 8 705
小鲜肉
小鲜肉 2020-11-29 07:20

I\'m trying to fill DataSet which contains 2 tables with one to many relationship. I\'m using DataReader to achieve this :

    public DataSet SelectOne(int i         


        
8条回答
  •  星月不相逢
    2020-11-29 08:07

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("data source=.;uid=sa;pwd=123;database=shop");
        //SqlCommand cmd = new SqlCommand("select * from tblemployees", con);
        //SqlCommand cmd1 = new SqlCommand("select * from tblproducts", con);
        //SqlDataAdapter da = new SqlDataAdapter();
    
        //DataSet ds = new DataSet();
        //ds.Tables.Add("emp");
        //ds.Tables.Add("products");
        //da.SelectCommand = cmd;
        //da.Fill(ds.Tables["emp"]);
        //da.SelectCommand = cmd1;
    
        //da.Fill(ds.Tables["products"]);
        SqlDataAdapter da = new SqlDataAdapter("select * from tblemployees", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "em");
        da = new SqlDataAdapter("select * from tblproducts", con);
        da.Fill(ds, "prod");
    
        GridView1.DataSource = ds.Tables["em"];
        GridView1.DataBind();
        GridView2.DataSource = ds.Tables["prod"];
        GridView2.DataBind();
    }
    

提交回复
热议问题