.net批量向Oracle数据库中插入数据

使用Oracle插件ODP技术向oracle数据库中批量插入数据

using System;
using Oracle.DataAccess.Client;

namespace Common.DB
{
    ///


    /// ODP数据库操作通用
    ///

    public static class ODPCommon
    {
        ///
        /// 连接字符串
        ///

        private static string connString = Bosera.Common.Util.FrameUtil.ConnectionString;

        ///


        /// 批量插入
        ///

        /// 插入数量
        /// insert语句
        /// 参数
        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);

你可能感兴趣的:(asp.net)