【问题标题】:How to ignore and split sql commands based on Go word如何基于 Go word 忽略和拆分 sql 命令
【发布时间】:2015-12-29 18:24:44
【问题描述】:

我有一个包含several SQL Commands 的脚本。由于我需要一一拆分命令,我使用go 字来创建批处理。

Dim script = "SELECT * FROM TABLEX; " _
             & "go " _
             & "----- some coment that includes go word like ago " _
             & "INSERT INTO TABLEX .... ;" _
             & "go "
For Each sqlBatch As String In script.Split(New String() {"GO", "Go", "go"}, StringSplitOptions.RemoveEmptyEntries)
   'use sqlBatch 
Next

如果脚本有 cmets 它工作正常,但是当它碰巧有 go"ago" 这样的词时,指令会破坏字符串并且 sql 抛出错误,例如在示例中我会得到:

SELECT * FROM TABLEX;
----- some coment that includes 
 word like a
INSERT INTO TABLEX .... ;

这显然不是我想要的,我如何告诉使用 Split 来避免 cmets 并且不要在这些包含 go 单词的情况下失败? 所以我明白了:

SELECT * FROM TABLEX;
INSERT INTO TABLEX .... ;

【问题讨论】:

  • 最好的方法是使用比“go”词“更强”的东西来分割你的字符串。喜欢“|去|” (“go”被竖线字符包围)。
  • 您对如何创建此脚本有任何控制权吗?
  • more less,我可以告诉人们添加或删除一些东西,但是脚本可能> 100句

标签: sql-server vb.net string split


【解决方案1】:

您可以使用Sql Server Management objects 库。该库中的对象不会被命令之间的 GO 分隔符所欺骗,您可以使用单个 ExecuteNonQuery 提交脚本

Imports Microsoft.SqlServer.Management.Common
Imports Microsoft.SqlServer.Management.Sdk.Sfc
Imports Microsoft.SqlServer.Management.Smo
Imports System.Collections.Specialized

' Read the script. It is important to use a StringCollection'
Dim cmd = File.ReadAllText("d:\temp\create.sql")
Dim col = new StringCollection()
col.Add(cmd)

Using con = new SqlConnection(".....")
    Dim svrConnection = new ServerConnection(con)
    Dim server = new Server(svrConnection)
    server.ConnectionContext.ExecuteNonQuery(col)
End Using

要使此代码正常工作,您需要安装 Sql Server 管理对象库并添加所需的引用和 Imports 语句

Microsoft.SqlServer.ConnectionInfo.dll
Microsoft.SqlServer.Smo.dll
Microsoft.SqlServer.Management.Sdk.sfc.dll

【讨论】:

  • 批量拆分脚本的目的是执行语句然后读取结果,结果可以用于其他程序或写入excel或xml。如果我只使用一个 ExecuteNonQuery,我怎么能从所有句子中读取所有结果?
  • 不,当然不能。所以也许这不是最好的方法。但如果我没记错的话,这些库中还有一些东西可以用来解析脚本和验证语法。如果我发现有用的东西,我会环顾四周。
  • 我放弃了,找不到任何关于如何使用这个类的简单例子。
  • 我既不是我的朋友,但越少的努力就是支持
【解决方案2】:

使用来自ststeigerScriptSplitter 类完成

Dim script = "SELECT * FROM TABLEX; " _
             & Environment.NewLine & "go " _
             & Environment.NewLine & "----- some coment that includes go word like ago " _
             & Environment.NewLine & "INSERT INTO TABLEX .... ;" _
             & Environment.NewLine & "go "
Dim scs As New Subtext.Scripting.ScriptSplitter(script)
For Each str As String In scs
    Console.WriteLine(str)
Next

【讨论】:

    【解决方案3】:

    不在我的开发机器上,但我认为这可行:

        For Each sqlBatch As String In script.Split(New String() 
    {environment.newline+"GO", 
    environment.newline+"Go", 
    environment.newline+"go", 
    StringSplitOptions.RemoveEmptyEntries)
       'use sqlBatch
    

    【讨论】:

    • 如果 Go 写在空格之后,它可能会失败,不是吗?
    • 看起来你在 go 之前没有空格,但我认为你可以将“go”“go”和“go”添加到数组中,因为单词中没有空格,尽管取决于是否有合格的文本,您可能仍需要检查换行符
    猜你喜欢
    • 1970-01-01
    • 2019-11-26
    • 2013-07-09
    • 2017-10-16
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    相关资源
    最近更新 更多