如何在c#中查找两个相同字符串之间的文本

时间:2016-03-16 08:18:02

标签: c# sql-server winforms

我正在尝试从“存储过程”中获取“选择”和“来自”之间的字段名称。但问题是在同一程序内,如果有多个选择,那么如何获取字段。 例如:

IF 1=2 BEGIN

    SELECT Code = NULL, Name = NULL, ItemCount = NULL, SalesAmount = NULL, Quantity = NULL, HeaderID = NULL, Hour = NULL, Operator = NULL, RepTypeName = NULL, TotalSalesAmount = NULL
END;

DECLARE @StartTimeCode VarChar(10), @EndTimeCode VarChar(10);

SELECT @StartTimeCode = StartTimeCode, @EndTimeCode = EndTimeCode
FROM dbo.vDim_Time
WHERE Code = @TimeCode;

SELECT A.ProductCode, A.ProductName, B.DEPTCode, A.GroupCode, A.CatCode, A.Price, A.Size, A.Measure
INTO #MiscProduct
FROM dbo.Dim_Product A
JOIN dbo.dim_Group B ON A.GroupCode = B.GroupCode
WHERE A.StoreCode = @StoreCode
AND A.ProductName LIKE 'MISC %' AND LEN(A.ProductCode) = 6;

我编写的代码仅从第一个选择发生的地方返回字段的名称。即

代码:

protected void GetFieldsAndFormulaFromStoredProcedure(DataTable     pdtUserSelectedStoredProcedure)

    {
         string TempStoredProcedureName = string.Empty,  
         TempStoredProcedureText = string.Empty, FieldName = string.Empty,         FieldFormula=string.Empty;
        List<string> listFields = new List<string>();
        foreach (DataRow dr in pdtUserSelectedStoredProcedure.Rows)
        {
            TempStoredProcedureName = dr["StoredProcedureName"].ToString();
            TempStoredProcedureText = dr["StoredProcedureText"].ToString();

            if (TempStoredProcedureText.ToLower().Contains("select") && TempStoredProcedureText.ToLower().Contains("from"))
            {
                int pFrom=           TempStoredProcedureText.ToLower().IndexOf("select") + "Select".Length;
                int pTo = TempStoredProcedureText.ToLower().IndexOf("from");
                FieldName = TempStoredProcedureText.Substring(pFrom, pTo - pFrom);
                listFields.Add(FieldName);
            }
        }
    }

在listFields中,我只能从选择下面获取字段名称。

  

SELECT Code = NULL,Name = NULL,ItemCount = NULL,SalesAmount = NULL,   Quantity = NULL,HeaderID = NULL,Hour = NULL,Operator = NULL,   RepTypeName = NULL,TotalSalesAmount = NULL

如何在同一过程中选择Select和From之间的其余字段名称。任何帮助将不胜感激。

0 个答案:

没有答案