我試圖與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?連接。
謝謝。
不確定是什麼問題。只需在'.sql'文件中包含'go'即可。 –
@Martin Prikryl問題是'GO'是一個批終止符,並在ADOCommand.Execute()中使用它是不正確的。拋出一個異常,例如「GO附近語法不正確」。「 – abdul
與按順序執行多個單獨的SQL腳本不一樣嗎? –