难以从 C# 执行 "Execute" 查询

Difficulty Executing "Execute" Query From C#

我有这个查询在托管供应商应用程序的 SQLServer 上运行。

exec sp_configure 'show advanced options',1
RECONFIGURE 
exec sp_configure 'Ole Automation Procedures', 1
RECONFIGURE

exec sp_configure 'show advanced options', 0
RECONFIGURE 

exec CreateMunisExportFile

exec sp_configure 'show advanced options',1
RECONFIGURE

exec sp_configure 'Ole Automation Procedures', 0
RECONFIGURE

exec sp_configure 'show advanced options', 0
RECONFIGURE

我想使用 C# 远程执行它。

我正在使用 Visual Studio 2015,但无法找到允许您创建 Server 对象的 SQLServer 模块的访问权限。我丢失了 post 的 link,但也在 SO this-post, and that-post 中查看了以下和其他 post。

我在以下函数中从 ExecuteNonQuery 得到 -1,想知道如何获得更多错误信息。

   private void btRunQuery_Click(object sender, EventArgs e)
    {
        using (var conn = new SqlConnection("Data Source=SERVER\TICKETTRAK;Initial Catalog=TTRAK9.4.20;User Id=sa;Password=xxxyyyyz;Pooling=false"))
        {
                 string script = File.ReadAllText(@".\SpecialTickeTrakExport_20181106.sql");
                 using (SqlCommand mySqlCmd = new SqlCommand(script))
                 {
                     conn.Open();
                     mySqlCmd.Connection = conn; 
                     var result = mySqlCmd.ExecuteNonQuery();
                     conn.Close();
                 }
        }
    }

没什么问题。

documentation for ExecuteNonQuery 说:

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

由于您正在执行的命令不是 UPDATEINSERTDELETE,因此 return 值将始终是 -1