2015-05-14 96 views
1

.NET 2.0中使用VB里面工作的串,我想创建一个简单的程序,使得XML文件进行脚本(文本文件)我浏览这样的创建:VB - 得到声明

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”,“LMNOP(AEIOU)”

感谢您的帮助!

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

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

EDIT2: 到目前为止,已经能够开始提取一些数据,只是不是我想要的样子。这是之前甚至加载到一个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() 
+0

我不明白你试图在这里做。你能否试着澄清一下? –

+0

@Sean Lange,我会尽快更新我的帖子。 – Gmac

+0

我会改变你的问题,而是问“我想要得到IN语句中的字符串” –

回答

1

这是非常hacky,但它给你一个想法去哪里。使用Microsoft.SqlServer.TransactSQL.ScriptDom库来解析TSQL。可能比在输入中查找特定字符串更可靠/灵活。

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 

输出这... enter image description here

+0

这项工作适合你吗? –

+0

对不起,迟到了,是的,这让我走上了正轨。谢谢! – Gmac

+0

太棒了! ScriptDom库对我来说是新的,我可以在某些事情上看到一些应用程序,所以对我们来说这是一个很好的练习。谢谢! –