Context Connection & Using

  • Thread starter Thread starter Scott M.
  • Start date Start date
S

Scott M.

When working with the SQL CLR, should I be using "using" when referring to
the "context connection"?
 
Hello Scott,

According to your description, you want to know if we should wrap the
connection with "using" blocks in SQL CLR, correct? If I misunderstood
anything here, please don't hesitate to correct me.

In my opinion, working with the Using Block is recommended, but no
necessary.
Working with using block guarantees disposal of the resources, no matter
how we exit the block. However, if you make sure you will close the
connection by yourself, it is not necessary to put it into Using Block
again.

Hope this helps. Please feel free to update there again, if there is
anything unclear. We are glad to assist you.

Have a great day,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Wen,

Thanks for you response.

I am aware of what using does and agree that if you manage the connection
correctly, its use is not needed. My question is a bit more involved than
this though. When using the context connection in SQL CLR, should Using be
used (or if I am managing the connection myself, should I be closing and
disposing it)?

The point being that the context connection represents the connection that
that calling code is using. If I were to close and dispose of that
connection in the SQL CLR code, then wouldn't that have ramifications on the
caller's ability to continue to use the connection if desired?

-Scott
 
Hello Scott,

It sound like your main concern is if closing/disposing the connection in
SQLCLR code will effect on the caller's ability to continue to use the
connection, correct? Please don't hesitate to correct me, if I
misunderstood anything again.

SQL CLR creates new connection with the same context of caller's connection
in his code. It doesn't use caller's connection. They are not the same
connection. Thereby, (as far as I know), whatever we do on the context
connection, this will not effect on the caller's connection.

Hope this helps. Please feel free to update here again, if there is
anything unclear. We are glad to assist you.
Have a great day,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
My question (again) is why are you using the Context Connection in the first
place and not a TSQL routine to manage rowsets?

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
 
I'm asking the question purely for an understanding of how to do it. I
understand when to use T-SQL vs. CLR code.
 
Back
Top