2016-10-27 61 views
2

我想將數組傳遞給oracle過程。我搜索了它,首先我創建了一個名爲'dizi'的類型(如enter link description here)。所以它適用於oracle開發人員。問題是;我無法將我的c#數組作爲參數傳遞給過程。那麼我怎樣才能將我的數組傳遞給我的程序呢?將數組傳遞給Oracle過程從c#

這是我的代碼(當我執行,Oracle錯誤表示:不結合所有的變量)

public void InsertQuestion(List<string> area_list) 
{ 
    quest_areas = area_list.ToArray(); 
    command.Connection = connect; 
    connect.Open(); 

    var arry = command.Parameters.Add("Areas", OracleDbType.Varchar2); 
    arry.Direction = ParameterDirection.Input; 
    arry.CollectionType = OracleCollectionType.PLSQLAssociativeArray; 
    arry.Value = quest_areas; 


    command.CommandText ="TESTPROCEDURE(:Areas)"; 
    command.CommandType = CommandType.StoredProcedure; 
    command.ExecuteNonQuery(); 
    connect.Close(); 
} 
+0

您已經在執行'command.Parameters.Add'添加'DbParameter',內部是'OracleParameter' –

+0

但它不起作用? –

+0

什麼是錯誤,你可以檢查ODP.Net示例 –

回答

1
  1. 定義的陣列類型和過程:

    CREATE or replace PACKAGE Testpackage AS 
        TYPE Areas_t is table of VARCHAR(100) index by BINARY_INTEGER; 
        PROCEDURE TESTPROCEDURE(Areas IN Areas_t);  
    END Testpackage; 
    
  2. C#程序:

    public void InsertQuestion(IEnumerable<string> area_list) 
    { 
        var connect = new OracleConnection("YOUR CONNECTION STRING"); 
    
        var command = new OracleCommand("BEGIN Testpackage.Testprocedure(:Areas); END;", connect); 
    
        connect.Open(); 
    
        var arry = command.Parameters.Add("Areas", OracleDbType.Varchar2); 
    
        arry.Direction = ParameterDirection.Input; 
        arry.CollectionType = OracleCollectionType.PLSQLAssociativeArray; 
        arry.Value = area_list.ToArray(); 
        arry.Size = area_list.Count(); 
        arry.ArrayBindSize = area_list.Select(_ => _.Length).ToArray(); 
        arry.ArrayBindStatus = Enumerable.Repeat(OracleParameterStatus.Success, area_list.Count()).ToArray(); 
    
        command.ExecuteNonQuery(); 
    
        connect.Close(); 
    }