<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Using static properties in SQLCLR UDTs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/using-static-properties-in-sqlclr-udts.aspx</link><description>I spoke at the Beantown .NET user group meeting tonight, on the topic of SQLCLR in SQL Server 2005. One of the questions that came up during the UDT part of the talk was whether static properties are supported. Unfortunately, I had no answer at the time--it's</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Using static properties in SQLCLR UDTs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/using-static-properties-in-sqlclr-udts.aspx#10070</link><pubDate>Mon, 24 Nov 2008 12:54:51 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10070</guid><dc:creator>Oleg</dc:creator><description>&lt;p&gt;Hi, &lt;/p&gt;
&lt;p&gt;I have function and I must have static member in it. &lt;/p&gt;
&lt;p&gt;I do not see another way. Also, i detected interesting effect:&lt;/p&gt;
&lt;p&gt;1. I have function in &amp;nbsp;one db&lt;/p&gt;
&lt;p&gt;2. I call it, then I change connectin to another db&lt;/p&gt;
&lt;p&gt;3. I call this function &amp;nbsp;again and I have result! but function exists only in the 1st db :-)&lt;/p&gt;
&lt;p&gt;using System;&lt;/p&gt;
&lt;p&gt;using System.Data;&lt;/p&gt;
&lt;p&gt;using System.Data.SqlClient;&lt;/p&gt;
&lt;p&gt;using System.Data.SqlTypes;&lt;/p&gt;
&lt;p&gt;using Microsoft.SqlServer.Server;&lt;/p&gt;
&lt;p&gt;using System.Text.RegularExpressions;&lt;/p&gt;
&lt;p&gt;using nvg_net2;&lt;/p&gt;
&lt;p&gt;public partial class UserDefinedFunctions&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static readonly SynonymFinder sp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static UserDefinedFunctions()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sp = new SynonymFinder();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;[SqlFunction(DataAccess = DataAccessKind.Read)]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static SqlString SynonymFinderor(SqlString strInput)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;using (SqlConnection m_connection = new SqlConnection(&amp;quot;context connection=true&amp;quot;))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (sp != null &amp;amp;&amp;amp; !sp.IsInitialized())&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (m_connection != null &amp;amp;&amp;amp; m_connection.State != ConnectionState.Open) m_connection.Open();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sp.m_connection = m_connection;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sp.InitObject();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Regex rx = new Regex(@&amp;quot;\w+&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string result = rx.Replace(strInput.ToString(), new MatchEvaluator(Synonym));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return result;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;private static string Synonym(Match m)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string x = m.ToString();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string str = &amp;quot;&amp;quot;;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sp.correct(x, ref str);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (str == &amp;quot;&amp;quot;) str = x;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return str;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;};&lt;/p&gt;
</description></item><item><title>re: Using static properties in SQLCLR UDTs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/using-static-properties-in-sqlclr-udts.aspx#11100</link><pubDate>Mon, 12 Jan 2009 21:41:16 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:11100</guid><dc:creator>Firefight</dc:creator><description>&lt;p&gt;I can't believe they implemented static members in SQLCLR this way. They should have made static variables unique to each session/connection. That's how Oracle implemented their package level variables in PL/SQL and static method in their embedded Java implementation, and it's what you want 99.9% of the time.&lt;/p&gt;
&lt;p&gt;Now almost every 3rd party library is going to have to be marked as UNSAFE because of this. You almost never want pragmatic information passing accross connections like this, I don't know what they were thinking.&lt;/p&gt;
</description></item><item><title>re: Using static properties in SQLCLR UDTs</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/using-static-properties-in-sqlclr-udts.aspx#11113</link><pubDate>Tue, 13 Jan 2009 15:03:09 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:11113</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;I don't really see the point of having a static member scoped to the session, and this implementation certainly does not prevent you from being able to accomplish that without the use of UNSAFE. &amp;nbsp;All you need to do, in essence, is set up a keyed collection and use a GUID or something similar to uniquely identify the session. &amp;nbsp;Some kind of expiration logic would also be necessary, in case the session doesn't call back as planned... &lt;/p&gt;
</description></item></channel></rss>