Skillfully use the C # Split () function to get the action fields in the SQL statement

Source: Internet
Author: User

This is the day before the work of the request, the SQL statement operation Field gets out hanging on the tree node, feel that this function may be used later, it is summarized that the function does not implement the operation of SELECT *, as long as the addition of judgment conditions can be.

Tool function: Split () function: Splits a string into a one-dimensional array of type string.

String.Split method has 6 overloaded functions: 1) public string[] Split (params char[] separator)The returned string array contains the substrings in this instance2) public string[] Split (char[] separator, int count)The returned string array contains the substrings in this instance.  parameter specifies the substring of the returned string.Maximumnumber.
3) Public string[] Split (char[] separator, stringsplitoptions options)The string array that is returned contains the substrings in this string.  parameter specifies whether an empty array element is returned.
4) Public string[] Split (string[] separator, stringsplitoptions options)The string array that is returned contains the substrings in this string.  parameter specifies whether an empty array element is returned.
5) Public string[] Split (char[] separator, int count, stringsplitoptions options)The returned string array contains the substrings in this string, separated by the elements of the specified Unicode character array.  parameter specifies the maximum number of substrings to return, and whether to return an empty array element.
6) Public string[] Split (string[] separator, int count, stringsplitoptions options)The returned string array contains the substrings in this string, separated by the elements of the specified string array.  parameter specifies the maximum number of substrings to return, and whether to return an empty array element.

Note:StringSplitOptions is an enumeration type with two members: (1)none: The return value includes an array element containing an empty string, and (2) removeemptyentries: The return value does not include an array element   that contains an empty string. Specific examples can be found online to find the relevant code. idea: splitting a string into a string array through the split () function and then judging the string data. Effect:
code:
         <summary>///Get query field code///</summary>//<param name= "Soperationsql" >        </param>//<returns></returns> private string Getsqloperatefield (String soperationsql) {if (string. IsNullOrEmpty (Soperationsql)) {return string.            Empty; } string sappendnodetext = String.            Empty;            Soperationsql = Soperationsql.trim ();            string[] strlist = null;            String SKey = soperationsql.substring (0, 6); Switch (Skey.toupper ()) {case "select": {strlist = s                        Operationsql.split (new char[2] {", ', '}); for (int i = 1; i < strlist.length; i++) {if (Strlist[i]. ToUpper ().                            Equals ("from")} {break;         }                   if (!string. IsNullOrEmpty (Strlist[i])) {Sappendnodetext + = Strlist[i] + ","                            ;                            }} if (Sappendnodetext.length > 1) {                        Sappendnodetext = sappendnodetext.substring (0, sappendnodetext.length-1);                        } sappendnodetext = "select" "+ Sappendnodetext +" ";                    Break } case "INSERT": {strlist = Soperationsql.trim ().                        Split (new char[5] {', ', ', ' (', ') ', ' \ '}); for (int i = 0; i < strlist.length; i++) {if (Strlist[i]. ToUpper ().                            Equals ("WHERE")) {break; } if (!string. IsNUllorempty (Strlist[i]) && Strlist[i]. IndexOf ("=") >-1 && strlist[i]. Length > 1) {int iIndex = Strlist[i].                                IndexOf ("="); if (IIndex > 0) {sappendnodetext + = Strlist[i].                                Substring (0, IIndex) + ","; }} if (!string. IsNullOrEmpty (Strlist[i]) && Strlist[i]. IndexOf ("=", 0, 1) >-1) {Sappendnodetext + = Strlist[i-1]                            + ",";                            }} if (Sappendnodetext.length > 1) {                        Sappendnodetext = sappendnodetext.substring (0, sappendnodetext.length-1);     } sappendnodetext = "INSERT" "+ Sappendnodetext +" ";                   Break } case "UPDATE": {strlist = Soperationsql.trim ().                        Split (new char[2] {", ', '}); for (int i = 0; i < strlist.length; i++) {if (Strlist[i]. ToUpper ().                            Equals ("WHERE")) {break; } if (!string. IsNullOrEmpty (Strlist[i]) && Strlist[i]. IndexOf ("=") >-1 && strlist[i]. Length > 1) {int iIndex = Strlist[i].                                IndexOf ("="); if (IIndex > 0) {sappendnodetext + = Strlist[i].                                Substring (0, IIndex) + ","; }} if (!string. IsNullOrEmpty (Strlist[i]) && sTrlist[i]. IndexOf ("=", 0, 1) >-1) {Sappendnodetext + = Strlist[i-1]                            + ",";                            }} if (Sappendnodetext.length > 1) {                        Sappendnodetext = sappendnodetext.substring (0, sappendnodetext.length-1);                        } sappendnodetext = "UPDATE" "+ Sappendnodetext +" ";                    Break } case "DELETE": {strlist = Soperationsql.trim ().                        Split (new char[2] {", ', '}); for (int i = 0; i < strlist.length; i++) {if (!string. IsNullOrEmpty (Strlist[i]) && Strlist[i]. IndexOf ("=") >-1 && strlist[i]. Length > 1) {int iIndex = Strlist[i]. IndexOf ("="); if (IIndex > 0) {sappendnodetext + = Strlist[i].                                Substring (0, IIndex) + ","; }} if (!string. IsNullOrEmpty (Strlist[i]) && Strlist[i]. IndexOf ("=", 0, 1) >-1) {Sappendnodetext + = Strlist[i-1]                            + ",";                            }} if (Sappendnodetext.length > 1) {                        Sappendnodetext = sappendnodetext.substring (0, sappendnodetext.length-1);                        } sappendnodetext = "DELETE" "+ Sappendnodetext +" ";                    Break        }} return sappendnodetext; }

Test Case:
            String sselsql = "Select BSM from STUDENT";            String sresult = Getsqloperatefield (sselsql);            This.treeList1.AppendNode (new object[] {sresult}, null);            String supdata = "UPDATE STUDENT SET name= ' JAMES '";            Sresult = Getsqloperatefield (supdata);            This.treeList1.AppendNode (new object[] {sresult}, null);            String sinsertsql = "INSERT into STUDENT name= ' LUCY ', id= ' 2001 ' WHERE score= '";            String sinsertsql = "INSERT into Persons (LastName, Address) VALUES (' Wilson ', ' champs-elysees ')";      This writing            Sresult = Getsqloperatefield (sinsertsql) is not supported at this stage;            This.treeList1.AppendNode (new object[] {sresult}, NULL);

It is very simple and needs to be perfected.

Skillfully use the C # Split () function to get the action fields in the SQL statement

Related Article

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.