在.NET(ODP.NET)中,我如何通过名称访问oracle参数?

cnwbcb6i  于 2023-06-22  发布在  Oracle
关注(0)|答案(1)|浏览(120)

使用ODP和VB.NET,我创建了带参数的insert语句,类似于下面的代码:

Dim objOracleParameter As OracleParameter
Dim objCommand As New OracleCommand

objOracleParameter = New OracleParameter
objOracleParameter.ParameterName = ":TestField"

objOracleParameter.OracleDbType = [Enum].Parse(GetType(OracleDbType), "Varchar2")
objOracleParameter.Value = "Data1"
Dim strSQL As String = "INSERT INTO MyTable (TestField) Values (:TestField)"
objCommand.Parameters.Add(objOracleParameter)
objCommand.CommandText = strSQL
objCommand.ExecuteNonQuery()

objOracleParameter = New OracleParameter
objOracleParameter.ParameterName = ":TestField"

objCommand = New OracleCommand

objOracleParameter.OracleDbType = [Enum].Parse(GetType(OracleDbType), "Varchar2")
objOracleParameter.Value = "Data2"
Dim strSQL As String = "INSERT INTO MyTable (TestField) Values (:TestField)"
objCommand.Parameters.Add(objOracleParameter)
objCommand.CommandText = strSQL
objCommand.ExecuteNonQuery()

目前,我为每个要插入的记录执行此代码,我认为这是低效的。相反,我想创建一次命令,然后为每个要插入的新值更改参数值。然而,我一直无法找到一个例子,如何做到这一点,我不知道如果它不能做到这一点。我的伪代码描述了我想做的事情如下。请注意,我只想更新参数值,其他一切保持不变:

Dim objOracleParameter As OracleParameter
Dim objCommand As New OracleCommand

objOracleParameter = New OracleParameter
objOracleParameter.ParameterName = ":TestField"

objOracleParameter.OracleDbType = [Enum].Parse(GetType(OracleDbType), "Varchar2")
objOracleParameter.Value = "Data1"
Dim strSQL As String = "INSERT INTO MyTable (TestField) Values (:TestField)"
objCommand.Parameters.Add(objOracleParameter)
objCommand.CommandText = strSQL
objCommand.ExecuteNonQuery()

objOracleParameter(":TestField").Value = "Data2"
objCommand.ExecuteNonQuery()

更新:

我简化了我在上面做的事情。实际的用例是创建一个通用例程,通过定义表中的数据布局并使用它来解析和验证数据,该例程允许验证和导入数据到各种表中。该例程目前有效,但我正在努力提高性能。
我发现需要导入的最大文件有超过700,000行,每行有~45个字段。实施性能改进至关重要。

cetgtptt

cetgtptt1#

我发现需要导入的最大文件有超过700,000行,每行有~45个字段。
这听起来像是OracleBulkCopy类型的工作。性能会更好。
但是有时候你确实有一组合理的记录要在一个紧密的循环中处理。为此,我将代码结构如下。

Dim SQL As String = "
INSERT INTO MyTable (StringField1, IntField2, DateTimeField3) 
VALUES 
( :StringField1, :IntField2, :DateTimeField3);"

' Returns a collection of objects with properties matching the query above
Dim data = GetDataForInsert() 

Using cn As New OracleConnection("connection string here"), _
      cmd As New OracleCommand(SQL, cn)

    Dim sf1 = cmd.Parameters.Add(":StringField1", OracleDbType.NVarchar2, 30)
    Dim if2 = cmd.Parameters.Add(":IntField2", OracleDbType.Int32)
    Dim df3 = cmd.Parameters.Add(":DateTimeField3", OracleDbType.Date)

    cn.Open()
    For Each item In Data
        sf1.Value = item.StringField1
        if2.Value = item.IntField2
        df3.Value = item.DateTimeField3

        ' OR
        cmd.Parameters(":StringField1").Value = item.StringField1
        cmd.Parameters(":IntField2").Value = item.IntField2
        cmd.Parameters(":DateTimeField3").Value = item.DateTimeField3
 
        ' One or the other of the above blocks, not both

        cmd.ExecuteNonQuery()
    Next
End Using

相关问题