簡體   English   中英

如何將多個存儲過程傳遞給 SSIS 中的腳本任務 C# 代碼並生成輸出文件?

[英]How can I pass multiple stored procedures to my script task C# code in SSIS and generate output files?

如何將多個存儲過程傳遞給 SSIS 中的 Scripttask C# 代碼並生成輸出文件?

我能夠成功地從 SQL Server 數據庫運行一個存儲過程並輸出一個文件。

public void Main()
{
    string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");

    try
    {
            string FileNamePart = Dts.Variables["User::FlatFileNamePart"].Value.ToString();
            string DestinationFolder = Dts.Variables["User::DestinationFolder"].Value.ToString();
            string StoredProcedureFig1_1 = Dts.Variables["User::StoredProcedureFig1_1"].Value.ToString();
            string StoredProcedureFig1_2 = Dts.Variables["User::StoredProcedureFig1_2"].Value.ToString();
            string FileDelimiter = Dts.Variables["User::FileDelimiter"].Value.ToString();
            string FileExtension = Dts.Variables["User::FileExtension"].Value.ToString();

            // USE ADO.NET Connection from SSIS Package to get data from table
            SqlConnection myADONETConnection = new SqlConnection();
            myADONETConnection = (SqlConnection)(Dts.Connections["localhost.onramps_tacc"].AcquireConnection(Dts.Transaction) as SqlConnection);

            // Execute stored procedure and save results in data table
            string query1 = "EXEC " + StoredProcedureFig1_1;

            // how to run below query too? Stackoverflow question                 
            string query2 = "EXEC " + StoredProcedureFig1_2;

            SqlCommand cmd = new SqlCommand(query1, myADONETConnection);
            DataTable d_table = new DataTable();

            d_table.Load(cmd.ExecuteReader());

            myADONETConnection.Close();

            string FileFullPath = DestinationFolder + "\\" + FileNamePart + "_" + datetime + FileExtension;

            StreamWriter sw = null;
            sw = new StreamWriter(FileFullPath, false);

            // Write the Header Row to File
            String var = d_table + "i";
            int ColumnCount = var.Columns.Count;

            for (int ic = 0; ic < ColumnCount; ic++)
            {
                    sw.Write(d_table.Columns[ic]);

                    if (ic < ColumnCount - 1)
                    {
                        sw.Write(FileDelimiter);
                    }
            }

            sw.Write(sw.NewLine);

            // Write all rows to the file
            foreach (DataRow dr in d_table.Rows)
            {
                    for (int ir = 0; ir < ColumnCount; ir++)
                    {
                        if (!Convert.IsDBNull(dr[ir]))
                        {
                            sw.Write(dr[ir].ToString());
                        }

                        if (ir < ColumnCount - 1)
                        {
                            sw.Write(FileDelimiter);
                        }
                    }

                    sw.Write(sw.NewLine);
            }

            sw.Close();

            Dts.TaskResult = (int)ScriptResults.Success;
    }
    catch (Exception exception)
    {
        // Create Log File for Errors
        using (StreamWriter sw = File.CreateText(Dts.Variables["User::LogFolder"].Value.ToString() + "\\" +
                "ErrorLog_" + datetime + ".log"))
        {
            sw.WriteLine(exception.ToString());
            Dts.TaskResult = (int)ScriptResults.Failure;
        }
    } 

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

實際的:

一個存儲過程作為輸入並輸出一個 O/P 文件或錯誤文件中的列和行。

預期的:

接受多個存儲過程並相應生成OutputFiles和錯誤文件

解決方案:

public void Main()
        {
            // TODO: Add your code here

            string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
            try
            {

                //Declare Variables
                string FileNamePart = Dts.Variables["User::FlatFileNamePart"].Value.ToString();
                string DestinationFolder = Dts.Variables["User::DestinationFolder"].Value.ToString();
                string StoredProcedureFig1_1 = Dts.Variables["User::StoredProcedureFig1_1"].Value.ToString();
                string StoredProcedureFig1_2 = Dts.Variables["User::StoredProcedureFig1_2"].Value.ToString();
                string FileDelimiter = Dts.Variables["User::FileDelimiter"].Value.ToString();
                string FileExtension = Dts.Variables["User::FileExtension"].Value.ToString();



                //USE ADO.NET Connection from SSIS Package to get data from table
                SqlConnection myADONETConnection = new SqlConnection();
                myADONETConnection = (SqlConnection)(Dts.Connections["localhost.onramps_tacc"].AcquireConnection(Dts.Transaction)
                    as SqlConnection);



                //Execute Stored Procedure and save results in data table
                string query1 = "EXEC " + StoredProcedureFig1_1;
                string query2 = "EXEC " + StoredProcedureFig1_2;
                SqlCommand cmd1 = new SqlCommand(query1, myADONETConnection);
                SqlCommand cmd2 = new SqlCommand(query2, myADONETConnection);
                DataSet dset = new DataSet();
                DataTable d_table1 = dset.Tables.Add("Fig1_1");
                DataTable d_table2 = dset.Tables.Add("Fig1_2");
                d_table1.Load(cmd1.ExecuteReader());
                d_table2.Load(cmd2.ExecuteReader());
                myADONETConnection.Close();


                foreach (DataTable table in dset.Tables)
                {
                    string FileFullPath = DestinationFolder + "\\" + FileNamePart + "_" + table + datetime + FileExtension;

                    StreamWriter sw = null;
                    sw = new StreamWriter(FileFullPath, false);

                    // Write the Header Row to File

                    int ColumnCount = table.Columns.Count;
                    for (int ic = 0; ic < ColumnCount; ic++)
                    {
                        sw.Write(table.Columns[ic]);
                        if (ic < ColumnCount - 1)
                        {
                            sw.Write(FileDelimiter);

                        }
                    }
                    sw.Write(sw.NewLine);
                    // Write All Rows to the File
                    foreach (DataRow dr in table.Rows)
                    {
                        for (int ir = 0; ir < ColumnCount; ir++)
                        {
                            if (!Convert.IsDBNull(dr[ir]))
                            {
                                sw.Write(dr[ir].ToString());

                            }
                            if (ir < ColumnCount - 1)
                            {
                                sw.Write(FileDelimiter);

                            }
                        }
                        sw.Write(sw.NewLine);



                    }
                    sw.Close();

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

暫無
暫無

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

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