调用exec sp时过程正常工作,但使用process.exit()命令从c#执行时失败

2024-03-28 18:43:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我能够从SSMS成功执行dbo.sp_addlinkedserver,但在从c#使用process.WaitForExit()命令行参数执行时,它失败,出现以下错误

错误:(15002,b“无法在事务中执行过程'sys.sp_dropserver'。DB-Lib错误消息20018,严重性16:\n一般SQL Server错误:检查来自SQL Server的消息\n”)

--------------程序------------------

   EXEC master.dbo.sp_dropserver @server=N'test', @droplogins='test'
  GO


   /****** Object:  LinkedServer [ASLODS]    Script Date: 1/28/2021 3:40:13 AM ******/
   EXEC master.dbo.sp_addlinkedserver @server = N'test', @srvproduct=N'test', 
  @provider=N'OraOLEDB.Oracle', @datasrc=N'test'
    /* For security reasons the linked server remote logins password is changed with ######## */

EXEC master.dbo.sp_serveroption @server=N'test', @optname=N'remote proc transaction promotion', 
@optvalue=N'true'
GO
GO


     

调用过程文件以执行的c#代码

      using (Process process = new Process())
        {process.StartInfo.FileName = filename;
            process.StartInfo.Arguments = arguments;

            if(workingDir != null)
            {
                process.StartInfo.WorkingDirectory = workingDir;
            }               
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            .......................
             .........................
             ...........................
            Task<bool> waitForExit = WaitForExitAsync(process, timeout);
            result.ExitCode = process.ExitCode;  //Here it fails. 

I want to execute addLinkedServer procedure from C#.