2016-05-24 107 views
0

我试图与SQL服务器连接并在安装安装程序时执行一个脚本文件。我设法执行了一个没有GO声明的简单脚本。Inno Setup在安装过程中执行一个大型的sql脚本文件

问题:有没有办法通过(跳过)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 [email protected] 
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用于除我来说,我必须包括在我的脚本GO以类似的方式TLama's答案How to connect to MS SQL Server using InnoSetup?连接。

谢谢。

+0

不确定是什么问题。只需在'.sql'文件中包含'go'即可。 –

+0

@Martin Prikryl问题是'GO'是一个批终止符,并在ADOCommand.Execute()中使用它是不正确的。抛出一个异常,例如“GO附近语法不正确”。“ – abdul

+0

与按顺序执行多个单独的SQL脚本不一样吗? –

回答

0

您可以拆分SQL脚本以分隔长为go语句的脚本文件,并按顺序逐个执行它们。


如果这不是一种选择,你必须使用API​​ /工具,支持go声明,即sqlcmd工具,而不是ADO。


或者只是在通过ADO执行脚本之前加载脚本文件并删除go语句。可以使用StringChange function

+0

对不起,我不在工作。我尝试了StringChange函数,它不起作用抛出'CREATE/ALTER PROCEDURE'必须是查询批处理中的第一条语句。我认为sqlcmd是我唯一的选择。我对此一无所知,如果有关于如何在innosetup上使用它的例子或链接。谢谢 – abdul

+0

你可以在你的问题中附加一个脚本示例以及你的'StringChange'代码吗? –

+0

你甚至不能删除所有'SET ANSI_NULLS ON'和'SET QUOTED_IDENTIFIER ON'吗? –

相关问题