Solved

Using Regex MatchEvaluators with an SqlConnection

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

18 Experts available now in Live!

Get 1:1 Help Now