Welcome to MSDN Blogs Sign in | Join | Help

Sql Server: Everything you always wanted to know about SMO Connections

There are several ways of controlling connections in SMO -- you can control pretty much any aspect of a SMO connection.

Pooled connections

Let's take a look at a typical way how you would obtain a connection using a simple C# sample:

Server svr = new Server();
Console.WriteLine(svr.Information.Product + " " + svr.Information.VersionString + " " + svr.Information.ProductLevel);

When you run this sample it will print the version, and edition, but you will also notice that the Connect() call is missing. Is there a need to call Connect()? The answer is both "yes", and "no". When you do not call Connect(), SMO will automatically obtain a connection from the connection pool when it needs one (in this case when accessing the svr.Information.VersionString/Edition properties), and after the operation has finished it will return that connection to the pool. This is a nice model as it stops you from needing to worry about when to call Disconnect(). SMO will do that bookkeeping for you and if your application has to exit because of a failure you do not need to release the connection -- SMO already has done that for you.

Explicitly connecting

You can override that behavior by explicitly connecting:

Server svr = new Server();
svr.ConnectionContext.Connect();
Console.WriteLine(svr.Information.Product + " " + svr.Information.VersionString + " " + svr.Information.ProductLevel);
svr.ConnectionContext.Disconnect();

What happens here is that SMO will obtain a connection from the connection pool on your request, and will not automatically release it to the pool, until you call Disconnect().

This is very much the same pattern that you would use in SQL-DMO. Also, if you are not able to connect to the server, you will get an exception during the Connect() call. However, there is one slight difference. The connection that is obtained comes from a connection pool. If you call Disconnect() and Connect() in succession then you will likely get the connection from the pool, so this allows you to efficiently manage your connections. Connection pooling means the connection is not immediately closed. It’s up to the connection pool to close it when the lifetime expires.

Non pooled connections

In some cases you want to not draw connections from a pool, but make sure that you get a non-pooled connection that you can hold on as long as your application is running. This can be helpful when running in single-user mode, or when you are paranoid about SMO to do the right thing with your connections and want to have complete control over the connections that are managed. This can be simply accomplished by setting the NonPooledConnection property to true.

Server svr = new Server();
svr.ConnectionContext.NonPooledConnection = true;
Console.WriteLine(svr.Information.Product + " " + svr.Information.VersionString + " " + svr.Information.ProductLevel);

The connection is then a dedicated connection that will not be returned to the pool. SMO will again, like with pooled connections, connect on demand, but in this case it will hold on to the connection until you specifically call Disconnect(). Connecting is in this case much more expensive, as there will not be a connection in the pool. This type of connection however guarantees that you have that connection for the lifetime of your application. For example, SQL Server Management Studio uses such a connection.

Sharing connections between processes

You can share connections between SMO Server instances, and pass and Create a new ServerConnection object based on an existing object, which is helpful if you need multiple connections with the same setting:

Server svr = new Server();

svr.ConnectionContext.NonPooledConnection = true; // comment this out to see what happens to the SPID's
Console.WriteLine(svr.ConnectionContext.ProcessID);
// do work...

 

// Pass the connection on to new Server instance -- the connection is reused

Server svr2 = new Server(svr.ConnectionContext);

Console.WriteLine(svr2.ConnectionContext.ProcessID);

// do work...

 

// The Copy() method will create a new object, and will use a new connection to the server

Server svr3 = new Server(svr2.ConnectionContext.Copy());

Console.WriteLine(svr3.ConnectionContext.ProcessID);
// do work...

If you run this example, you will see that the ProcessID (SPID) only differs for svr3, as it will not use the existing connection.

Comment out the indicated line, and see if you understand what's happening! (please post a response, don't be shy! ;-).

Other settings

There are various other settings of the ServerConnection object that can be manipulated to finetune SMO's behavior. Examples of this are MaxPoolSize, PooledConnectionLifeTime. Take a look at the object and you will find that all possible connection settings are available.

