使用Oracle插件ODP技术向oracle数据库中批量插入数据
using System;
using Oracle.DataAccess.Client;
namespace Common.DB
{
/// <summary>
/// ODP数据库操作通用
/// </summary>
public static class ODPCommon
{
/// <summary>
/// 连接字符串
/// </summary>
private static string connString = Bosera.Common.Util.FrameUtil.ConnectionString;
/// <summary>
/// 批量插入
/// </summary>
/// <param name="recordCount">插入数量</param>
/// <param name="sql">insert语句</param>
/// <param name="paras">参数</param>
public static void BatchInsert(int recordCount, string sql, OracleParameter[] paras)
{
using (OracleConnection conn = new OracleConnection(connString))
{
using (OracleCommand command = new OracleCommand
{
Connection = conn,
/*这个参数需要指定每次批插入的记录数 */
ArrayBindCount = recordCount,
CommandText = sql
})
{
conn.Open();
//添加参数
if (paras != null)
{
command.Parameters.AddRange(paras);
}
//这个调用将把参数数组传进SQL,同时写入数据库
command.ExecuteNonQuery();
command.Parameters.Clear();
}
conn.Close();
}
}
}
}
调用:
//1、使用ODP时,防止数据库中有DATE类型的数据,不设置格式则会出现无效月份的情况,如:new OracleDate("2011-08-26 17:18:19");
OracleGlobalization og = OracleGlobalization.GetClientInfo();
og.DateFormat = "YYYY-MM-DD HH24:MI:SS"; //格式字符串不区分大小写
OracleGlobalization.SetThreadInfo(og);
//2、insert带参的SQL语句
insert into userinfo(uname,upwd) values(:uname,:upwd)
//3、为参数设置值
OracleParameter[] paras =
{
new OracleParameter { ParameterName = "uname",Value="zwq",OracleDbType = OracleDbType.Varchar2},
new OracleParameter { ParameterName = "upwd",Value="123",OracleDbType = OracleDbType.Varchar2}
};
//调用数据库操作通用方法
ODPCommon.BatchInsert(200, insertSql, paras);