我正在开发使用ODP.NET来调用Oracle存储过程的应用程序。 当我在sqldeveloper中运行过程时它工作正常。 当我尝试使用ODP.NET调用过程时,它返回空结果。 我试图改变数据类型,值,但没有成功。
程序调用示例:
BEGIN "DB"."DB_API"."GETEXTENDEDCOLLECTEDDATA"(1, 50537, NULL, NULL, '20141230 00:00:00', '20150201 00:00:00', NULL, :P_CV_COLLECTEDDATA); END;
代码,在sqldeveloper中生成:
PROCEDURE GetExtendedCollectedData
( p_UserId IN SEC_VS_USER_T.USR_ID%TYPE,
p_SiteId IN SEC_NN_SERVER_T.SRV_ID%TYPE,
p_DeviceName IN DAT_DEVICE_INFO_T.DEVICE_NAME%TYPE,
p_PrptyName IN APP_PROPERTY_INFO_T.PRPTY_NAME%TYPE,
p_FromDate IN VARCHAR2,
p_ToDate IN VARCHAR2,
p_PrptyCategory IN APP_PROPERTY_INFO_T.PRPTY_CTGRY_CODE%TYPE,
p_cv_CollectedData IN OUT cvt_ExtCollectedData);
DECLARE
P_USERID NUMBER;
P_SITEID NUMBER;
P_DEVICENAME VARCHAR2(255);
P_PRPTYNAME VARCHAR2(255);
P_FROMDATE VARCHAR2(200);
P_TODATE VARCHAR2(200);
P_PRPTYCATEGORY NUMBER;
P_CV_COLLECTEDDATA DB.DB_API.cvt_ExtCollectedData;
BEGIN
P_USERID := 1;
P_SITEID := 50537;
P_DEVICENAME := NULL;
P_PRPTYNAME := NULL;
P_FROMDATE := '20140101 12:00:00';
P_TODATE := '20151212 11:59:59';
P_PRPTYCATEGORY := NULL;
P_CV_COLLECTEDDATA := NULL;
DB_API.GETEXTENDEDCOLLECTEDDATA(
P_USERID => P_USERID,
P_SITEID => P_SITEID,
P_DEVICENAME => P_DEVICENAME,
P_PRPTYNAME => P_PRPTYNAME,
P_FROMDATE => P_FROMDATE,
P_TODATE => P_TODATE,
P_PRPTYCATEGORY => P_PRPTYCATEGORY,
P_CV_COLLECTEDDATA => P_CV_COLLECTEDDATA
);
/* Legacy output:
DBMS_OUTPUT.PUT_LINE('P_CV_COLLECTEDDATA = ' || P_CV_COLLECTEDDATA);
*/
:P_CV_COLLECTEDDATA := P_CV_COLLECTEDDATA; --<-- Cursor
--rollback;
END;
C#代码:
public const string DateFormat = "yyyyMMdd hh:mm:ss";
using (var connection = new OracleConnection(ConnectionString))
{
connection.Open();
using (var command = new OracleCommand
{
Connection = connection,
CommandText = "\"DB\".\"DB_API\".\"GETEXTENDEDCOLLECTEDDATA\"",
CommandType = CommandType.StoredProcedure
})
{
command.Parameters.Add("p_UserId", OracleDbType.Int32).Value = userId;
command.Parameters.Add("p_SiteId", OracleDbType.Int32).Value = siteId;
command.Parameters.Add("p_DeviceName", OracleDbType.Varchar2).Value = deviceName;
command.Parameters.Add("p_PrptyName", OracleDbType.Varchar2).Value = prptyName;
command.Parameters.Add("p_FromDate", OracleDbType.Varchar2).Value = fromDate.ToString(DateFormat);
command.Parameters.Add("p_ToDate", OracleDbType.Varchar2).Value = toDate.ToString(DateFormat);
var propertyCategoryParam = new OracleParameter("p_PrptyCategory", OracleDbType.Int32)
{
IsNullable = true,
};
if (string.IsNullOrEmpty(propertyCategory))
{
propertyCategoryParam.Value = null;
}
else
{
propertyCategoryParam.Value = Int32.Parse(propertyCategory);
}
command.Parameters.Add(propertyCategoryParam);
var refCursorParam = new OracleParameter("p_cv_CollectedData", OracleDbType.RefCursor)
{
Direction = ParameterDirection.InputOutput,
};
command.Parameters.Add(refCursorParam);
for (int i = 0; i < command.Parameters.Count; i++)
{
var p = command.Parameters[i];
Log.Info("CSTR: " + p.ParameterName + ": " + p.Value + " - " + p.OracleDbType, this);
}
int result = command.ExecuteNonQuery();
Log.Info("command.ExecuteNonQuery: " + result, this);
var reader = ((OracleRefCursor) command.Parameters["p_cv_CollectedData"].Value).GetDataReader();
Log.Info("command.Parameters[p_cv_CollectedData].Value: " + command.Parameters["p_cv_CollectedData"].Value, this);
Log.Info("reader: " + reader, this);
Log.Info("reader.FetchSize: " + reader.FetchSize, this);
Log.Info("reader.FieldCount: " + reader.FieldCount, this);
Log.Info("reader.Depth: " + reader.Depth, this);
Log.Info("reader.HasRows: " + reader.HasRows, this);
Log.Info("reader.RowSize: " + reader.RowSize, this);
记录结果:
4276 17:04:15 INFO CSTR: p_UserId: 1 - Int32
4276 17:04:15 INFO CSTR: p_SiteId: 50537 - Int32
4276 17:04:15 INFO CSTR: p_DeviceName: NULL - Varchar2
4276 17:04:15 INFO CSTR: p_PrptyName: NULL - Varchar2
4276 17:04:15 INFO CSTR: p_FromDate: 20140101 12:00:00 - Varchar2
4276 17:04:15 INFO CSTR: p_ToDate: 20151212 11:59:59 - Varchar2
4276 17:04:15 INFO CSTR: p_PrptyCategory: - Int32
4276 17:04:15 INFO CSTR: p_cv_CollectedData: - RefCursor
4276 17:04:15 INFO command.ExecuteNonQuery: -1
4276 17:04:15 INFO command.Parameters[p_cv_CollectedData].Value: Oracle.DataAccess.Types.OracleRefCursor
4276 17:04:15 INFO reader: Oracle.DataAccess.Client.OracleDataReader
4276 17:04:15 INFO reader.FetchSize: 131072
4276 17:04:15 INFO reader.FieldCount: 19
4276 17:04:15 INFO reader.Depth: 0
4276 17:04:15 INFO reader.HasRows: False
4276 17:04:15 INFO reader.RowSize: 6476