2015-05-14 48 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庫對我來說是新的,我可以在某些事情上看到一些應用程序,所以對我們來說這是一個很好的練習。謝謝! –