Geeks With Blogs

Scott Kuhl Warning: I may have no idea what I am talking about!

Beyond all the basic filtering available in Fetch methods and collection filtering, SubSonic also provides a way to dynamically build SQL queries.  To get started, create a new Query object, using the CreateQuery method.

SubSonic.Query query = Product.CreateQuery();

or pass it the table name

SubSonic.Query query = new SubSonic.Query(product.TableName);

or pass it the table schema

SubSonic.Query query = new SubSonic.Query(Product.GetTableSchema());

The last option will prevent SubSonic from loading the table information from the database, eliminating an extra call. 

Note: You can also get the table schema by calling the static method BuildTableSchema or the property Schema.

Running the Query

Executing the query follows the same rules as executing ADO.NET queries.  The Execute method runs the query and returns nothing.

query.Execute();

The ExecuteDataSet method returns a DataSet.

GridView1.DataSource = query.ExecuteDataSet();

The ExecuteReader method returns a DataReader.

GridView1.DataSource = query.ExecuteReader();

And finally, the ExecuteScalar returns a single value.

Label1.Text = query.ExecuteScalar().ToString();

Find the entire series of posts here.

Posted on Wednesday, December 6, 2006 10:40 PM SubSonic | Back to top


Comments on this post: Getting Started with SubSonic - Part 14, Queries

# re: Getting Started with SubSonic - Part 14, Queries
Requesting Gravatar...
Thanks for this post
Left by Pham Son on Nov 07, 2009 8:16 AM

Your comment:
 (will show your gravatar)


Copyright © Scott Kuhl | Powered by: GeeksWithBlogs.net | Join free