Demo of using Regex to replace fields in SQL statements

Source: Internet
Author: User

These two days in the demo. The function is implemented in a template (string Type, which is actually an SQL select query statement), and a regular expression is used to query fields that meet the conditions, then, replace other values with the actual available query statements and display them.

This is because you are not quite clear about what you need. So after a while, I figured out my thoughts.

The general implementation steps are as follows:

1. Because the template is in an arrayinfo, find arrayinfo first, and then find the template contained in arrayinfo, take this value out and assign it to another string type variable (this is just clear );

IEnumerator IEatorArrayinfoList = arrayinfo. GetEnumerator ();

While (IEatorArrayinfoList. MoveNext ())

{

RuleTypeInfo ruleInfo = (RuleTypeInfo) IEatorArrayinfoList. Current;

If (cboRuleType. SelectedValue. ToString () = ruleInfo. RuleTypeID. ToString ())

{

StrSelect = ruleInfo. RuleTypeTemplate;

}

}

2. Use a regular expression (Regex ). It took some time to use the regular expression, because everything learned between them was forgotten. I had to look for another article, "getting started with regular expression 30", because this article is quite basic and easy to understand. It probably means understanding, but the actual operation is still a bit difficult. So when I thought about it, I wrote two regular expressions and added them to the ArrayList, which is the same as what I needed.
// Obtain the fields to be replaced

Public static ArrayList showMatches (string expression, RegexOptions option, string MS)

{

Regex regex = new Regex (expression, option );

MatchCollection matches = regex. Matches (MS );

ArrayList list = new ArrayList ();

Foreach (Match m in matches)

{

List. Add (m. ToString ());

}

Return list;

}

// Obtain the fields extracted using regular expressions (a field with a regular expression and a field with a pure expression)

ListSign = showMatches (@ "" & "w *" d * ".", RegexOptions. None, strSelect );

ListField = showMatches (@"(? <= &) "W *" d * [^ &.] (? = ".)", RegexOptions. None, strSelect );

3. Add the name and value of the value to be replaced to HashTable (the value is key and the value is value ).

// Fill the parameters in paralist with hashtable

This. gridParameters. Items. Refresh ();

TxtName. Focus ();

IEnumerable IEnumberTableList = this. gridParameters. Items. SourceCollection;

IEnumerator IEatorList = IEnumberTableList. GetEnumerator ();

While (IEatorList. MoveNext ())

{

ParamersGridInfo infoList = (ParamersGridInfo) IEatorList. Current;

HtRulePara. Add (infoList. RuleTypeParaName, infoList. RuleTypeParaValue );

}

4. Then, cyclically compare the key in HashTable with the field (ArrayList [I]) found using the regular expression. If the key is the same, the string is used. replace can be replaced, and then a new SQL select query statement is created.

// Obtain the replaced SQL select statement

Foreach (DictionaryEntry de in htRulePara)

{

For (int I = 0; I <listField. Count; I ++)

{

If (de. Key. ToString () = listField [I]. ToString ())

{

StrSelect = strSelect. Replace (listSign [I]. ToString (), de. Value. ToString ());

}

}

}

MessageBox. Show (strSelect, Constant. SystemTitle, MessageBoxButton. OK, MessageBoxImage. Information );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.