VB.NET调用PL / SQL过程

时间:2014-09-09 12:57:11

标签: vb.net string plsql return-value

所以我从vb.net函数调用下面的pl / sql语句。无论是否存在异常,结果都会添加到ListBox中。当我使用函数ORA-06502: PL/SQL: numeric or value error: character string buffer too small时,我一直收到以下错误我尝试重写pl / sql语句和vb.net语句,但我似乎无法找到避免这种情况的方法错误。有什么建议??

create or replace function GET_CUST_STRING_FROM_DB(pcustid number)
return varchar2 as
vcustid number;
vcustname varchar2(100);
vcuststatus varchar2(100);
vcustsales number;

begin

  select custid, custname, status, sales_ytd into vcustid, vcustname, vcuststatus, vcustsales from customer where custid = pcustid;

  return 'Custid: '||vcustid||' Name: '||vcustname||' Status: '||vcuststatus||' SalesYTD: '||vcustsales;
exception
  when NO_DATA_FOUND then
    raise_application_error(-20021, 'Error: Customer ID not found');
  when others then
    raise_application_error(-20000, SQLERRM);
end;

上述函数由以下vb.net函数调用

Private Sub GetCustString(custID As Integer)
        Dim connOracle As Oracle.DataAccess.Client.OracleConnection
        Dim cmd As New Oracle.DataAccess.Client.OracleCommand
        Dim param As Oracle.DataAccess.Client.OracleParameter
        Dim vOutcome As String

        param = New Oracle.DataAccess.Client.OracleParameter
        param.ParameterName = "vreturn"
        param.DbType = DbType.String
        param.Direction = ParameterDirection.ReturnValue
        cmd.Parameters.Add(param)

        param = New Oracle.DataAccess.Client.OracleParameter
        param.ParameterName = "pcustid"
        param.DbType = DbType.Int64
        param.Direction = ParameterDirection.Input
        param.Value = custID
        cmd.Parameters.Add(param)

        connOracle = CreateConnection()
        lsDisplay.Items.Add("Getting details for custID: " & custID)

        Try
            connOracle.Open()
            cmd.Connection = connOracle
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "GET_CUST_STRING_FROM_DB"
            cmd.ExecuteNonQuery()
            vOutcome = cmd.Parameters.Item("pReturn").Value.ToString
            connOracle.Close()
            lsDisplay.Items.Add(vOutcome)
        Catch ex As Exception
            Dim split As String() = ex.Message.Split(New String() {"ORA-06512:"}, StringSplitOptions.RemoveEmptyEntries)
            vOutcome = split(0)
            lsDisplay.Items.Add(vOutcome)
        Finally
        End Try
    End Sub

这是CUSTOMER表定义

CREATE TABLE "S5849497"."CUSTOMER" 
   (    "CUSTID" NUMBER, 
    "CUSTNAME" VARCHAR2(100 BYTE), 
    "SALES_YTD" NUMBER, 
    "STATUS" VARCHAR2(7 BYTE), 
     PRIMARY KEY ("CUSTID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"  ENABLE, 
     CONSTRAINT "CK_ID_RANGE" CHECK (custid > 0 and custid < 500) ENABLE
   ) SEGMENT CREATION IMMEDIATE 
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS" ;

1 个答案:

答案 0 :(得分:0)

返回值字符串大小需要设置为更高的值。我将其设置为4000只是为了安全,但默认情况下必须将其设置为更低的值。这马上解决了这个问题

param.size = 4000