How to fill Dataset with multiple tables?

前端 未结 8 660
小鲜肉
小鲜肉 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 07:53

    If you are issuing a single command with several select statements, you might use NextResult method to move to next resultset within the datareader: http://msdn.microsoft.com/en-us/library/system.data.idatareader.nextresult.aspx

    I show how it could look bellow:

    public DataSet SelectOne(int id)
    {
        DataSet result = new DataSet();
        using (DbCommand command = Connection.CreateCommand())
        {
            command.CommandText = @"
    select * from table1
    select * from table2
            ";
    
            var param = ParametersBuilder.CreateByKey(command, "ID", id, null);
            command.Parameters.Add(param);
    
            Connection.Open();
            using (DbDataReader reader = command.ExecuteReader())
            {
                result.MainTable.Load(reader);
                reader.NextResult();
                result.SecondTable.Load(reader);
                // ...
            }
            Connection.Close();
        }
        return result;
    }
    

提交回复
热议问题