Query Database dynamically

Posts   
 
    
MichelZ avatar
MichelZ
User
Posts: 24
Joined: 25-Jul-2008
# Posted on: 01-Oct-2009 16:30:42   

Hi guys

Got a question for querying a database where the table/fields to query is not known during design/compile time, but dynamic.

Let's do an example:

Small Web form, where Joe User has got 3 Text Fields, one for Table, one for Column, and one for ID.

Joe User types in: "Order", "Quantity", 12

This would retrieve row with ID 12 from the Orders Table, and would display Field "Quantity".

How would I solve this in actual code, when the only things I get is 3 parameters (Table, Column, ID)?

How do I translate this to OrderEntity order = new OrderEntity(12); adapter.FetchEntity(order); Console.WriteLine(order.Quantity);

dynamically, without Switching trough all the possibilities? i.e. switch (Table) { case "Order": tableEntity = OrderEntity; break; [.....] }

Is there something like "Eval" I could use? (C# 2.0, LLBLGEN 2.6, Adapter)

Haven't found a way of doing it yet rage

Cheers Michel

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 01-Oct-2009 17:03:26   

This has been discussed several times before. Try searching the forums for "dynamic query", have a read of the threads, and then come back to us with any more specific questions.

Hint - Reflection is your friend...:-)

Matt

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39862
Joined: 17-Aug-2003
# Posted on: 01-Oct-2009 20:42:31   

Also take into account that entity types are available through the EntityType enum and all entity fields are available through the fields enums for each entity (and with factories you can create the instances without a lot of effort)

Frans Bouma | Lead developer LLBLGen Pro
MichelZ avatar
MichelZ
User
Posts: 24
Joined: 25-Jul-2008
# Posted on: 14-Oct-2009 17:33:19   

Thanks guys!

I did search before, but seems like i used bad search terms simple_smile

Thanks for pointing me in the right direction!

/Michel