Some interesting facts

  • The default instance is assumed when you do not specify a server name
  • SMO uses Integrated security by default (the most secure option)
  • Once you have connected, you cannot change the properties of the ServerConnection object. You need to get a new ServerConnection object or use Copy() if you want a new connection with the same properties.

Important note

SMO allows you to pass on ServerConnections to different Server instances, but be very careful when doing so. SMO is making assumptions about various settings, so if you change settings, for example you grab the ServerConnection for other purposes than using it for SMO, and issue a SET NOEXEC ON, then SMO will fail to execute the statements. This will cause SMO to respond unpredictable, with application exceptions likely to occur. It is strongly discouraged to reuse connections for other uses than in SMO. Use pooling and obtain a dedicated connection for purposes other than using it with SMO.
Published Monday, May 02, 2005 3:40 PM by mwories
Filed under:

Comments

# re: Sql Server: Everything you always wanted to know about SMO Connections

Wednesday, December 28, 2005 8:44 PM by Deepankar (brokenrulz)
Excellent starter..

# re: Sql Server: Everything you always wanted to know about SMO Connections

Saturday, December 02, 2006 3:13 AM by Eleonor Memo

<a href= http://forum.lixium.fr/cgi-bin/index.eur?mitsu > wellbutrin medication </a> [url= http://forum.lixium.fr/cgi-bin/index.eur?mitsu ] wellbutrin medication [/url]

# re: Sql Server: Everything you always wanted to know about SMO Connections

Monday, February 19, 2007 10:38 AM by Linkin park

Best my wishes to Admin. Plz look my site too:

<a href=http://linkin-park.creablog.com>linkin">http://linkin-park.creablog.com>linkin park concert ticket</a> | [url=http://linkin-park.creablog.com]linkin park concert ticket[/url] | http://linkin-park.creablog.com - linkin park concert ticket !  Thanks.  p.s. Linkin Park Concert Ticket

# re: Sql Server: Everything you always wanted to know about SMO Connections

Saturday, March 31, 2007 2:40 AM by Britneyeejud

Very nice! I have some LJ with news, check this out:

<a href= http://michelas.livejournal.com >My live journal</a>

<a href= http://homerius.livejournal.com >Lastest news</a>

<a href= http://johnyknoxw.livejournal.com >My live journal</a>

# re: Sql Server: Everything you always wanted to know about SMO Connections

Saturday, March 31, 2007 7:34 AM by Britneyhvvvp

Very nice! I have some LJ with news, check this out:

<a href= http://iwantubadlyz.livejournal.com >Newest news</a>

<a href= http://annakubat.livejournal.com >Check this out</a>

<a href= http://jackie_simpson.livejournal.com >livejournal</a>

# re: Sql Server: Everything you always wanted to know about SMO Connections

Tuesday, April 03, 2007 6:09 PM by itnslrq,itnslrq,itnslrq

<a href= http://enorme.italyans.info >Enorme</a> [url=http://enorme.italyans.info]Enorme[/url] <a href= http://isolata.italyans.info >Isolata</a> [url=http://isolata.italyans.info]Isolata[/url] <a href= http://sembrata.italyans.info >sembrata</a> [url=http://sembrata.italyans.info]sembrata[/url] <a href= http://sotto.italyans.info >SOTTO</a> [url=http://sotto.italyans.info]SOTTO[/url]

# re: Sql Server: Everything you always wanted to know about SMO Connections

Thursday, April 05, 2007 2:29 PM by Peni

Your site is best look my site - <a href="http://freecal.brownbearsw.com/Magnarx">Magna rx</a>

# re: Sql Server: Everything you always wanted to know about SMO Connections

Wednesday, April 18, 2007 10:06 AM by healthbve,healthbve,healthbve

The Best Catalog.

<a href=http://healthpiece.info/>Real">http://healthpiece.info/>Real Catalog</a>[url=http://healthpiece.info/]The Real Catalog[/url]

# re: Sql Server: Everything you always wanted to know about SMO Connections

