How to select rows inside a CLR stored procedure in MS SQL Server 2008? -


following along example found on internet, wrote following c# code implements clr stored procedure in ms sql server 2008:

public class class1 {     [microsoft.sqlserver.server.sqlprocedure]     public static void countstringlength(string inputstring)     {         sqlcontext.pipe.send(inputstring.length.tostring());                 } } 

this procedure takes string parameter , outputs number of characters in string.

i have been working on code retrieves data sql; there's nothing special way data retrieved: makes connection sql server, selects data, , closes connection. want make code run inside stored procedure or trigger on sql server.

i suppose make code run same existing sql code: make connection sql server, select data, , close connection. however, doesn't make sense once code running on sql server itself! why want code runs on sql server make server connect itself?!?!

is there best practice i'm trying do? can select rows in code using same connection used execute stored procedure?

i found answer explained here: http://technet.microsoft.com/en-us/library/ms131053.aspx

the connection clr procedure runs called "context connection" , used this:

using(sqlconnection connection = new sqlconnection("context connection=true"))  {     connection.open();     // use connection } 

i wonder why have "open" connection? think connection open since executing procedure running.

also closely related: http://msdn.microsoft.com/en-us/library/938d9dz2(v=vs.90).aspx


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -