您的位置:首页 > 数据库 > Oracle

OracleBulkCopy 修正帮

2015-06-08 14:48 417 查看
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Contract.Domain;
using DevExpress.Data;
using Framework;
using Holworth.Utility;
using HraWeb.Common;
using Holworth.RiskInterface;
using System.Net;
using System.IO;
using System.Text;
using Contract.IService;
using HraModel;
using Oracle.DataAccess.Client;
using OracleConnection = System.Data.OracleClient.OracleConnection;

namespace HraWeb
{
public partial class Test : BasePage
{
DataTable dtMulu = new DataTable();
//获取表的英文名
public string GetTableToEn(string tableName)
{
string ReturntableName = "";
for (int i = 0; i < dtMulu.Rows.Count; i++)
{
if (tableName == dtMulu.Rows[i][1].ToString().Trim())
{
ReturntableName = dtMulu.Rows[i][2].ToString();
break;
}
}
return ReturntableName;
}

public void GetExcelDirectory()
{

// System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
string fileName;
//if (fd.ShowDialog() == DialogResult.OK)
{
//fileName = fd.FileName;
fileName = @"F:\project1\附件1.【数据集市】交易数据汇总-1(修改) - 副本";
dtMulu = ExcelUtil.GetExcelSheetContent(fileName, "目录" + "$");
string[] dtAllTable = ExcelUtil.GetExcelTableName(fileName);
HraWeb.ReportEg.To38Validate.Validate.FX_FORWARD_V fc = new HraWeb.ReportEg.To38Validate.Validate.FX_FORWARD_V();
foreach (string item in dtAllTable)
{
if (!(item.LastIndexOf("属性表") > 0))
{

#region 反射获取验证页面所在的程序集

Assembly asm = Assembly.GetAssembly(fc.GetType());

#endregion
DataTable dtAllList = ExcelUtil.GetExcelSheetContent(fileName, item + "$");
if (dtAllTable.Length > 0 && dtAllTable != null)
{
string tableEn = GetTableToEn(item);

Type t = asm.GetType(fc.GetType().Namespace+ "."+tableEn);

object o = Activator.CreateInstance(t);
MethodInfo getSqlMethodInfo = t.GetMethod("TestImport");

if (!string.IsNullOrEmpty(tableEn))
{
getSqlMethodInfo.Invoke(o, new object[] { fileName,tableEn });

}

}
}
}
}
}

public void TestImport()
{
string[] tableName= Holworth.Utility.ExcelUtil.GetExcelTableName(@"c:\1.xls");
System.Data.DataTable table = Holworth.Utility.ExcelUtil.GetExcelSheetContent(@"c:\1.xls",tableName[0]);
System.Text.StringBuilder etrorSb=new StringBuilder();

int i=0;
System.Collections.ArrayList list = new System.Collections.ArrayList();
foreach (System.Data.DataRow row in table.Rows)
{
Contract.Domain.BasAgreement a = new Contract.Domain.BasAgreement();
string AgreementCode=row[0].ToString();
if(string.IsNullOrEmpty(row[0].ToString()))//记录结束
{

break;
}
if (AgreementCode == "1")//验证自己调用
{
a.AgreementCode = AgreementCode;
}
else
{
etrorSb.Append(string.Format("第{0}行{1}列值为:{2}转换失败",i,1,AgreementCode));
continue;

}
//验证成功后
list.Add(a);
a.State.MarkNew();
i++;
}
if (list.Count > 0)
{
Dao.SaveOrUpdateAll(list);
}

if (!string.IsNullOrEmpty(etrorSb.ToString()))
{
Utility.JSUtil.log(etrorSb.ToString());
}
}
protected void Page_Load(object sender, EventArgs e)
{
// GetExcelDirectory();
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request["_method"]))
{
try
{
switch (Request["_method"])
{
case "Test":

int riskBookId = 10002;
int attributeLookupId = 100;
int timeHorizon = 1;
int periodLength = 1;
int dateTimeSpan = 1;
int windowSize = 30;
int simulationNumber = 0;
int tickNumber = 30;
int resultNumber = 5;
int riskModelTypeID = 1302;
double decayFactor = 1.0;
int horizonNumber = 1;
double timeStepLength = 1;
DateTime computeDate=DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));

ShockInputData shockInputData = new ShockInputData();

shockInputData.ShockFlag = Constants.SHOCK_NO;
shockInputData.ShockTypeID = Constants.DEFAULT_SHOCK_TYPE_ID;
shockInputData.ShockValueTypeID = Constants.DEFAULT_SHOCK_VALUE_TYPE_ID;
shockInputData.ShockModeID = Constants.DEFAULT_SHOCK_MODE_ID;
shockInputData.ShockValue = Constants.DEFAULT_SHOCK_VALUE;

CashflowGenerationService.GenerateTransactionCashflowByDatesWithShock(computeDate, computeDate, computeDate, computeDate, shockInputData, "DEAL_ENTRY");

//riskReturnComputationService.ProcessPriceReturn(CurrentUser.UserId, CurrentUser.UserName, computeDate, periodLength, windowSize, dateTimeSpan);
//riskVolatilityComputationService.ProcessPriceVolatility(CurrentUser.UserId, CurrentUser.UserName, computeDate, periodLength, windowSize, decayFactor);
//riskCorrelationComputationService.ProcessPriceCorrelation(CurrentUser.UserId, CurrentUser.UserName, computeDate, periodLength, windowSize, decayFactor);

//forwardPriceSimulationService.SimulateForwardPriceForMultiPeriod(computeDate, simulationNumber, horizonNumber, timeStepLength, windowSize, 602, 100);
wfService.SaveStartFlow(Utility.Util.NewGuid(), "Tesy","测试");
valueAtRiskCalculationService.SaveComputeValueAtRiskByUserId(computeDate, riskBookId, attributeLookupId, timeHorizon, windowSize, simulationNumber, tickNumber, resultNumber, riskModelTypeID, CurrentUser.UserId, CurrentUser.UserName);

//riskCorrelationDecompositionService.DecomposeRiskFactorCorrelationData(DateTime.Parse("2013/11/29"), 30);
// Holworth.Utility.Utility.DownLoadExchageRade("2014_10", @"c:\1.xls");
var obj = new
{
Message = "测试成功!"
};
Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
break;
}
}
catch (Exception ex)
{
var obj = new
{
ErrorCode = 9999,
Message = ex.Message
};
Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
Utility.JSUtil.log(ex);

}
finally
{
Response.End();
}
}
}
}
IRiskVolatilityComputationService _riskVolatilityComputationService = null;
IRiskVolatilityComputationService riskVolatilityComputationService
{
get
{
if (_riskVolatilityComputationService == null)
{
_riskVolatilityComputationService = ctx["RiskVolatilityComputationService"] as IRiskVolatilityComputationService;
}
return _riskVolatilityComputationService;
}
}

