1
不確定爲什麼我的代碼在從VB調用時返回一個錯誤,但PLSQL(Oracle)中的類似代碼沒有返回錯誤。從Visual Basic中調用Oracle函數
Oracle函數:
CREATE OR REPLACE FUNCTION GET_CUST_STRING_FROM_DB (pcustid NUMBER) RETURN VARCHAR2 AS
returnstring VARCHAR2(200);
vcustomer customer%rowtype;
BEGIN
SELECT * INTO vcustomer
FROM customer
WHERE custid = pcustid;
returnstring := 'CustID: ' || vcustomer.custid || ' Name: ' || vcustomer.custname || ' Status: ' || vcustomer.status || ' SalesYTD: ' || vcustomer.sales_ytd;
RETURN returnstring;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20021,'Error: Customer ID not found');
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000,SQLERRM);
END;
/
Oracle調用:
set serveroutput on;
begin
dbms_output.put_line(get_cust_string_from_db(1));
end;
甲骨文輸出:
Anonymous Block Completed
CustID: 1 Name: Colin Smith Status: OK SalesYTD: 0
VB代碼:
Private Sub GetCustString()
Try
Dim connOracle As Oracle.DataAccess.Client.OracleConnection
Dim commOracle As New Oracle.DataAccess.Client.OracleCommand
Dim paramOracle As Oracle.DataAccess.Client.OracleParameter
connOracle = CreateConnection()
commOracle.Connection = connOracle
commOracle.CommandType = CommandType.StoredProcedure
commOracle.CommandText = "GET_CUST_STRING_FROM_DB"
paramOracle = New Oracle.DataAccess.Client.OracleParameter
paramOracle.ParameterName = "pcustid"
paramOracle.DbType = DbType.Int16
paramOracle.Value = tbGetCustStringCustID.Text
paramOracle.Direction = ParameterDirection.Input
commOracle.Parameters.Add(paramOracle)
paramOracle = New Oracle.DataAccess.Client.OracleParameter
paramOracle.ParameterName = "pReturnValue"
paramOracle.DbType = DbType.String
paramOracle.Direction = ParameterDirection.ReturnValue
commOracle.Parameters.Add(paramOracle)
lbOutput.Items.Add(commOracle.Parameters.Item("pcustid").Value)
lbOutput.Items.Add("--------------------------------------------")
lbOutput.Items.Add("Getting Details for CustID " & tbGetCustStringCustID.Text)
connOracle.Open()
commOracle.ExecuteNonQuery()
Dim vStr As String
vStr = commOracle.Parameters.Item("pReturnValue").Value.ToString
lbOutput.Items.Add(vStr)
connOracle.Close()
Catch ex As Exception
lbOutput.Items.Add(ex.Message)
End Try
End Sub
VB輸出:
1
-----------------------------------------
Getting Details for CustID 1
ORA-20021: Error: Customer ID not found
所以我不能確定,爲什麼這件事會發生。 我試圖捕捉參數的值,並確保它是一個實際的整數,而不是一個字符串。 CustID 1確實存在於數據庫中並被提交。
任何想法?
謝謝@Ciarán!訂單確實重要。我剛剛學到了一些我不想知道的東西。解除了......但也有點傷心。 –