批量插入CSV到oracle表 [英] Bulk insert CSV to oracle table
本文介绍了批量插入CSV到oracle表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过File.ReadAllLines()读取csv文件内容并将其存储到DataTable中。
存储后,我调用OracleBulkCopy将所有记录插入到我的表中。
我使用了.NET 4和Oracle 11g版本的框架。
这给了我错误的说法
I am trying to insert csv file content by reading it through File.ReadAllLines() and storing it into DataTable.
After storing this, I am calling OracleBulkCopy to insert all records into my Table.
I have used framework 4 for .NET and Oracle 11g version.
This gives me error saying "
Could not load file or assembly 'Oracle.DataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342'
帮我看看我到底缺少什么。
我附上代码供参考。
我尝试过:
"
Help me to find out what exactly I am missing here.
I am attaching code for reference.
What I have tried:
using System;
using System.Data;
using System.IO;
using System.Configuration;
using Oracle.DataAccess.Client;
namespace ImportCSV
{
class Program
{
static void Main()
{
try
{
// your code here
string CSVFilePathName = @"C:\TempShare\TestCSVFile.csv";
string[] Lines = File.ReadAllLines(CSVFilePathName);
string[] Fields;
Fields = Lines[0].Split(new char[] { ',' });
int Cols = Fields.GetLength(0);
DataTable dt = new DataTable();
DataRow Row;
//1st row must be column names; force lower case to ensure matching later on.
for (int i = 0; i < Cols; i++)
{
dt.Columns.Add(Fields[i].ToLower(), typeof(string));
}
for (int i = 1; i < Lines.GetLength(0); i++)
{
Fields = Lines[i].Split(new char[] { ',' });
Row = dt.NewRow();
for (int f = 0; f < Cols; f++)
Row[f] = Fields[f];
dt.Rows.Add(Row);
}
SaveUsingOracleBulkCopy("tFinal", dt);
}
catch (Exception ex)
{
Console.Write("Error is " + ex.ToString());
throw;
}
}
static void SaveUsingOracleBulkCopy(string destTableName, DataTable dt)
{
try
{
string oradb = "Data Source=(DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=000.00.00.00)(PORT=0000)) (CONNECT_DATA =(SID=XX))); User Id=XXX;Password=xxxxxxx;";
using (var connection = new OracleConnection(oradb))
{
connection.Open();
Console.WriteLine("Connected to Oracle Database {0}", connection.ServerVersion);
Console.WriteLine("Press RETURN to exit.");
Console.ReadLine();
using (var bulkCopy = new OracleBulkCopy(connection, OracleBulkCopyOptions.UseInternalTransaction))
{
bulkCopy.DestinationTableName = destTableName;
bulkCopy.BulkCopyTimeout = 600;
bulkCopy.WriteToServer(dt);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
推荐答案
选择可能的解决方案已在此处发布... 无法加载文件或汇编'Oracle.DataAccess'或其依赖项之一。尝试加载格式不正确的程序。 [ ^ ]
A selection of possible solutions have been posted here ... Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. An attempt was made to load a program with an incorrect format.[^]
c# - Oracle.DataAccess.Client依赖项 - 堆栈溢出 [ ^ ]
这是我遇到的最佳解决方案。
c# - Oracle.DataAccess.Client Dependencies - Stack Overflow[^]
This is the best solution I have come across.
>
这篇关于批量插入CSV到oracle表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文