IWorkFlowEngineService _wfService = null;
IWorkFlowEngineService wfService
{
get
{
if (_wfService == null)
{
_wfService = ctx["WorkFlowEngineService"] as IWorkFlowEngineService;
}
return _wfService;
}
}

IValueAtRiskCalculationService _valueAtRiskCalculationService = null;
IValueAtRiskCalculationService valueAtRiskCalculationService
{
get
{
if (_valueAtRiskCalculationService == null)
{
_valueAtRiskCalculationService = ctx["ValueAtRiskCalculationService"] as IValueAtRiskCalculationService;
}
return _valueAtRiskCalculationService;
}
}

IRiskReturnComputationService _riskReturnComputationService = null;
IRiskReturnComputationService riskReturnComputationService
{
get
{
if (_riskReturnComputationService == null)
{
_riskReturnComputationService = ctx["RiskReturnComputationService"] as IRiskReturnComputationService;
}
return _riskReturnComputationService;
}
}

IRiskCorrelationComputationService _riskCorrelationComputationService = null;
IRiskCorrelationComputationService riskCorrelationComputationService
{
get
{
if (_riskCorrelationComputationService == null)
{
_riskCorrelationComputationService = ctx["RiskCorrelationComputationService"] as IRiskCorrelationComputationService;
}
return _riskCorrelationComputationService;
}
}

