VB - 获取 IN 语句中的字符串

VB - Get the strings inside IN statement

在 .Net 2.0 中使用 VB,我想创建一个简单的程序,从我用这样的视图创建的脚本(文本文件)中创建一个 xml 文件:

USE [Archive-FIRSTVIEW]
GO
/****** Object:  View [dbo].[vABCDEFG]    Script Date: 5/14/2015 8:01:58 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[vABCDEFG]
AS
SELECT        dbo.[Document].docGUID, dbo.[Document].accountGUID, dbo.[Document].jobGUID, dbo.[Document].DOC9docID, dbo.[Document].docDate, dbo.[Document].docType, 
                     dbo.[Document].docPages, dbo.[Document].tsCreated
FROM            dbo.AcctLookupKey INNER JOIN
                     dbo.[Document] ON dbo.AcctLookupKey.docGUID = dbo.[Document].docGUID
WHERE        (dbo.AcctLookupKey.ix = 3) AND (dbo.AcctLookupKey.val IN ('ABCDEFG'))


GO
/****** Object:  View [dbo].[vLMNOP]    Script Date: 5/14/2015 8:01:58 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[vLMNOP] AS


SELECT        dbo.[Document].*
FROM            dbo.AcctLookupKey INNER JOIN  dbo.[Document] ON dbo.AcctLookupKey.docGUID = dbo.[Document].docGUID
WHERE        (dbo.AcctLookupKey.ix = 3) 
AND (dbo.AcctLookupKey.val in ('LMNOP','LMNOP (AEIOU)'))

我只想检索每个 WHERE 行末尾的文本,例如,第一个检索 'ABCDEFG',第二个检索 'LMNOP'、[=34] =]

感谢您的帮助!

编辑: 我想创建一个看起来像这样的 XML 文件:

<Views Code="FIRSTVIEW">
    <View Name="vABCDEFG">
        <Criteria>ABCDEFG</Criteria>
    </View>
    <View Name="LMNOP">
        <Criteria>LMNOP</Criteria>
        <Criteria>LMNOP (AEIOU)</Criteria>
    </View>
<Views>

编辑2: 到目前为止,已经能够开始提取一些数据,只是不是我想要的样子。这是在将它加载到 xml.

之前
Dim dir As New DirectoryInfo("d:\input")
    Dim sw As New StreamWriter("d:\input\extract.txt")

    For Each fi As FileInfo In dir.GetFiles("views.txt")
        Dim sr As New StreamReader(fi.FullName)
        Dim root As String
        Dim child As String
        Dim substring As String
        While Not sr.EndOfStream
            Dim sLine As String = sr.ReadLine

            If sLine.Contains("USE [") Then
                root = sLine '.Substring(13) - 1
            End If

            If sLine.Contains("CREATE VIEW") Then
                child = sLine '.Substring(19) - 1
            End If

            If sLine.Contains("(''") Then
                substring = sLine
            End If

            sw.WriteLine(root)
            sw.WriteLine(child)
            sw.WriteLine(substring)
        End While
        sr.Close() : sr.Dispose()
    Next
    sw.Flush() : sw.Close() : sw.Dispose()

这非常 hacky,但它让您知道去哪里。使用 Microsoft.SqlServer.TransactSQL.ScriptDom 库解析 TSQL。可能 dependable/flexible 比在输入中查找特定字符串要多得多。

Imports Microsoft.SqlServer.TransactSql.ScriptDom

Module Module1

    Sub Main()
        Dim fragment As TSqlFragment
        Dim parser As New TSql120Parser(True)
        Dim reader As System.IO.StreamReader
        Dim errors As IList(Of ParseError)

        reader = IO.File.OpenText("script.sql")

        fragment = parser.Parse(reader, errors)

        Dim foundIn As Boolean = False
        Dim foundView As Boolean = False
        Dim viewName As String = ""

        For Each tkn As TSqlParserToken In fragment.ScriptTokenStream
            If tkn.TokenType = TSqlTokenType.View Then
                foundView = True
            End If
            If tkn.TokenType = TSqlTokenType.In Then
                foundIn = True
            End If

            'Once you see a View, take everything until you see an As
            If foundView = True And tkn.TokenType = TSqlTokenType.As Then
                Console.WriteLine(viewName.Trim)
                viewName = ""
                foundView = False
            ElseIf foundView = True Then
                viewName += tkn.Text
            End If

            'Once you see an IN, collect the ASCII elements until a right parentheses is encountered.
            If tkn.TokenType = TSqlTokenType.AsciiStringLiteral And foundIn = True Then
                Console.WriteLine(tkn.Text)
            ElseIf tkn.TokenType = TSqlTokenType.RightParenthesis And foundIn = True Then
                'end of the IN condition
                foundIn = False
            End If
        Next

        Console.ReadKey()
    End Sub

End Module

输出这个...