你如何解析大的SQL脚本分批?脚本、SQL

2023-09-04 00:43:50 作者:蹲墙角沉默

我有我要分手成执行批量非常大的SQL文件。 我想确保我解析它以同样的方式SSMS和SQLCMD做。

I have a very large sql file I want to break up into batches for execution. I want to make sure I'm parsing it the same way that SSMS and SQLCMD do.

微软有一个名为Microsoft.SqlServer.BatchParser一类名为解析器,接缝像它会做的伎俩一个伟大的混合模式组件。

Microsoft has a great mixed mode assembly named Microsoft.SqlServer.BatchParser with a class named Parser that seams like it would do the trick.

它想IBatchSource作为一个参数实现调用解析()之前SetBatchSource。

It wants an implementation of IBatchSource as an argument to SetBatchSource before calling Parse().

我在哪里可以找到如何使用这个功能的实现IBatchSource,更多的信息?

Where can I find an implementation of IBatchSource, and more information on how to make use of this functionality?

推荐答案

我发现在GAC大会Microsoft.SqlServer.BatchParser连同它包含的实现朋友Microsoft.SqlServer.BatchParserClient接口IBatchSource。

I found the assembly Microsoft.SqlServer.BatchParser in the GAC along with it's friend Microsoft.SqlServer.BatchParserClient that contains implementations the interface IBatchSource.

namespace Microsoft.SqlServer.Management.Common
{
  internal class BatchSourceFile : IBatchSource
  internal class BatchSourceString : IBatchSource
}

下面的谈话,然后发生了。

The following conversation then occurred.

大会:的啰!我的名字是    Microsoft.SqlServer.Management.Common.ExecuteBatch 。你想StringCollection GetStatements(字符串的SqlCommand)?

Assembly: Hello! My name is Microsoft.SqlServer.Management.Common.ExecuteBatch. Would you like to StringCollection GetStatements(string sqlCommand)?

我:的是,我会的,BatchParserClient组装。谢谢你的邀请!

Me: Yes, I would, BatchParserClient assembly. Thanks for asking!

重复说明(不要在家里尝试这个!)

Repeatable Instructions (Do try this at home!)

在安装的Microsoft SQL Server 2008 R2共享管理对象 复制Microsoft.SqlServer.BatchParser.dll和Microsoft.SqlServer.BatchParserClient.dll从GAC到解决方案中的一个文件夹。 参考Microsoft.SqlServer.BatchParser和放大器; Microsoft.SqlServer.BatchParserClient

的Program.cs

using System;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using Microsoft.SqlServer.Management.Common;

namespace ScriptParser
{
   class Program
   {
      static void Main(string[] args)
      {
         ExecuteBatch batcher = new ExecuteBatch();
         string text = File.ReadAllText(@"Path_To_My_Long_Sql_File.sql");
         StringCollection statements = batcher.GetStatements(text);
         foreach (string statement in statements)
         {
            Console.WriteLine(statement);
         }
      }
   }
}

的app.config

<?xml version="1.0"?>
<configuration>
   <startup useLegacyV2RuntimeActivationPolicy="true">
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
   </startup>
</configuration>
 
精彩推荐
图片推荐