Solved

Using Regex MatchEvaluators with an SqlConnection

Posted on 2004-10-05
3
343 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Search by two fields at the same time using c# radio button 5 39
DateTimepicker 4 33
Error on link 14 38
C# guarantee sql connection close 6 30
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now