VS2008 C#OracleCommand不使用以

时间:2018-02-01 13:25:51

标签: c# oracle windows-ce

代码问题出在使用CoreLab和Oracle SQL后端在VS2008中编写的WinCE应用程序中。详情如下所示 : 将20位数字添加到参数列表中以用于执行过程。如果数字以00结尾,则程序的打印输出表明该值从未到达过程,但在其他情况下它可以正常工作。

下面是添加参数并执行过程的代码:

public static List<OracleParameter> ExecProc(string proc, List<OracleParameter> parameters, OracleConnection conn)
{
    if (conn == null)
    {
        throw new Exception("Unable to connect");
    }

    if (proc == null || proc == "")
    {
        throw new Exception("Procedure name cannot be blank");
    }

    CheckConnState(conn);

    List<OracleParameter> paramOutList = new List<OracleParameter>();
    OracleCommand com = new OracleCommand();
    com.Connection = conn;
    OracleTransaction trans = conn.BeginTransaction();

    com.CommandTimeout = 0;
    com.CommandText = proc;
    com.CommandType = CommandType.StoredProcedure;
    foreach (OracleParameter param in parameters)
    {
        com.Parameters.Add(param.ParameterName, param.OracleDbType).Value = param.Value;
        com.Parameters[param.ParameterName].Direction = param.Direction;
        if (param.Direction == ParameterDirection.Output)
        {
            paramOutList.Add(com.Parameters[param.ParameterName]);
        }
    }

    try
    {
        com.ExecuteNonQuery();

        //Set the output parameter values for use outside
        if (paramOutList.Count > 0)
        {
            foreach (OracleParameter p in paramOutList)
            {
                p.Value = com.Parameters[p.ParameterName].Value;

                //Check if there are errors and either rollback or commit. We can't leave the transaction open
                if (p.ParameterName.IndexOf("error") > 0)
                {
                    if (p.Value.ToString() != null && p.Value.ToString() != "")
                    {
                        trans.Rollback();
                    }
                    else
                    {
                        trans.Commit();
                    }
                }
            }
        }
        else
        {
            trans.Commit();
        }
        return paramOutList;
    }
    catch
    {
        trans.Rollback();
        return paramOutList;
    }
}

编辑: 下面是调用上述方法的示例代码。

List<OracleParameter> paramList = new List<OracleParameter>
{
    GenericLib.AddParam("pn_id", OracleDbType.Number, iLongId),
    GenericLib.AddParam("pn_indicator", OracleDbType.Number, iInd),
    GenericLib.AddParam("pn_user_id", OracleDbType.Number, iUserId),
    GenericLib.AddParam("pv_in_code", OracleDbType.VarChar, null, ParameterDirection.Output),
    GenericLib.AddParam("pv_out_code", OracleDbType.VarChar, null, ParameterDirection.Output),
    GenericLib.AddParam("pv_error_code", OracleDbType.VarChar, null, ParameterDirection.Output)
};

List<OracleParameter> output = GenericLib.ExecProc("sample_procedure", paramList, conn);

从我所看到的第一个参数永远不会到达数据库层,因为我在proc的第一行打印输出。

0 个答案:

没有答案