我们使用Oracle Odp.Net(连接到Oracle 9)遇到了一个奇怪的错误。下面的代码片段说明了这个问题。
这是我们遇到的错误:
ORA-00600:内部错误代码,参数:[15419],[PL / SQL执行期间出现严重错误],[],[],[],[],[],[]
ORA-06544:PL / SQL:内部错误,参数:[78502],[],[],[],[],[],[],[]
ORA-06553:PLS-801:内部错误[78502]
谷歌搜索周围让我们怀疑(尽管我们并不完全确定)Odp.Net不支持传递一组时间戳。
所以问题是2倍:
说明问题的C#控制台程序:
using System;
using System.Collections;
using System.Data;
using Oracle.DataAccess.Client;
class Program
{
private const string _db = "<db>";
private const string _username = "<user>";
private const string _password = "<password>";
private const string _storedProcedureName = "<sproc>";
static void Main(string[] args)
{
var connectionString = string.Format(
"data source={0};user id={1};password={2}",
_db, _username, _password);
var connection = new OracleConnection(connectionString);
try
{
connection.Open();
var timeStamps = new[] { DateTime.Now, DateTime.Now };
var parameter = new OracleParameter("inTimeStamps", OracleDbType.TimeStamp)
{
Direction = ParameterDirection.Input,
CollectionType = OracleCollectionType.PLSQLAssociativeArray,
Size = timeStamps.Length,
Value = timeStamps
};
var command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = _storedProcedureName;
command.Parameters.Add(parameter);
command.ExecuteReader();
}
finally
{
connection.Close();
}
}
}
代码正在调用以下PL / SQL存储过程
TYPE ArrayOfTimestamps is table of timestamp index by binary_integer;
PROCEDURE TestOdpTimeStamp (inTimeStamps in ArrayOfTimestamps)
IS
test number;
BEGIN
select 1 into test from dual;
END;
答案 0 :(得分:3)
您可以将嵌套的时间戳表而不是关联数组传递给PL / SQL过程。
您需要odp.net 11.1.0.6.20或更高版本,您可以将odp.net 11.1.0.6.20连接到Oracle 9服务器。
以Oracle用户testts身份执行:
create or replace type MyTimeStamp as object
(
my timestamp
)
/
create or replace type mytimestamp_table as table of MyTimeStamp
/
create table testinserttimestamp
( my timestamp);
create or replace procedure test_timestamp_table (p_in in mytimestamp_table)
is
begin
for i in p_in.first..p_in.last loop
insert into testinserttimestamp values (p_in(i).my);
end loop;
commit;
end;
在C#中,使用名为button1的按钮创建一个表单,并执行...
using System;
using System.Data;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
namespace TestTimeStamp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class MyUdtTimeStamp : INullable, IOracleCustomType
{
[OracleObjectMappingAttribute("MY")]
public OracleTimeStamp My { get; set; }
public bool IsNull
{
get { return false;}
}
public void FromCustomObject(OracleConnection con, IntPtr pUdt)
{
OracleUdt.SetValue(con, pUdt, "MY", My);
}
public void ToCustomObject(OracleConnection con, IntPtr pUdt)
{
My = (OracleTimeStamp)OracleUdt.GetValue(con, pUdt, "MY");
}
}
[OracleCustomTypeMappingAttribute("TESTTS.MYTIMESTAMP")]
public class StudentFactory : IOracleCustomTypeFactory
{
public IOracleCustomType CreateObject()
{
return new MyUdtTimeStamp();
}
}
[OracleCustomTypeMappingAttribute("TESTTS.MYTIMESTAMP_TABLE")]
public class PersonArrayFactory : IOracleArrayTypeFactory
{
public Array CreateArray(int numElems)
{
return new MyUdtTimeStamp[numElems];
}
public Array CreateStatusArray(int numElems)
{
return null;
}
}
private void button1_Click(object sender, EventArgs e)
{
OracleConnectionStringBuilder b = new OracleConnectionStringBuilder();
b.UserID = "testts";
b.Password = "ts";
b.DataSource = "ora11";
using (OracleConnection conn = new OracleConnection(b.ToString())) {
conn.Open();
using (OracleCommand comm = conn.CreateCommand())
{
comm.CommandText = "begin test_timestamp_table(:1); end;";
OracleParameter p = new OracleParameter();
p.OracleDbType = OracleDbType.Array;
p.Direction = ParameterDirection.Input;
p.UdtTypeName = "TESTTS.MYTIMESTAMP_TABLE";
MyUdtTimeStamp[] times = new MyUdtTimeStamp[2];
MyUdtTimeStamp m1 = new MyUdtTimeStamp();
m1.My = new OracleTimeStamp(DateTime.Now);
MyUdtTimeStamp m2 = new MyUdtTimeStamp();
m2.My = new OracleTimeStamp(DateTime.Now);
times[0] = m1;
times[1] = m2;
p.Value = times;
comm.Parameters.Add(p);
comm.ExecuteNonQuery();
}
conn.Close();
}
}
}
}
在Oracle中做...
SQL> select * from testinserttimestamp;
MY
-------------------------------------------------
12-10-09 21:13:54,328125
12-10-09 21:13:55,171875
答案 1 :(得分:2)
有一条Metalink说明(788282.1)声明这可能是传递不受支持的数据类型的错误。不支持TIMESTAMP。您可以通过在C#代码中构建匿名PL / SQL块并从该块中调用有问题的存储过程来解决它。
编辑:
由于显而易见的原因,我无法发布Metalink的代码。
如果您的数组包含大量值,则解决方法是有问题的,因为匿名PL / SQL块必须包含代码,以通过绑定变量显式地为数组中的每个条目赋值。它很笨重。这说明了这个想法:
comm.CommandText = "declare "+
"theTS mytimestamp_table;"+
"begin"+
" theTS(1):= :1;"+
" theTS(2):= :2;"+
" test_timestamp_table(theTS);"+
" end;";
然后,您必须构造参数列表,以便为每个绑定变量提供值。
答案 2 :(得分:2)
我最近也遇到了这个问题,但是我通过使用字符串和to_timestamp以不同的方式克服了这些问题,我希望这可以帮助任何进一步下线的人遇到这个问题: http://timscyclingblog.wordpress.com/2011/10/07/oracle-plsqlassociativearray-timestamp-workaround/