簡體   English   中英

通過SQL AGENT作業運行SSIS包

[英]Run SSIS Package via SQL AGENT job

我對SSIS很好,我創建了一個程序包,它將excel文件加載到SQL SERVER表中。

我編寫了C#代碼以僅提取第一個工作表名稱,然后程序包將加載該第一個工作表中的數據(僅)。

通過BIDS運行該程序包時,該程序運行良好。 但是,當我通過SQL AGENT JOB運行此程序包時,同一程序包在C#代碼步驟中引發了錯誤。

我看到很多帖子暗示這可能是由於在C#代碼中使用了Microsoft.interop.excel引用。

C#中是否有任何其他方法可以不使用互操作庫來提取excel文件的第一個工作表名稱(不基於升序)。

另外,當在代碼內部使用互操作時,如何配置SQL AGENT作業以成功運行?

我的服務器是64位。

我嘗試在“ C:\\ Windows \\ System32 \\ config \\ systemprofile \\”中創建“ Desktop”文件夾

錯誤:描述:System.Reflection.TargetInvocationException:調用的目標引發了異常。 ---> System.NullReferenceException:對象引用未設置為對象的實例。 在ST_ecfa668f250a45e18c95639c9ffd64d4.csproj.ScriptMain.Main()-內部異常堆棧跟蹤的結尾--在System.RuntimeMethodHandle._InvokeMethodFast(對象目標,Object []參數,SignatureStruct&sig,MethodAttributes methodAttributes,RuntimeTypeHandle typeOwner)在SystemRun。 .System.Reflection.RuntimeMethodInfo.Invoke.InvokeMethodFast(對象目標,Object []參數,簽名sig,MethodAttributes methodAttributes,RuntimeTypeHandle typeOwner),位於System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder活頁夾,Object []參數,CultureInfo文化,Boolean skipVisibilityChecks) System.Reflection.RuntimeMethodInfo.Invoke(對象obj,BindingFlags invokeAttr,活頁夾裝訂器,Object []參數,CultureInfo文化)
在System.Type.InvokeMember(字符串名稱,BindingFlags bindingFlags,活頁夾活頁夾,對象目標,Object []提供的Args,ParameterModifier []修飾符,CultureInfo文化,String []命名為Params),位於System.Type.InvokeMember(字符串名稱,BindingFlags invokeAttr,活頁夾活頁夾,對象目標,Object [] args,CultureInfo文化),位於Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()結束錯誤錯誤:2015-02-27 11:24:00.23代碼:0x00000001
源:用戶郵件描述:System.Reflection.TargetInvocationException:調用的目標引發了異常。 ---> System.Data.SqlClient.SqlException:建立與SQL Server的連接時發生與網絡相關或特定於實例的錯誤。 服務器未找到或無法訪問。 驗證實例名稱正確,並且已將SQL Server配置為允許遠程連接。 (提供者:命名管道提供程序,錯誤:40-無法打開與SQL Server的連接)位於System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)的System.Data.SqlClient.SqlInternalConnection.OnError(SqlException異常,布爾值BreakConnection) )在System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,SqlInternalConnectionTds connHandler,布爾ignoreSniOpenTimeout,Int64 timerExpire,布爾加密,布爾trustServerCert,布爾IntegratedSecurity,SqlConnection owningObject) System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover中的String newPassword,布爾ignoreSniOpenTimeout,Int64 timerExpire,SqlConnection owningObject)(System.Data.SqlClientds.SqlInternal.SqlString。 OpenLoginEnlis System.Data.SqlClient.SqlInternalConnectionTds..ctor(System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity Identity,SqlConnectionString connectionOptions,Object providerInfo,String newPassword,Sqlnewwnword,SqlConnection owningObject,Boolean redirectedUserInstance)在System.Data.SqlClient System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection,DbConnectionPool池,DbConnectionOptions選項)處的.SqlConnectionFactory.CreateConnection(DbConnectionOptions選項,對象poolGroupProviderInfo,DbConnectionPool池,DbConnection owningConnection) ...包執行失敗...步驟失敗

C#代碼-

using System;
using System.Data;
using System.Diagnostics;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
using Microsoft.Office.Interop;
using System.Runtime.InteropServices;

namespace ST_ecfa668f250a45e18c95639c9ffd64d4.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion


        public void Main()
        {   /*Passing the file path via User::File_Name Variable*/
            string FileName = Dts.Variables["User::File_Name"].Value.ToString();
            Microsoft.Office.Interop.Excel.Application xlApp = null;
            Microsoft.Office.Interop.Excel.Workbook excelBook = null;
            try
            {
                xlApp = new Microsoft.Office.Interop.Excel.Application();
                excelBook = xlApp.Workbooks.Open(FileName, Type.Missing,
                                                Type.Missing, Type.Missing, Type.Missing,
                                                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                Type.Missing, Type.Missing);
                string[] excelSheets = new string[excelBook.Worksheets.Count];
                int i = 0;
                foreach (Microsoft.Office.Interop.Excel.Worksheet wSheet in excelBook.Worksheets)
                {
                    excelSheets[i] = wSheet.Name;
                    i++;
                }
                Dts.Variables["User::WorkSheetName"].Value = excelSheets[0] + "$";
            }
            catch (Exception ex)
            {
                excelBook.Close(false, FileName, null);
                Marshal.ReleaseComObject(excelBook);
                string error = ex.Message;
            }
            finally
            {
                excelBook.Close(false, FileName, null);
                Marshal.ReleaseComObject(excelBook);
            }

            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}

嘗試創建此文件夾: C:\\Windows\\SysWOW64\\config\\systemprofile\\Desktop

請參閱此鏈接以獲取更多詳細信息

您必須在32位計算機上運行BIDS,這就是軟件包成功運行的原因。 在您的UAT服務器上,請在SQL作業步驟中將程序包配置為以32位模式運行。 為此,請修改配置SSIS程序包的SQL作業步驟-

1)轉到執行選項選項

2)選中“ 使用32位運行時”復選框

它應該可以解決您的問題。

目標是擁有一個帶有C3腳本的SQL Job SSIS程序包,該程序可以在網絡共享上動態創建excel文件。 我能夠使用代理帳戶通過ssisdb直接從SQL Server運行該程序包。 但是未能作為工作。 最后,經過長時間的無奈。 由於以下鏈接成功

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM