【问题标题】:c# extract sql query sections from stringc#从字符串中提取sql查询部分
【发布时间】:2018-11-17 12:59:23
【问题描述】:

大家好,我正在寻找一种干净的方法来提取我的 SQL 查询字符串的各个部分。

我的意思是:

假设我有这样的查询:

SELECT 
    poc, event, network, vendor 
FROM 
    bLine 
WHERE 
    network = 'something' 
AND 
    event = 'simple' 
OR 
    event != 'hard' 
ORDER BY 
    poc ASC

我正在寻找将每个部分拆分为自己的部分(使用字典作为每个存储 2 个值的一种方式):

Key      |Value
-------------------------------------
SELECT   |poc, event, network, vendor
FROM     |bLine
WHERE    |network = 'something' 
AND      |event = 'simple' 
OR       |event != 'hard' 
ORDER BY |poc 
??       |ASC

有人碰巧有这样的东西吗?我已经在 SO 上尝试过 THIS 示例,因为这似乎是我想要做的,但代码似乎不起作用:

错误提示:

错误 CS0117 'TokenInfo' 不包含 'Start' 的定义

错误 CS0117 'TokenInfo' 不包含 'End' 的定义

错误 CS0117“TokenInfo”不包含“IsPairMatch”的定义

ETC 等....

更新

似乎这个 SO 示例 HERE 做了我需要它做的事情并且没有错误! :)

【问题讨论】:

  • 一些不同的问题 - 更复杂的语句,如Select into...Group By、多个Joins、嵌套SQL 等。但是,鉴于上面的示例,多个@987654330 怎么样@s / Ors 或 Where 子句中的嵌套(括号)标准?....这可能不是解决此问题的正确方法....
  • 它不会有 INTO、GROUP、JOINS 等它只会有我在 OP 查询中列出的内容。

标签: c# regex parsing extract indexof


【解决方案1】:

如果你一直有一个固定的字符串,你可以使用这样的东西。这是我解决问题的尝试,我可以说这不是最有效或最先进的方法,但它是一种方法。

        string[] Params = { "SELECT", "FROM", "WHERE", "AND", "OR", "ORDER BY" };
        Dictionary<string, string> sqlSource = new Dictionary<string, string>();
        string sqlString = "SELECT poc, event, network, vendor FROM bLine WHERE network = 'something'";
        int i, j;

        //iterate through all the items from Params array
        for (i = 0; i < Params.Length; i++)
        {

            string result = "";

            //take next element from Params array for SELECT next will be FROM
            for (j = i + 1; j < Params.Length; j++)
            {
                int ab = sqlString.IndexOf(Params[j]);
                //Get the substring between SELECT and FROM
                if (ab > 0)
                {
                    int pFrom = sqlString.IndexOf(Params[i]) + Params[i].Length;
                    int pTo = sqlString.LastIndexOf(Params[j]);
                    result = sqlString.Substring(pFrom, (pTo - pFrom));
                }


                //if there is a string then break the loop  
                if (result != "")
                {
                    sqlSource.Add(Params[i], result);
                    break;
                }
            }

            //if result is empty which is possible after FROM clause if there are no WHERE/AND/OR/ORDER BY then get substring between FROM and end of string
            if (result == "")
            {
                int pFrom = sqlString.IndexOf(Params[i]) + Params[i].Length;
                int pTo = sqlString.Length;
                result = sqlString.Substring(pFrom, (pTo - pFrom));
                sqlSource.Add(Params[i], result);
                break;
            }

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 2011-08-30
    • 2017-04-05
    • 1970-01-01
    相关资源
    最近更新 更多