Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using Regex MatchEvaluators with an SqlConnection

Posted on 2004-10-05
3
Medium Priority
?
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Assisted Solution

by:NipNFriar_Tuck
NipNFriar_Tuck earned 400 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 1600 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

730 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