我的目标是将数据从 Oracle 加载到 Excel。我正在使用以下代码
#region Help: Introduction to the script task
/* The Script Task allows you to perform virtually any operation that can be accomplished in
* a .Net application within the context of an Integration Services control flow.
*
* Expand the other regions which have "Help" prefixes for examples of specific ways to use
* Integration Services features within this script task. */
#endregion
#region Namespaces
using System;
using System.Data;
using System.IO;
using System.Data.OleDb;
using Oracle.DataAccess.Client;
#endregion
namespace ST_61034037a64840399d3ad5467ff6efa2
{
/// <summary>
/// ScriptMain is the entry point class of the script. Do not change the name, attributes,
/// or parent of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
// I have added below code.
string datetime = DateTime.Now.ToString("yyyyMMdd");
try
{
//Declare Variables
string ExcelFileName = Dts.Variables["User::ExcelFileName"].Value.ToString();
string FolderPath = Dts.Variables["User::FolderPath"].Value.ToString();
string TableName = Dts.Variables["User::TableName"].Value.ToString();
string SheetName = Dts.Variables["User::SheetName"].Value.ToString();
ExcelFileName = ExcelFileName + "_" + datetime;
OleDbConnection Excel_OLE_Con = new OleDbConnection();
OleDbCommand Excel_OLE_Cmd = new OleDbCommand();
//Construct ConnectionString for Excel
string connstring = "Provider=Microsoft.ACE.OLEDB.12.0" + "Data Source=" + FolderPath + ExcelFileName
+ ";" + "Extended Properties = 'Excel 8.0;HDR=YES;";
//< add key = "Excel07ConnectionString" value = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'" />
//drop Excel file if exists
File.Delete(FolderPath + "\\" + ExcelFileName + ".xlsx");
//USE ADO.NET Connection from SSIS Package to get data from table
//SqlConnection myADONETConnection = new SqlConnection();
OracleConnection oracleConnection = new OracleConnection();
oracleConnection = (OracleConnection)(Dts.Connections["DBConn"].AcquireConnection(Dts.Transaction) as OracleConnection);
//Load Data into DataTable from SQL ServerTable
// Assumes that connection is a valid SqlConnection object.
string queryString =
"SELECT COUNTRY, T.NATIONALITY,NATION_TREP_CODE.REP_CODE_DESC ,T.NATIONAL_ID1,NATIONAL1_TREP_CODE.REP_CODE_DESC AS NATIONAL_ID_CARDTYPE_DESC FROM TUER_ADDRESS_V TC INNER JOIN TPER_DET T ON T.PERSON_ID = TC.PERSON_ID LEFT OUTER JOIN TREP_CODE NATION_TREP_CODE ON T.NATIONALITY = NATION_TREP_CODE.REP_CODE AND NATION_TREP_CODE.REP_CODE_TYPE_ID = 'NATION' LEFT OUTER JOIN TREP_CODE NATIONAL1_TREP_CODE ON T.NATIONAL_ID1 = NATIONAL1_TREP_CODE.REP_CODE AND NATIONAL1_TREP_CODE.REP_CODE_TYPE_ID = 'NATIONAL' WHERE T.STAFF_ID IS NOT NULL fetch next 500 rows only;";
OracleDataAdapter adapter = new OracleDataAdapter(queryString, oracleConnection);
DataSet ds = new DataSet();
adapter.Fill(ds);
//Get Header Columns
string TableColumns = "";
// Get the Column List from Data Table so can create Excel Sheet with Header
foreach (DataTable table in ds.Tables)
{
foreach (DataColumn column in table.Columns)
{
TableColumns += column + "],[";
}
}
// Replace most right comma from Columnlist
TableColumns = ("[" + TableColumns.Replace(",", " Text,").TrimEnd(','));
TableColumns = TableColumns.Remove(TableColumns.Length - 2);
//MessageBox.Show(TableColumns);
//Use OLE DB Connection and Create Excel Sheet
Excel_OLE_Con.ConnectionString = connstring;
Excel_OLE_Con.Open();
Excel_OLE_Cmd.Connection = Excel_OLE_Con;
Excel_OLE_Cmd.CommandText = "Create table " + SheetName + " (" + TableColumns + ")";
Excel_OLE_Cmd.ExecuteNonQuery();
//Write Data to Excel Sheet from DataTable dynamically
foreach (DataTable table in ds.Tables)
{
String sqlCommandInsert = "";
String sqlCommandValue = "";
foreach (DataColumn dataColumn in table.Columns)
{
sqlCommandValue += dataColumn + "],[";
}
sqlCommandValue = "[" + sqlCommandValue.TrimEnd(',');
sqlCommandValue = sqlCommandValue.Remove(sqlCommandValue.Length - 2);
sqlCommandInsert = "INSERT into " + SheetName + "(" + sqlCommandValue.TrimEnd(',') + ") VALUES(";
int columnCount = table.Columns.Count;
foreach (DataRow row in table.Rows)
{
string columnvalues = "";
for (int i = 0; i < columnCount; i++)
{
int index = table.Rows.IndexOf(row);
columnvalues += "'" + table.Rows[index].ItemArray[i] + "',";
}
columnvalues = columnvalues.TrimEnd(',');
var command = sqlCommandInsert + columnvalues + ")";
Excel_OLE_Cmd.CommandText = command;
Excel_OLE_Cmd.ExecuteNonQuery();
}
}
Excel_OLE_Con.Close();
//Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(Dts.Variables["User::FolderPath"].Value.ToString() + "\\" +
Dts.Variables["User::ExcelFileName"].Value.ToString() + datetime + ".log"))
{
sw.WriteLine(exception.ToString());
}
}
}
}
}
使用.dll
但得到一个错误
进度选项卡显示错误为“错误:无法加载文件或程序集‘Oracle.DataAccess,版本=4.122.1.0,文化=中性,PublicKeyToken=89b483f429c47342’或其依赖项之一。系统找不到文件指定。”。
我使用的是 VS2017 并且没有在本地安装任何 Oracle DB。使用 Oracle 连接管理器从服务器获取记录。
提前致谢。