IRiskCorrelationDecompositionService _riskCorrelationDecompositionService = null;
IRiskCorrelationDecompositionService riskCorrelationDecompositionService
{
get
{
if (_riskCorrelationDecompositionService == null)
{
_riskCorrelationDecompositionService = ctx["RiskCorrelationDecompositionService"] as IRiskCorrelationDecompositionService;
}
return _riskCorrelationDecompositionService;
}
}

IForwardPriceSimulationService _forwardPriceSimulationService = null;
IForwardPriceSimulationService forwardPriceSimulationService
{
get
{
if (_forwardPriceSimulationService == null)
{
_forwardPriceSimulationService = ctx["ForwardPriceSimulationService"] as IForwardPriceSimulationService;
}
return _forwardPriceSimulationService;
}
}

ICashflowGenerationService _CashflowGenerationService;
ICashflowGenerationService CashflowGenerationService
{
get
{
if (_CashflowGenerationService == null)
{
_CashflowGenerationService = ctx["CashflowGenerationService"] as ICashflowGenerationService;
}
return _CashflowGenerationService;
}
}

protected void testLongData(object sender, EventArgs e)
{
QueryInfo info = new QueryInfo();
info.QueryObject = "select * from ccdd where risk_book_id=4513";
DataTable dt = Dao.ExcuteDataSet(info).Tables[0];
DataRow row = dt.Rows[0];
for (int i = 0; i < 10000 * 100; i++)
{
DataRow NewRow = dt.NewRow();
NewRow.ItemArray = row.ItemArray;
dt.Rows.Add(NewRow);

}
GC.Collect();
// dt.Columns.Remove("risk_book_id");
BulkToDB(dt, "ccdd");
}
public void BulkToDB(DataTable dt, string targetTable)
{

QueryInfo info = new QueryInfo();
DataTable table = null;
Int64 increId = 0;
lock (new object())
{
#region 获取序列的当前值

QueryInfo searchInfo = new QueryInfo();
searchInfo.CustomSQL = "select CCDD_SEQ.NEXTVAL from dual";
table = Dao.ExcuteDataSet(searchInfo).Tables[0];
increId = Convert.ToInt64(table.Rows[0][0].ToString());

#endregion

info.NamedQuery = "PRO_SEQUENCE";
info.Parameters.Add("v_simulation_number", dt.Rows.Count);
Dao.ExcuteDataSet(info);
foreach (DataRow t in dt.Rows)
{
t["risk_book_id"] = increId++;

}

}
string connOrcleString =
"Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.2.252)(PORT = 1521))(CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME =hra)));User Id=nethra;Password=nethra;";
//= "Data Source=192.168.2.252;Database=hra;Uid=nethra;Pwd=nethra;";
OracleConnection conn = new OracleConnection(connOrcleString);
// OracleBulkCopy bulkCopy = new OracleBulkCopy(connOrcleString, OracleBulkCopyOptions.UseInternalTransaction); //用其它源的数据有效批量加载Oracle表中
OracleBulkCopy bulkCopy = new OracleBulkCopy(connOrcleString,
OracleBulkCopyOptions.UseInternalTransaction);
bulkCopy.BulkCopyTimeout = 260 * 1000;
bulkCopy.DestinationTableName = targetTable; //服务器上目标表的名称
bulkCopy.BatchSize = 50000; //每一批次中的行数
try
{
conn.Open();
if (dt != null && dt.Rows.Count != 0)

bulkCopy.WriteToServer(dt); //将提供的数据源中的所有行复制到目标表中
QueryInfo searchInfo = new QueryInfo();
searchInfo.CustomSQL = "select CCDD_SEQ.NEXTVAL from dual";
table = Dao.ExcuteDataSet(searchInfo).Tables[0];
}
catch (Exception ex)
{
throw ex;
}
finally
{
info = new QueryInfo();
info.NamedQuery = "PRO_SEQUENCE";
info.Parameters.Add("v_simulation_number", -1);
Dao.ExcuteDataSet(info);
conn.Close();
if (bulkCopy != null)
bulkCopy.Close();

}
throw new Exception("测试大数据成功");

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: