介于Oracle.DataAccess.dll使用问题,小编又找了一种新的替代方式,还请技术主管帮忙封装了一下,以后遇到大数据导入的时候,就再也不害怕了,哈哈。
忍不住了直接上代码了哈哈。
参数赋值到sql中
private void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, string cmdText, OracleParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.BindByName = true;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (OracleParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
导入的方法
#region 批量导入
public bool BatchImport(string SQLString, int rownum, params OracleParameter[] cmdParms)
{
using (OracleConnection connection = new OracleConnection(this.connectionString))
{
using (OracleCommand cmd = new OracleCommand(SQLString, connection))
{
try
{
cmd.CommandType = CommandType.Text;
cmd.ArrayBindCount=rownum;
this.PrepareCommand(cmd, connection, null, SQLString, cmdParms);
//connection.Open();
int rows=cmd.ExecuteNonQuery();
connection.Close();
cmd.Dispose();
connection.Dispose();
return rows >= 0 ? true : false;
}
catch (Oracle.ManagedDataAccess.Client.OracleException E)
{
connection.Close();
//throw new Exception(E.Message);
return false;
}
}
}
}
#endregion
组建参数
///
/// 组建参数用
///
///
///
///
///
///
///
public OracleParameter BuildOracleParameter(string name, OracleDbType oracleType, ParameterDirection direction, int size, object value)
{
OracleParameter paramerer = new OracleParameter(name, value);
paramerer.ParameterName = name;
paramerer.OracleDbType = oracleType;
paramerer.Direction = direction;
if (size > 0)
{
paramerer.Size = size;
}
return paramerer;
}
调用过程
#region 直接全表匹配插入+BatchGoodsDrugCode+贾文静+2017年3月9日12:44:03
public bool BatchGoodsDrugCode(List goodDrugCodeLists)
{
string sql = "INSERT INTO goods_drugcode VALUES (:org_id,:goods_id,:batch_no,:valid_date,:product_date,:code,:parent_code,:Isleaf,sysdate,:create_man,:Isdelete,sysdate,:modify_man,:owner_id)";
int recc = goodDrugCodeLists.Count;
string[] org_id = new string[recc];
string[] goods_id = new string[recc];
string[] batch_no = new string[recc];
string[] valid_date = new string[recc];
string[] product_date = new string[recc];
string[] code = new string[recc];
string[] parent_code = new string[recc];
string[] Isleaf = new string[recc];
string[] create_man = new string[recc];
string[] Isdelete = new string[recc];
string[] modify_man = new string[recc];
string[] owner_id = new string[recc];
for (int i = 0; i < recc; i++)
{
org_id[i] = goodDrugCodeLists[i].ORG_ID.ToString();
goods_id[i] = goodDrugCodeLists[i].GOODS_ID.ToString();
batch_no[i] = goodDrugCodeLists[i].BATCH_NO.ToString();
valid_date[i] = goodDrugCodeLists[i].VALID_DATE.ToString();
product_date[i] = goodDrugCodeLists[i].PRODUCT_DATE.ToString();
code[i] = goodDrugCodeLists[i].CODE.ToString();
parent_code[i] = goodDrugCodeLists[i].PARENT_CODE.ToString();
Isleaf[i] = goodDrugCodeLists[i].ISLEAF.ToString();
create_man[i] = goodDrugCodeLists[i].CREATE_MAN.ToString();
Isdelete[i] = goodDrugCodeLists[i].ISDELETED.ToString();
modify_man[i] = goodDrugCodeLists[i].MODIFY_MAN.ToString();
owner_id[i] = goodDrugCodeLists[i].OWNER_ID.ToString();
}
OracleParameter[] param = {
ora.BuildOracleParameter(":org_id",OracleDbType.Varchar2,ParameterDirection.Input,0,org_id),
ora.BuildOracleParameter(":goods_id",OracleDbType.Varchar2,ParameterDirection.Input,0,goods_id),
ora.BuildOracleParameter(":batch_no",OracleDbType.Varchar2,ParameterDirection.Input,0,batch_no),
ora.BuildOracleParameter(":valid_date",OracleDbType.Varchar2,ParameterDirection.Input,0,valid_date),
ora.BuildOracleParameter(":product_date",OracleDbType.Varchar2,ParameterDirection.Input,0,product_date),
ora.BuildOracleParameter(":code",OracleDbType.Varchar2,ParameterDirection.Input,0,code),
ora.BuildOracleParameter(":parent_code",OracleDbType.Varchar2,ParameterDirection.Input,0,parent_code),
ora.BuildOracleParameter(":Isleaf",OracleDbType.Varchar2,ParameterDirection.Input,0,Isleaf),
ora.BuildOracleParameter(":create_man",OracleDbType.Varchar2,ParameterDirection.Input,0,create_man),
ora.BuildOracleParameter(":Isdelete",OracleDbType.Varchar2,ParameterDirection.Input,0,Isdelete),
//ora.BuildOracleParameter(":modify_date",OracleDbType.Varchar2,ParameterDirection.Input,0,modify_date),
ora.BuildOracleParameter(":modify_man",OracleDbType.Varchar2,ParameterDirection.Input,0,modify_man),
ora.BuildOracleParameter(":owner_id",OracleDbType.Varchar2,ParameterDirection.Input,0,owner_id)
};
return ora.BatchImport(sql, recc, param);
}
#endregion
终于完美实现大量数据的直接插入,2W数据,不超过五秒,而且引用的是Oracle.ManagedDataAccess.Client,实施起来也比较方便。效率也非常可观。
正在小编嘚瑟的时候,组长说了一句话,心都塞了。如果在数据表上添加字段有影响吗,需要改程序吗?
这个小编先说一句啊,这个方法是对表中全部字段匹配,在sql语句insert需要写所有的字段,否则会提示错误。所以这样的弊端就是每当数据表结构改变,程序也需要改变,真是不够灵活的!!!
所以当组长说到这个问题时候,我才开始考虑,修改表结构的问题。无奈,这次又不是很完美。好在数据表结构不经常改变,所以还可以正常使用。
批量导入到此结束,三种方式,根据情况自由选择吧。