Solved

Using Regex MatchEvaluators with an SqlConnection

Posted on 2004-10-05
3
350 Views
Last Modified: 2006-11-17
Hi,

I wrote code that searches an XML document for patterns (using Regex) of the form #<variable-name>#, and then substitutes the right value for <variable-name> by querying an SQL database.

To do the substitutions, I'm using Regex.Replace() with a MatchEvaluator function.

My problem is, MatchEvaluator is a static function, so it can't access the class's existing non-static SqlConnection to query the database.  If I make the SqlConnection static, it becomes non-thread-safe.  For efficiency reasons, I don't want to open a new SqlConnection every time I call MatchEvaluator.  Is there any way to do this?
0
Comment
Question by:yizchaknaveh
3 Comments
 
LVL 7

Assisted Solution

by:NipNFriar_Tuck
NipNFriar_Tuck earned 100 total points
ID: 12231725
Some possibilities are that you could do a type of singleton or you can make the connection static and then do a lock on the connection, lastly you could create a non static function that calls the static function and let the non static function get the connection and pass it in to the static function.

Hope this helps...
0
 
LVL 19

Accepted Solution

by:
drichards earned 400 total points
ID: 12233445
MatchEvaluator does not have to be a static function.  If you have an instance of an object (obj) with a member function 'Func', you can do:

    MatchEvaluator me = new MatchEvaluator(obj.Func);

and Func will be called in the context of object 'obj'.

And according to the SqlConnection docs, "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe."  Read at:

   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlConnectionClassTopic.asp

It would seem that you want the SqlConnection to be static anyway.  Do you know of documentation to the contrary?
0
 

Author Comment

by:yizchaknaveh
ID: 12241429
Excellent.  Thanks.  I made it non-static and all problems went away.  I'm not sure why I thought it had to be static.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System.Security 2 29
Hey!!!! 1 30
How to use spatial data types in SQL Server Database project 2 24
c# DateTime Format validation 4 46
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question