C# Oracle大数据批量插入到数据库的方法
【代码】C# Oracle大数据批量插入到数据库的方法
·
1、首先要引用NuGet包:2、新建一个表测试:
-- Create table
create table TEST
(
id VARCHAR2(32) not null,
name VARCHAR2(255)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table TEST
add primary key (ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
string connectStr = "User ID=hh;Password=hh;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))";
CExcuteNonQuery(connectStr);
}
public static bool CExcuteNonQuery(string connectStr)
{
List<string> strList = new List<string>();
List<string> strList2 = new List<string>();
List<DateTime> dateList = new List<DateTime>();
for (int i = 0; i < 200000; i++)
{
strList.Add(Guid.NewGuid().ToString("N"));
strList2.Add("1");
dateList.Add(DateTime.Now);
}
Oracle.ManagedDataAccess.Client.OracleConnection conn = new Oracle.ManagedDataAccess.Client.OracleConnection(connectStr);
Oracle.ManagedDataAccess.Client.OracleCommand command = new Oracle.ManagedDataAccess.Client.OracleCommand();
command.Connection = conn;
command.ArrayBindCount = 200000;
command.CommandText = @"insert into TEST(ID,NAME) values(:ID,:NAME) ";
conn.Open();
OracleParameter fid = new OracleParameter("ID", OracleDbType.Char);
fid.Direction = ParameterDirection.Input;
fid.Value = strList.ToArray();
command.Parameters.Add(fid);
OracleParameter fhspid = new OracleParameter("NAME", OracleDbType.Char);
fhspid.Direction = ParameterDirection.Input;
fhspid.Value = strList2.ToArray();
command.Parameters.Add(fhspid);
//这个调用将把参数数组传进SQL,同时写入数据库
command.ExecuteNonQuery();
return true;
}
}
}
更多推荐
所有评论(0)