Wednesday, March 28, 2012

One Connection Several queries?

I need to make one connection to a database and return 4 different pieces of data from the database so that I can maximize effciency. Well doing it that way is a guess on my part about maximizing effeciency.

Databases and me don't know a lot about each other. I can do some basic sql queries but that is about it and as far as my knowledge seems to go. ADO.NET is something I am still trying to learn well.

Any help would be great.

You can use Sp_Executesql Microsoft have provided some code to get you started. Hope this helps.

http://support.microsoft.com/default.aspx?scid=kb;en-us;262499

|||

It is advisable to minimize your connections, so you're off to a good start. You have two options for the multiple queries. One would be to open the connection, execute and process query 1, execute and process query 2, etc. For each query, you just specify the same connection. Your other option would be to put all your queries in a single stored procedure. You'd send down the command to execute the stored procedure with any paramers, and all 4 recordsets would be returned as children of one large recordset. You'd then process the first recordset, use the .movenext method, process the next recordset, etc.

Which way you choose depends on what you're doing with the data, how your app is architected, and your level of comfort.

|||Thank you both for your resposnes this has given me a direction to look in. I guess my next step is to learn Stored Procedures. Always new that not learning it would come back and haunt me.

No comments:

Post a Comment