oracle 預存程序定義及調試,並最終被C# 調用 代碼,
C# 調用預存程序
參考了很多文章,寫了如下文字,算是分享吧
目的:更改積分,並作一定校正
一、一般的調試方法:
方法一:帶返回out參數,必須定義變數 myresult
DECLARE
myresult INT;
BEGIN
P_CHANGEVIPBALANCE('011111111',-1,'TEST',myresult);
END;
/*
如果不帶輸出參數,則可以直接用:
方法二:call P_CHANGEVIPBALANCE('018604712233',-1,'TEST');//假設本身就沒有要輸出的參數,即預存程序定義為:
CREATE OR REPLACE PROCEDURE WX_120719_FLXT.p_changevipbalance ( vipno IN VARCHAR2 , point in VARCHAR2, str_reason IN VARCHAR2 ) //相比之前的沒有:,myresult OUT int 這個out 參數 */
方法三:
更詳細的調試方法還是PLSQL,選擇要調試的過程名,找test,如果不能進入到裡面去,好像要要編譯,產生debug資訊之類的。
二、預存程序定義:2015-7-18 20:00:21更新備忘計算方法
CREATE OR REPLACE PROCEDURE WX_120719_FLXT.p_changevipbalance ( vipno IN VARCHAR2 , point in VARCHAR2, str_reason IN VARCHAR2 ,myresult OUT int )
IS
point_old INT;
point_updated INT;
yhjerror EXCEPTION;
tempint INT;
tempa INT;
BEGIN
myresult:=0;
SELECT M.POINTS
INTO point_old
FROM MEMBERSHIP M
WHERE TRIM(M.MEMBER_CODE) =vipno;
UPDATE MEMBERSHIP M
SET M.POINTS= M.POINTS+point,
M.REMARK=TRIM(M.REMARK)||TO_CHAR(SYSDATE,'YY-MM-DD HH24:MI:SS')||':'||str_reason
WHERE TRIM(M.MEMBER_CODE)=vipno;
myresult:=SQL%ROWCOUNT;
dbms_output.put_line(point_old);
SELECT M.POINTS
INTO point_updated
FROM MEMBERSHIP M
WHERE TRIM(M.MEMBER_CODE) =vipno;
tempa:=ABS((point_old)-(point_updated));
tempint:=ABS(point);
IF ((tempa <>tempint ) or (myresult<>1)) THEN -- AND
BEGIN
myresult:=0;
RAISE yhjerror;
end;
END IF ;
EXCEPTION
WHEN yhjerror THEN
BEGIN
ROLLBACK;
END;
COMMIT;
END p_changevipbalance;
三、預存程序被C#調用,代碼:
public static String CAL_changevipbalancebyStoredProcedure(string vipno, int point, string str_reason )
{//需要增加日誌
string strCmd = OracleAccess.str_vip_update1 + point + OracleAccess.str_vip_update2 + vipno.Trim() + "\'";
int old_points = 0; int after_point = 0; int kkk = 0; int myresult = 0;
//i = Convert.ToInt32(DBUtil.SqlExecuteScalar(strCmd.Replace("TESTA", OracleAccess.str_USER)));
if (exist_vip(vipno))
{
old_points = int.Parse(CAL_VipBalance(vipno));
OracleParameter[] parameters ={
new OracleParameter("vipno",OracleType.VarChar,50),
new OracleParameter("point",OracleType.VarChar,30),
new OracleParameter("str_reason",OracleType.VarChar,20),
new OracleParameter("myresult",OracleType.Int32)
};
parameters[0].Value = vipno; parameters[1].Value = point; parameters[2].Value = str_reason; parameters[3].Value = myresult; //parameters[13].Value = str_sku;// parameters[10].Value = str_no; parameters[11].Value = str_sku;
parameters[0].Direction = ParameterDirection.Input; parameters[1].Direction = ParameterDirection.Input; parameters[2].Direction = ParameterDirection.Input; parameters[3].Direction = ParameterDirection.Output;
try
{
YHJ_StoredProcedure.RunProcedure("WX_120719_FLXT.p_changevipbalance", parameters);
kkk = Convert.ToInt32(parameters[3].Value);
if (kkk>=1)
return OracleAccess.str_db_operate_sucess;
else
return "ERROR內部校正出錯";
}
catch (Exception e)
{
throw e;
}
//DBUtil.SqlExecuteNonQuery(strCmd.Replace("TESTA", OracleAccess.str_USER)); //ok 防注入,不用
//after_point = int.Parse(CAL_VipBalance(vipno));
//if ((after_point - old_points) == point)
// return OracleAccess.str_db_operate_sucess;
//else
// return "ERROR內部校正出錯";
}
else
return "ERROR會員不存在";
//return Convert.ToInt32(DBUtil.SqlExecuteScalar(strCmd.Replace("TESTA", OracleAccess.str_USER)));
}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。