使用 ODP.NET 进行批量插入






1.53/5 (5投票s)
使用 ODP.NET 进行批量插入
引言
本文档将从前端解释批量插入的概念。
背景
前提条件是 ODP.NET,可以从 此链接 下载。
Using the Code
首先,您需要将 OracleAccess 添加到您的项目引用中。

从 .NET 组件中选择 Oracle.DataAccess
。

导入
在您的类文件中导入 Oracle.DataAccess
。
// import oracle Dataacces in your class file
using Oracle.DataAccess.Client;
导入 Oracle.DataAccess.Client
后,创建连接字符串。
连接详情
// create the connection string
// Give your Database details in the connection string
OracleConnection objConn =
new OracleConnection("data source=***;user id=***;password=***");
批量插入
要从前端本身进行批量插入,您需要创建一个数组对象并将值添加到其中。该数组将作为参数传递给过程,该过程将包含插入查询。
//creating an array
string strEmpName = new string[0];
string strEmpAge = new string[0];
首先,数组大小初始化为长度 0
。在将值添加到数组之前,应调整数组的大小。
// Resizing the array
public void Resize_Array(int arrCount)
{
Array.Resize(ref strEmpName , arrCount + 1);
Array.Resize(ref strEmpAge, arrCount + 1);
}
将值添加到数组
// Add values into array
Resize_Array(1);
strEmpName[0] ="sam";
strEmpAge[0]="25";
Resize_Array(2);
strEmpName[1] ="john";
strEmpAge[2]="25";
后端过程
在 Oracle 中创建过程
// Create a procedure in your back end
create or replace PROCEDURE Sample_Bulk_Insert
(
INS_EMP_NAME IN VARCHAR2,
INS_EMP_AGE IN NUMBER,
)
is
begin
INSERT INTO EMP(NAME,AGE)
VALUES (INS_EMP_NAME, INS_EMP_AGE);
COMMIT;
end;
创建过程后,从您的代码中调用该过程,并将数组作为参数传递给该过程。
调用过程
// create an Oracle command object
OracleCommand objCom = new OracleCommand();
// set the command type as stored procedure
objCom.CommandType = CommandType.StoredProcedure;
objCom.CommandText = "Sample_Bulk_Insert";
// add the parameter you need to pass to the procedure
OracleParameter param1 =objCom.Parameters.Add("INS_EMP_NAME",strEmpName);
OracleParameter param2 =objCom.Parameters.Add("INS_EMP_AG",strEmpAge);
param1.Size=200;
param2.Size=10;
/* The array length has to be passed as the Arraycount so that ODP.NET will execute
the Oracle procedure from the backend, the number of times mentioned in arraycount.
This loop is done by Oracle itself. So in a single connection 100's of records
can be inserted.
*/
objCom.ArrayBindCount = strEmpName.Length;
objCom.ExecuteNonQuery();
历史
- 2008年5月8日:初始发布