Thursday, April 19, 2007 10:12 AM by Blow Job

This is really fresh idea of the design of the site! I seldom met such in Internet. I will visit your website again. Good Work dude!

# re: Sql Server: Everything you always wanted to know about SMO Connections

Sunday, April 29, 2007 6:25 AM by Buy Synthroid

My compliments to a very nice website. I found lots of intresting things here. p.s. If there will not provide documentation in front of the thyroglossal duct

# re: Sql Server: Everything you always wanted to know about SMO Connections

Thursday, May 03, 2007 2:59 PM by Celebrex Online

"It's not because of fate, it's because of Tequila"  That may be the best thing I have ever read in my whole life!

# re: Sql Server: Everything you always wanted to know about SMO Connections

Saturday, May 05, 2007 4:27 AM by Albert Bibingo

This is really fresh idea of the design of the site! I seldom met such in Internet. Good Work dude!

# re: Sql Server: Everything you always wanted to know about SMO Connections

Sunday, May 06, 2007 4:48 PM by Robert Mamahoe

I found lots of intresting things here. Please more updates.

# re: Sql Server: Everything you always wanted to know about SMO Connections

Monday, May 07, 2007 4:48 PM by valium Genericu

Good Work dude! I will visit your website again.

# re: Sql Server: Everything you always wanted to know about SMO Connections

Thursday, May 17, 2007 5:11 PM by Buy Valium

Good Work dude! I will visit your website again.

http://valium1.blogcu.com/ Buy Valium

# re: Sql Server: Everything you always wanted to know about SMO Connections

Saturday, May 19, 2007 1:08 AM by Lesbiane Sexc

Hi Webmaster! It was a pleasure to look through this site! there is a lot of new and fresh ideas)!Thank You

# re: Sql Server: Everything you always wanted to know about SMO Connections

Monday, May 21, 2007 3:28 AM by Buy Caverta

Nice design. Please add more smiles to your guestbook :)  Please more updates.

# re: Sql Server: Everything you always wanted to know about SMO Connections

Friday, May 25, 2007 7:30 AM by Papayud

<a href= http://tyqahu.angelfire.com >a4 desk keygen</a> <a href= http://sivyva.angelfire.com >aaf air american filter</a> <a href= http://texejo.angelfire.com >a few good men video clip</a> <a href= http://mudyvi.angelfire.com >a letter to single</a> <a href= http://gysuzo.angelfire.com >aahpered</a>

# re: Sql Server: Everything you always wanted to know about SMO Connections

Monday, May 28, 2007 6:30 AM by Nbkvqyd,Nbkvqyd

# re: Sql Server: Everything you always wanted to know about SMO Connections

Monday, May 28, 2007 3:51 PM by Buy Caverta

Looks great! I found lots of intresting things here. Many thanks.

# re: Sql Server: Everything you always wanted to know about SMO Connections

Sunday, June 03, 2007 1:41 AM by Lesbians Sexo

Looks great! I found lots of intresting things here. Many thanks.

# re: Sql Server: Everything you always wanted to know about SMO Connections

Monday, June 04, 2007 10:42 PM by Buy Caverta

Hey! This is really your Work?! Cool! I never earlier did not see sites like this! Tnx!

# re: Sql Server: Everything you always wanted to know about SMO Connections

Tuesday, June 05, 2007 7:02 AM by Buy Caverta

What a good site! I think it wasnt easy to post here so much information. Thank you, I will add it to my bookmarks

# re: Sql Server: Everything you always wanted to know about SMO Connections

Hi man! Your site is cool! Would you please also visit my site?

# re: Sql Server: Everything you always wanted to know about SMO Connections

Friday, September 28, 2007 5:22 PM by Alesha

Hi, nice page.!

look on my pages:

<a href="http://profitb.free-site-host.com/associate-international-profit/associate-complaint-international-profit.html">associate complaint international profit</a>

ok.. bitte  

Anonymous comments are disabled
 
Page view tracker