473,480 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Best way or returning hierarchical data

Let's say I've got a forum, where users can be moderators of each forum.

Tables look like this:

USER
--------
user_key
name

FORUM
---------
forum_key
name

USERFORUM
----------
userforum_key
user_fkey
forum_fkey

(I'm sure you can work out where the relationships are!)

My user, "Danny" is a moderator in "Forum1" and "Forum2".

I've just logged in as Danny, and my User.Authenticate() method should go to
the database, check the details, and return all the info to populat the User
object with as little effort as possible (ie. 1 query if possible).

Most of the user object is just string variables, but there's also gotta be
a list of forums I can moderate. I'm open to suggestions of how this should
actually be stored (like an int[] of the keys, should be fine).

My question, is this... How is the "best" way to pull back these forum_keys
that I'm allowed to moderate? If I join to the table, I also pull back my
user details once for every forum (twice, in this case). This seems a bit
messy.

Stored procedures don't seem to be capable of returning more than a flat
table. How would you achieve what I'm trying?

Many thanks,
Danny
Nov 15 '05 #1
4 2589
You should store it in a table like this:

Moderates
------------
user_key
forum_key

To pull back the list of forums you can moderate you would do the following

SELECT forum_fkey FROM moderates WHERE user_fkey = xxxxxx

or if you have the user name but not their key you can do this:

SELECT forum_fkey FROM user a,moderates b WHERE name='danny' and a.user_key = b.user_key
Nov 15 '05 #2
"Jeff" <an*******@discussions.microsoft.com> wrote in message
news:AD**********************************@microsof t.com...
Moderates
------------
user_key
forum_key

To pull back the list of forums you can moderate you would do the following
SELECT forum_fkey FROM moderates WHERE user_fkey = xxxxxx

or if you have the user name but not their key you can do this:

SELECT forum_fkey FROM user a,moderates b WHERE name='danny' and

a.user_key = b.user_key

Sorry, I mustn't have explain correctly. I can do that bit fine, it's trying
to return "a row" or user data and "x rows" or forum keys from a single
stored procedure call I'm trying to to find the best way for
--
Daisy The Cow
Nov 15 '05 #3
Hi,

"Daisy" <da***@nospam.oops> wrote in message
news:bu**********@linux01.dannytuppeny.com...
Let's say I've got a forum, where users can be moderators of each forum.

Tables look like this:

USER
--------
user_key
name

FORUM
---------
forum_key
name

USERFORUM
----------
userforum_key
user_fkey
forum_fkey

First of all, I'm no db expert. And I don't think two queries is a shame.

However ADO does support hierarchical recordset, where the values of one
column are recordsets representing the child rows.

ADO.NET supports this too, for DataReader this almost the same. The values
of one column can be casted to another DataReader (containing the child
data).

And for an DataAdapter, the fill method can create two tables at once.

Now, to query for a hierarchical recordset you can use the Shape provider.
The shape provider provides the hierarchy while it uses other providers to
get the data.

See this sample suited for your case:

OleDbConnection conn = new OleDbConnection(
"Provider=MSDataShape;Data Provider=Microsoft.JET.OLEDB.4.0;" +
"Data Source=yourdb.mdb" );

conn.Open();
OleDbDataAdapter adap = new OleDbDataAdapter(
"SHAPE {SELECT user_key, name FROM tblUser WHERE name=?} " +
" APPEND ({SELECT forum_key, user_fkey, name FROM tblForum,tblUserForum
" +
"WHERE forum_key = forum_fkey } AS forum " +
" RELATE user_key TO user_fkey)", conn);

adap.SelectCommand.Parameters.Add("?", "the name of the user you want info
about");

DataSet ds = new DataSet();

adap.Fill (ds, "users");

foreach (DataRow dr in ds.Tables["users"].Rows)
{
Console.WriteLine("{0},{1}", dr["user_key"], dr["name"] );
foreach (DataRow dr2 in dr.GetChildRows(ds.Relations[0]))
{
Console.WriteLine("\t{0},{1}", dr2["forum_key"], dr2["name"] );
}
}
conn.Close();
This sample will generate hierarchical dataset, containing two tables (users
& forum) and a relation between them.

It will then show all rows from the parent table(users) and then for each
row it will show all child rows(forums).

In your case there will be only one row (one user) so it's an overhead here
to enumerate all rows in the users table. (Just use the first row, and if
there is none then the user account doesn't exist)

Just to note that it's possible to remove the where clause in the first
query. So that the users table would contain all rows (all users) and each
of those rows would have childrows representing the forums they can operate.

HTH
greetings

(I'm sure you can work out where the relationships are!)

My user, "Danny" is a moderator in "Forum1" and "Forum2".

I've just logged in as Danny, and my User.Authenticate() method should go to the database, check the details, and return all the info to populat the User object with as little effort as possible (ie. 1 query if possible).

Most of the user object is just string variables, but there's also gotta be a list of forums I can moderate. I'm open to suggestions of how this should actually be stored (like an int[] of the keys, should be fine).

My question, is this... How is the "best" way to pull back these forum_keys that I'm allowed to moderate? If I join to the table, I also pull back my
user details once for every forum (twice, in this case). This seems a bit
messy.

Stored procedures don't seem to be capable of returning more than a flat
table. How would you achieve what I'm trying?

Many thanks,
Danny

Nov 15 '05 #4
"BMermuys" <bm**************@hotmail.com> wrote in message
news:rR*********************@phobos.telenet-ops.be...

<snip DataShaping stuff>
Just to note that it's possible to remove the where clause in the first
query. So that the users table would contain all rows (all users) and each of those rows would have childrows representing the forums they can

operate.

Yep, I get that. I started using Datashaping with classic ASP a few months
back, however this involved the query being in ADO, and not in a stored
procedure, and we're trying to keep as much as possible in SP's...

Just thinking about it, if I have two SELECT queries in a stored proc, and
use ExecuteDataset on MS's SqlHelper, it might return a DataSet with two
DataTables - also quicker than two seperate calls to ExecuteDataset...?
--
Daisy The Cow
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
4896
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
5
2234
by: clintonG | last post by:
I'm looking for documentation and would not turn my nose up to any code from anybody who thinks they are good at the design of an algorythm that can be used to generated a hierarchical relational...
0
1424
by: stigbn | last post by:
When a DataSet is used as data source for a DataGrid one can get hiearachical datagrid by using relations among the DataTables of the DataSet. (By hierarchical datagrid, I mean columns that can be...
0
1754
by: Björn Bengtsson | last post by:
Hello! I have an urgent problem concerning ASP.NET, ADO.NET, SQL Server, XML and the TreeView control. I have two tables; one describing the products and one describing their relationships. A...
3
2021
by: Bennett Haselton | last post by:
I want to display a hierarchical listing of items from a database table, where, say, each row in the table has an "ID" field and a "parent_id" field giving the ID of its parent (NULL if it's at the...
4
1631
by: Dave | last post by:
(My apologies for posting this on two forums. I have just found out the other one was the incorrect location) I am writing a VB.NET 2003 web application to operate on my company's intranet. It...
2
5853
by: Don | last post by:
In a previous version of vb I used to save and load hierarchical data from an Access db into a treeview. But, I never found a very satisfying or elegant way to do it. I used a flat file approach...
4
7586
by: Congero | last post by:
I'm trying to find a way to bind hierarchical data to a gridview control. I've been able to do this with some third party controls and was wondering if this functionality is available with the...
2
2530
by: Ariana | last post by:
I have the following situation for an ASP.NET 2.0 web site: - Data is in one XML file - I want to transform the data using an XSLT file before using it - The data is hierarchical - The data...
0
7054
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6918
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3008
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
570
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.