views:

146

answers:

2

I'm designing a web service in ASP.NET and VS2008, and am using typed datasets for retrieving table data. These work well and establish their own connections through their associated TableAdapter objects. Edit: I'm using VB, BTW!

I am now attempting to run a custom SQL string using a DataAdapter and a Command object, however I need to reference a Connection object in order for the Command to work. What is the best way to handle this? Should I:

a) Create a global connection object using Global.asax, retrieving the connection string from web.config? (I've been trying that one already, with not much success)

b) Create a class-level connection object using the InitialiseComponent method, also retrieving the ConnectionString from web.config?

c) Retrieve a Connection from one of the TableAdapters that I've already created in my typed DataSets?

d) Something else I haven't thought of yet?

BTW I've been finding it very difficult to extract a ConnectionString from web.config, so any help with that would be appreciated also!

I'm not entirely inexperienced with ASP.NET, but my last big project used VS2003, and I want to make sure that I'm using the current tools correctly.

+3  A: 

To extract the connection string, use

WebConfigurationManager.ConnectionStrings["name"].ConnectionString

It's best to open and close the connections as close as possible to their use. ADO.NET will do connection pooling so that this won't be expensive:

var connectionString = 
    WebConfigurationManager.ConnectionStrings["name"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = new SqlCommand("query", conn))
    {
        conn.Open();

        // Use the command
    }
}
John Saunders
OK, thanks for that bit of advice - I was wondering whether a global connections were more efficient or not. I forgot to mention that I'm using VB, however I think I can translate!
Billious
+1  A: 

For Connection and data access problems I will advise you to go with some kind of Data Helpers like Microsoft Data Access Application Block

Here you can find small tutorial about how to use it.

For getting connectionstring from web.config use folowing methods

 public static string GetConnectionString( string strConstringKey )
        {
            return ConfigurationManager.ConnectionStrings[strConstringKey];
        }

        public static bool GetConnectionString(string strConstringKey, ref string strConstring)
        {
            return (strConstring = ConfigurationManager.ConnectionStrings[strConstringKey] ) == null ? false : true  ;
        }
Mahin
Thanks - your syntax for retrieving the connection string turned to be the one that worked. Up-voted it, since I can't accept two answers!
Billious
Also, I'm checking out the links you provided - forgot to mention that.
Billious
You are welcome mate... Happy to help :)
Mahin
Had a look at Data Access Application Block - great idea, will save a lot of coding! Thanks for the link.
Billious
Happy to help :)
Mahin