oracle 导入数据到 oracle c#,如何使用c#将csv数据导入Oracle

如何使用c#将csv数据导入Oracle.其中要导入的数据的大小为3GB,行数为7512263.我设法将csv数据导入到Oracle中,但是时间大约需要1个小时.如何加快将csv数据导入oracle所需的时间.谢谢.

这是我的代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;

using System.Threading;

using System.Text.RegularExpressions;

using System.IO;

using FileHelpers;

using System.Data.OracleClient;

namespace sqlloader

{

class Program

{

static void Main(string[] args)

{

int jum;

int i;

bool isFirstLine = false;

FileHelperEngine engine = new FileHelperEngine(typeof(XL_XDR));

//Connect To Database

string constr = "Data Source=(DESCRIPTION=(ADDRESS_LIST="

+ "(ADDRESS=(PROTOCOL=TCP)(HOST= pt-9a84825594af )(PORT=1521 )))"

+ "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=o11g)));"

+ "User Id=xl;Password=rahasia;";

OracleConnection con = new OracleConnection(constr);

con.Open();

// To Read Use:

XL_XDR[] res = engine.ReadFile("DataOut.csv") as XL_XDR[];

jum = CountLinesInFile("DataOut.csv");

FileInfo f2 = new FileInfo("DataOut.csv");

long s2 = f2.Length;

int jmlRecord = jum - 1;

for (i = 0; i < jum; i++)

{

ShowPercentProgress("Processing...", i, jum);

Thread.Sleep(100);

if (isFirstLine == false)

{

isFirstLine = true;

}

else

{

string sql = "INSERT INTO XL_XDR (XDR_ID, XDR_TYPE, SESSION_START_TIME, SESSION_END_TIME, SESSION_LAST_UPDATE_TIME, " +

"SESSION_FLAG, VERSION, CONNECTION_ROW_COUNT, ERROR_CODE, METHOD, HOST_LEN, HOST, URL_LEN, URL, CONNECTION_START_TIME, " +

"CONNECTION_LAST_UPDATE_TIME, CONNECTION_FLAG, CONNECTION_ID, TOTAL_EVENT_COUNT, TUNNEL_PAIR_ID, RESPONSIVENESS_TYPE, " +

"CLIENT_PORT, PAYLOAD_TYPE, VIRTUAL_TYPE, VID_CLIENT, VID_SERVER, CLIENT_ADDR, SERVER_ADDR, CLIENT_TUNNEL_ADDR, " +

"SERVER_TUNNEL_ADDR, ERROR_CODE_2, IPID, C2S_PKTS, C2S_OCTETS, S2C_PKTS, S2C_OCTETS, NUM_SUCC_TRANS, CONNECT_TIME, " +

"TOTAL_RESP, TIMEOUTS, RETRIES, RAI, TCP_SYNS, TCP_SYN_ACKS, TCP_SYN_RESETS, TCP_SYN_FINS, EVENT_TYPE, FLAGS, TIME_STAMP, " +

"EVENT_ID, EVENT_CODE) VALUES (" +

"'" + res[i].XDR_ID + "', '" + res[i].XDR_TYPE + "', '" + res[i].SESSION_START_TIME + "', '" + res[i].SESSION_END_TIME + "', " +

"'" + res[i].SESSION_LAST_UPDATE_TIME + "', '" + res[i].SESSION_FLAG + "', '" + res[i].VERSION + "', '" + res[i].CONNECTION_ROW_COUNT + "', " +

"'" + res[i].ERROR_CODE + "', '" + res[i].METHOD + "', '" + res[i].HOST_LEN + "', '" + res[i].HOST + "', " +

"'" + res[i].URL_LEN + "', '" + res[i].URL + "', '" + res[i].CONNECTION_START_TIME + "', '" + res[i].CONNECTION_LAST_UPDATE_TIME + "', " +

"'" + res[i].CONNECTION_FLAG + "', '" + res[i].CONNECTION_ID + "', '" + res[i].TOTAL_EVENT_COUNT + "', '" + res[i].TUNNEL_PAIR_ID + "', " +

"'" + res[i].RESPONSIVENESS_TYPE + "', '" + res[i].CLIENT_PORT + "', '" + res[i].PAYLOAD_TYPE + "', '" + res[i].VIRTUAL_TYPE + "', " +

"'" + res[i].VID_CLIENT + "', '" + res[i].VID_SERVER + "', '" + res[i].CLIENT_ADDR + "', '" + res[i].SERVER_ADDR + "', " +

"'" + res[i].CLIENT_TUNNEL_ADDR + "', '" + res[i].SERVER_TUNNEL_ADDR + "', '" + res[i].ERROR_CODE_2 + "', '" + res[i].IPID + "', " +

"'" + res[i].C2S_PKTS + "', '" + res[i].C2S_OCTETS + "', '" + res[i].S2C_PKTS + "', '" + res[i].S2C_OCTETS + "', " +

"'" + res[i].NUM_SUCC_TRANS + "', '" + res[i].CONNECT_TIME + "', '" + res[i].TOTAL_RESP + "', '" + res[i].TIMEOUTS + "', " +

"'" + res[i].RETRIES + "', '" + res[i].RAI + "', '" + res[i].TCP_SYNS + "', '" + res[i].TCP_SYN_ACKS + "', " +

"'" + res[i].TCP_SYN_RESETS + "', '" + res[i].TCP_SYN_FINS + "', '" + res[i].EVENT_TYPE + "', '" + res[i].FLAGS + "', " +

"'" + res[i].TIME_STAMP + "', '" + res[i].EVENT_ID + "', '" + res[i].EVENT_CODE + "')";

OracleCommand command = new OracleCommand(sql, con);

command.ExecuteNonQuery();

}

}

Console.WriteLine("Successfully Inserted");

Console.WriteLine();

Console.WriteLine("Number of Row Data: " + jmlRecord.ToString());

Console.WriteLine();

Console.WriteLine("The size of {0} is {1} bytes.", f2.Name, f2.Length);

con.Close();

}

static void ShowPercentProgress(string message, int currElementIndex, int totalElementCount)

{

if (currElementIndex < 0 || currElementIndex >= totalElementCount)

{

throw new InvalidOperationException("currElement out of range");

}

int percent = (100 * (currElementIndex + 1)) / totalElementCount;

Console.Write("\r{0}{1}% complete", message, percent);

if (currElementIndex == totalElementCount - 1)

{

Console.WriteLine(Environment.NewLine);

}

}

static int CountLinesInFile(string f)

{

int count = 0;

using (StreamReader r = new StreamReader(f))

{

string line;

while ((line = r.ReadLine()) != null)

{

count++;

}

}

return count;

}

}

[DelimitedRecord(",")]

public class XL_XDR

{

public string XDR_ID;

public string XDR_TYPE;

public string SESSION_START_TIME;

public string SESSION_END_TIME;

public string SESSION_LAST_UPDATE_TIME;

public string SESSION_FLAG;

public string VERSION;

public string CONNECTION_ROW_COUNT;

public string ERROR_CODE;

public string METHOD;

public string HOST_LEN;

public string HOST;

public string URL_LEN;

public string URL;

public string CONNECTION_START_TIME;

public string CONNECTION_LAST_UPDATE_TIME;

public string CONNECTION_FLAG;

public string CONNECTION_ID;

public string TOTAL_EVENT_COUNT;

public string TUNNEL_PAIR_ID;

public string RESPONSIVENESS_TYPE;

public string CLIENT_PORT;

public string PAYLOAD_TYPE;

public string VIRTUAL_TYPE;

public string VID_CLIENT;

public string VID_SERVER;

public string CLIENT_ADDR;

public string SERVER_ADDR;

public string CLIENT_TUNNEL_ADDR;

public string SERVER_TUNNEL_ADDR;

public string ERROR_CODE_2;

public string IPID;

public string C2S_PKTS;

public string C2S_OCTETS;

public string S2C_PKTS;

public string S2C_OCTETS;

public string NUM_SUCC_TRANS;

public string CONNECT_TIME;

public string TOTAL_RESP;

public string TIMEOUTS;

public string RETRIES;

public string RAI;

public string TCP_SYNS;

public string TCP_SYN_ACKS;

public string TCP_SYN_RESETS;

public string TCP_SYN_FINS;

public string EVENT_TYPE;

public string FLAGS;

public string TIME_STAMP;

public string EVENT_ID;

public string EVENT_CODE;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将Excel文件中的数据导入Oracle数据库可以使用以下步骤: 1. 首先,需要将Excel文件读取到C#中,可以使用NPOI库来读取Excel文件中的数据。 2. 连接Oracle数据库,并打开连接。 3. 创建一个OracleCommand对象,该对象用于执行SQL命令。 4. 遍历Excel文件中的每一行数据,并将数据插入到Oracle数据库中,可以使用OracleCommand对象的ExecuteNonQuery方法来执行SQL语句。 以下是一个示例代码: ```csharp using System; using System.Data; using System.Data.OracleClient; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.SS.Util; // 读取Excel文件 HSSFWorkbook workbook = new HSSFWorkbook(new FileStream("Excel文件路径", FileMode.Open)); ISheet sheet = workbook.GetSheetAt(0); // 连接Oracle数据库 string connectionString = "Data Source=数据库地址;User ID=用户名;Password=密码;"; OracleConnection connection = new OracleConnection(connectionString); connection.Open(); // 创建OracleCommand对象 OracleCommand command = new OracleCommand(); command.Connection = connection; // 遍历Excel文件中的每一行数据,将数据插入到Oracle数据库中 for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); if (row == null) continue; string col1 = row.GetCell(0).ToString().Trim(); string col2 = row.GetCell(1).ToString().Trim(); string col3 = row.GetCell(2).ToString().Trim(); string sql = "insert into table_name(col1, col2, col3) values(:col1, :col2, :col3)"; command.CommandText = sql; command.Parameters.Clear(); command.Parameters.Add(new OracleParameter(":col1", col1)); command.Parameters.Add(new OracleParameter(":col2", col2)); command.Parameters.Add(new OracleParameter(":col3", col3)); command.ExecuteNonQuery(); } // 关闭连接 connection.Close(); ``` 需要注意的是,在执行SQL语句时,使用了参数化查询,可以防止SQL注入攻击。另外,需要根据Excel文件和Oracle数据库的实际情况,修改代码中的表名、列名和连接字符串等信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值