GetPictures publicoverride ThreadSet GetPictures(GalleryThreadQuery query) { using( SqlConnection connection = GetSqlConnection() ) { using(SqlCommand command =new SqlCommand(databaseOwner +".cs_gallery_Threads_GetThreadSet", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@SectionID", SqlDbType.Int).Value = query.SectionID; command.Parameters.Add("@PageIndex", SqlDbType.Int, 4).Value = query.PageIndex; command.Parameters.Add("@PageSize", SqlDbType.Int, 4).Value = query.PageSize; command.Parameters.Add("@sqlPopulate", SqlDbType.NText).Value = SqlGenerator.BuildGalleryThreadQuery(query, databaseOwner); command.Parameters.Add("@UserID", SqlDbType.Int).Value = query.UserID; command.Parameters.Add("@IncludeCategories", SqlDbType.Bit).Value = query.IncludeCategories; command.Parameters.Add("@TotalRecords", SqlDbType.Int).Direction = ParameterDirection.Output; command.Parameters.Add(SettingsIDParameter()); ThreadSet ts =new ThreadSet(); // Execute the command //If we want to display the list of categories for the post, we will need to //use a dataset so we can join the child records if(query.IncludeCategories) { DataSet ds =new DataSet(); SqlDataAdapter da =new SqlDataAdapter(command); //wait as long as possible to open the conn connection.Open(); da.Fill(ds); connection.Close(); //keep a referece to the relation since it is used in the GetChildRows look up anyway DataRelation relation =new DataRelation("Categories",ds.Tables[0].Columns["PostID"],ds.Tables[1].Columns["PostID"],false); ds.Relations.Add(relation); DataRowCollection posts = ds.Tables[0].Rows; foreach(DataRow dr in posts) ts.Threads.Add(PopulatePictureFromIDataReader(dr, relation)); ds.Clear(); ds.Dispose(); ts.TotalRecords = (int) command.Parameters["@TotalRecords"].Value; } else { //No categories needed, so we can use a datareader. connection.Open(); using(SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection)) { while(dr.Read()) ts.Threads.Add(PopulatePictureFromIDataReader(dr)); dr.NextResult(); ts.TotalRecords = (int) command.Parameters["@TotalRecords"].Value; } } return ts; } } }