How to fill Dataset with multiple tables?

前端 未结 8 684
小鲜肉
小鲜肉 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:09

    Filling a DataSet with multiple tables can be done by sending multiple requests to the database, or in a faster way: Multiple SELECT statements can be sent to the database server in a single request. The problem here is that the tables generated from the queries have automatic names Table and Table1. However, the generated table names can be mapped to names that should be used in the DataSet.

    SqlDataAdapter adapter = new SqlDataAdapter(
          "SELECT * FROM Customers; SELECT * FROM Orders", connection);
    adapter.TableMappings.Add("Table", "Customer");
    adapter.TableMappings.Add("Table1", "Order");
    
    adapter.Fill(ds);
    

提交回复
热议问题