我们在 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();