gpt4 book ai didi

c# - 从 SQL Server 代理(作业)运行 C# 控制台应用程序?

转载 作者:太空狗 更新时间:2023-10-29 20:42:09 24 4
gpt4 key购买 nike

这可能是一个非常简单的问题,但我已经尝试了 4-5 个小时,但没有成功。 :(

我有一个 C# 控制台应用程序,它只打开一个 excel 文件。这个 excel 文件有 Workbook_Open() 事件,它运行我的宏。我的宏只是将事件工作表中的 sheet1 重命名为 RenameSheet1。

我可以从 IDE 运行我的 C# 项目。我想从 SQL 作业(SQL Server 2008)运行这个项目。我该怎么做?请帮我解决这个问题。谢谢。

根据 SilverNinnjas 创建代理帐户的建议:

-- 创建包含域帐户 CORP\PowerUser1 及其密码的凭据

CREATE CREDENTIAL PowerUser1 WITH IDENTITY = N'CORP\shress2', SECRET = N'P@ssw0rd'
GO
USE [msdb]
GO

-- 创建一个名为 ExcelProxy 的新代理并为其分配 PowerUser 凭证

EXEC msdb.dbo.sp_add_proxy 
@proxy_name=N'ExcelProxy',
@credential_name=N'PowerUser1',
@enabled=1

-- 授予 ExcelProxy 访问“CmdExec”子系统的权限

EXEC msdb.dbo.sp_grant_proxy_to_subsystem 
@proxy_name=N'ExcelProxy',
@subsystem_name =N'CmdExec'

-- 授予登录testUser使用ExcelProxy的权限

EXEC msdb.dbo.sp_grant_login_to_proxy 
@login_name = N'shress2',
@proxy_name=N'ExcelProxy'
GO

我仍然遇到同样的错误作为用户执行:CORP\shress2。

Unhandled Exception: System.Runtime.InteropServices.COMException: Microsoft Excel cannot access the file 'E:\data_extracts\RenameSheets.xlsm'. There are several possible reasons:
The file name or path does not exist.
The file is being used by another program.
The workbook you are trying to save has the same name as a currently open workbook. at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad) at T_OpenExcel.Program.Main(String[] args) in C:\Users\shress2\documents\visual studio 2010\projects\T_OpenExcel\T_OpenExcel\Program.cs:line 24. Process Exit Code -532462766. The step failed.

有什么原因吗?我急切地等待任何反馈。非常感谢。

@SilverNinja,这是我的 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Threading;


namespace T_OpenExcel
{
class Program
{
static void Main(string[] args)
{

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
//Excel.Worksheet xlWorkSheet;

object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.Application();
xlApp.Visible = true;
xlWorkBook = xlApp.Workbooks.Open("\\\\myserver\\data_extracts\\RenameSheets.xlsm", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

xlApp.DisplayAlerts = false;
xlWorkBook.SaveAs("\\\\myserver\\data_extracts\\RenameSheets.xlsm", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);


xlWorkBook.Close(true, misValue, misValue);
xlApp.DisplayAlerts = true;

xlApp.Quit();
}

private static void RunMacro(Excel.Workbook xlWorkBook, object[] p)
{
//throw new NotImplementedException();
}
}
}

最佳答案

您只需在新建作业步骤 编辑器中选择合适的作业类型。您可以使用 PowershellCmdExec

在命令区域中,单击打开 按钮以找到您的控制台应用程序已编译的可执行文件 (exe)。

如果您有任何参数,请在此处添加它们 - 否则配置计划。

您可能必须使用提升的权限。要使用提升的权限,只需在 SSMS 中导航至 Security->Credentials 并右键单击 New Credential。接下来,在 Sql Server Agent->Proxies 下配置 Proxy Account 并右键单击 New Proxy。为 CmdExec 配置代理并使用您之前设置的Credentials。在您的 SQL 代理作业步骤中,您可以选择此凭据以在运行命令时使用。

在 SQL 作业步骤命令区域中,您应该键入如下内容:

excel E:\data_extracts\RenameSheets.xlsm

关于c# - 从 SQL Server 代理(作业)运行 C# 控制台应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10691408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com