2

我们在 sql 文件中使用批量 sql 脚本集,并在运行时将该文件加载到 C# 中的文本变量。因此,我通过 Oledb 连接和命令类型作为“文本”执行以下查询。不幸的是,它给出了以下错误。

Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Incorrect syntax near 'GO'.

SQL 查询语法

  IF exists  (
                SELECT  '1' 
                FROM    sysobjects 
                WHERE   id = object_id(N'SP_Name')  
                and     objectproperty(id, N'isprocedure') = 1
               )
    BEGIN
        DROP PROC SP_Name 
    END
    GO
    Create PROCEDURE SP_Name 
     (  
       @var_id NUMERIC(10),
       @var_id1 NVARCHAR(100),
       @var_id_Out1     numeric(10)     output,
       @var_id_Out2     integer         output,
       @var_id_Out3     nvarchar(2000)  output
    )  
    WITH ENCRYPTION  
    AS  
    BEGIN  
    SET NOCOUNT ON  
        select * from demotable
    SET NOCOUNT OFF  
    END 
    GO

GRANT   EXEC ON SP_Name TO PUBLIC
GO

C# 代码片段

OleDbCommand cmd;
OleDbConnection co = new OleDbConnection("Provider=SQLOLEDB;Data Source=servername;Database=DB_Name;User Id=sa;Password=mypass;");
co.Open();
String[] strFileList = Directory.GetFiles(@"D:\Procedure");
String conntent = null;
foreach (string commandSP in strFileList)
{
       conntent = File.ReadAllText(commandSP);
       cmd = co.CreateCommand();
       cmd.CommandText = conntent;     
       cmd.ExecuteNonQuery();
}
co.Close();
4

2 回答 2

2

GO 命令是 T-SQL 语法...由前端解释为分开的批次...OLEDB 和服务器都不会理解该语句...

为了让它与 OLEDB 一起使用,您可以自己拆分批次并单独运行每个批次

于 2013-04-11T06:12:32.337 回答
2

GO 不是 T-SQL 关键字,即它不是语言的一部分,它只是一些客户端(例如 Management Studio)用来分隔批次的标记

于 2013-04-11T06:14:12.803 回答