更新內容:
1.添加超大文本匯入(原只支援DataReader)
2.支援從第x行資料開始匯入
3.決定是否儲存資料行號
4.列映射不區分大小寫
5.支援匯入常量列
6.相同源列匯入多目標列只分配一次記憶體,更加節約記憶體
7.增加了源列自動檢測功能
調用樣本如下:
1.匯入DataReader
View Code
/// <summary>
/// 快速匯入DataReader至Oracle
/// </summary>
/// <param name="strConnectionString">oracle連接字串如:"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx)(PORT=1521))(CONNECT_DATA=(SID=yyyy)));User Id=sys;Password=sys;"</param>
/// <param name="intTimeOut">匯入最大逾時時間</param>
/// <param name="dataReader">要匯入的DataReader</param>
/// <param name="strTableName">目標表名</param>
/// <param name="columnMapping">列映射關係</param>
/// <param name="intBatchSize">每次匯入多少條後提交</param>
/// <param name="intCommitRowCount">匯入成功行數</param>
/// <param name="spentTime">消耗時間</param>
/// <param name="strErrMsg">錯誤訊息</param>
/// <returns></returns>
public bool FastImportDataReaderToDb(string strConnectionString, int intBatchSize, int intTimeOut, IDataReader dataReader,
string strTableName,
Dictionary<string, string> columnMapping, out int intCommitRowCount, out long spentTime, out string strErrMsg)
{
bool blnFlag = false;
intCommitRowCount = 0;
spentTime = 0;
strErrMsg = string.Empty;
OracleBulkInsert oracleBulkInsert = new OracleBulkInsert(strConnectionString, strTableName, columnMapping, null, 1, null, intBatchSize, intTimeOut);
try
{
oracleBulkInsert.WriteToServer(dataReader);
blnFlag = true;
}
catch (OracleBulkInsertException ex)
{
strErrMsg = ex.ToString();
}
finally
{
intCommitRowCount = oracleBulkInsert.CopiedRowCount;
spentTime = oracleBulkInsert.TakeMilliseconds;
}
return blnFlag;
}
2.匯入文字檔
View Code
/// <summary>
/// 快速匯入文字檔至資料庫表
/// </summary>
/// <param name="strConnectionString">目的資料庫連接字串,oracle連接字串如:"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx)(PORT=1521))(CONNECT_DATA=(SID=yyyy)));User Id=sys;Password=sys;"</param>
/// <param name="intTimeOut">匯入最大逾時時間</param>
/// <param name="strTxtFile">要匯入的文字檔路徑</param>
/// <param name="strHead">列頭格式帶分隔字元</param>
/// <param name="strDelimited">資料行分隔符號</param>
/// <param name="strTableName">目標表名</param>
/// <param name="columnMapping">列映射關係,Txt第一行提供列名</param>
/// <param name="intBatchSize">每次匯入多少條後提交</param>
/// <param name="extraMapping">額外列映射</param>
/// <param name="strRowNumberColumn">儲存行號列名(null:不儲存行號)</param>
/// <param name="intCommitRowCount">匯入成功行數</param>
/// <param name="lSpentTime">消耗時間</param>
/// <param name="strErrMsg">錯誤訊息</param>
/// <param name="intStartRow">從第X行開始匯入</param>
/// <returns></returns>
public bool FastImportTextToDb(string strConnectionString, int intBatchSize, int intTimeOut, string strTxtFile, string strHead, string strDelimited, string strTableName, Dictionary<string, string> columnMapping, Dictionary<string, object> extraMapping, uint intStartRow, string strRowNumberColumn, out int intCommitRowCount, out long lSpentTime, out string strErrMsg)
{
bool blnFlag = false;
intCommitRowCount = 0;
lSpentTime = 0;
strErrMsg = string.Empty;
if (!".txt".Equals(Path.GetExtension(strTxtFile), StringComparison.CurrentCultureIgnoreCase))
{
throw new Exception("只支援匯入txt檔案...");
}
FileStream fileStream = new FileStream(strTxtFile, FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader streamReader = new StreamReader(fileStream, Encoding.Default);
OracleBulkInsert oracleBulkInsert = new OracleBulkInsert(strConnectionString, strTableName, columnMapping, extraMapping, intStartRow, strRowNumberColumn, intBatchSize, intTimeOut);
try
{
oracleBulkInsert.WriteToServer(streamReader, strHead, strDelimited);
blnFlag = true;
}
catch (OracleBulkInsertException ex)
{
strErrMsg = ex.ToString();
}
finally
{
intCommitRowCount = oracleBulkInsert.CopiedRowCount;
lSpentTime = oracleBulkInsert.TakeMilliseconds;
}
return blnFlag;
}
源碼下載:/Files/xiaobier/OracleBulkInsert.rar