Inno Setup 在安装期间执行大型 sql 脚本文件

Inno Setup Executing a large sql script file during installation

我正在尝试连接 SQL 服务器并在安装安装程序时执行脚本文件。我设法执行了一个没有 GO 语句的简单脚本。

问题:有没有办法传递(跳过)GO字并执行脚本?

 ADOConnection.ConnectionString :=
      'Provider=SQLOLEDB;' +  
        'Data Source=' + ServerEdit.Text + ';' +
        'User Id=' + UsernameEdit.Text + ';' +
        'Password=' + PasswordEdit.Text + ';' + 
        'Trusted_Connection=no;';
  end;

 ADOConnection.Open;
try

  ADOCommand := CreateOleObject('ADODB.Command');
  ADOCommand.ActiveConnection := ADOConnection;
  ScriptPath := ExpandConstant('{tmp}\Script2.sql');

  if LoadStringFromFile(ScriptPath, ssquery) then
  begin
   StringChangeEx(ssquery, 'GO', '', True);
    SQLQuery :=  ssquery
    ADOCommand.CommandText := SQLQuery;
    ADOCommand.Execute(NULL, NULL, adCmdText or adExecuteNoRecords);
    Result := True;
  end;
finally
  ADOConnection.Close;
  end;

Script2.sql

USE northwind3

GO
/****** Object:  StoredProcedure [dbo].[Customers By City]    Script Date: 5/25/2016 8:35:45 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[Customers By City]
    -- Add the parameters for the stored procedure here
    (@param1 NVARCHAR(20))
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    SELECT CustomerID, ContactName, CompanyName, City from Customers as c where c.City=@param1
END


GO
/****** Object:  StoredProcedure [dbo].[Customers Count By Region]    Script Date: 5/25/2016 8:35:45 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Customers Count By Region]
    -- Add the parameters for the stored procedure here
    (@param1 NVARCHAR(15))
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    DECLARE @count int
    SELECT @count = COUNT(*)FROM Customers WHERE Customers.Region = @Param1
    RETURN @count
END

注意:我正在以类似的方式使用 ADOB 进行连接 TLama's answer to How to connect to MS SQL Server using InnoSetup? 除了在我的情况下,我必须在我的脚本中包含 GO

谢谢。

您可以拆分 SQL 脚本以将脚本文件与 go 语句分开,并按顺序单独执行它们。


如果这不是一个选项,您必须使用支持 go 语句的 API/tool,即 sqlcmd 工具,而不是 ADO。


或者在通过 ADO 执行脚本之前加载脚本文件并删除 go 语句。

您可以使用 StringChange function.