在没有存储过程的情况下使用 c# 映射 Oracle UDT
Mapping Oracle UDT using c# without stored procedure
我需要在我的 C# 应用程序中映射一个 Oracle 对象类型。网上有大量示例,包括此处和其他 sites/blogs,但所有示例都包括使用存储过程,我不应该这样做。
我这两天一直在找,最接近的是docs.oracle.com上的一篇文章,但是没有例子
任何人都可以举例说明这是如何实现的吗?
我正在使用 Oracle.DataAccess
class 与我的数据库和下面给出的简单 UDT 进行通信:
create or replace
TYPE "MYNUMBER_TYPE" AS OBJECT (
MyNumber NUMBER(13)
)
INSTANTIABLE NOT FINAL;
如果你想执行PL/SQL你可以像下面这样做。这足以撕裂世界统治本身。差不多。
注意,这没有经过测试,因为我这里没有 Oracle 数据库。然而,我在我当前的一个项目中使用了这种方法。
cmd = New OracleCommand("declare " +
" lSomeVarchar2 varchar2(255); " +
" lSomeNumber number; " +
" lSomeLong long; " +
"begin " +
" loop " +
" --do something fancy here " +
" end loop; " +
" --you can pass variables from outside: " +
" :parameterNumber:= lSomeNumber ; " +
" :parameterVarChar := lSomeLong; " +
"end;", conn);
//make these of direction output and you can get values back
cmd.Parameters.Add("parameterNumber", OracleDbType.Integer).Direction = ParameterDirection.Output;
cmd.Parameters.Add("parameterVarChar", OracleDbType.VarChar).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
//now you can get the values using something like
int cNumber = (int)cmd.Parameters("parameterNumber").Value;
String myString = (String) cmd.Parameters("parameterNumber").Value;
编辑 3 又名回答你的评论:
对于 IOracleCustomType-Interface 的用法:
同样,我无法对其进行测试,因为我仍然无法访问 Oracle 数据库。不过,让我们来做点魔术吧。
步骤 1: 在 C# 代码中创建一个继承自 IOracleCustomType 的自定义类型:
[OracleCustomTypeMapping("C##USER.MYNUMBER_TYPE")]
public class MyCustomClass : IOracleCustomType
然后,您必须为每个 class 成员指定 Oracle 吊坠。在下文中,名称 "MyNumber" 来自您问题中的自定义类型规范。
[OracleObjectMappingAttribute("MyNumber")]
public virtual int cNumber{get; set;}
此外,您必须覆盖方法 FromCustomObject 和 ToCustomObject:
//this one is used to map the C# class-object to Oracle UDT
public virtual void FromCustomObject(OracleConnection conn, IntPtr object){
OracleUdt.SetValue(conn, object, "MyNumber", this.cNumber);
}
//and this one is used to convert Oracle UDT to C# class
public virtual void ToCustomObject(OracleConnection conn, IntPtr object){
this.cNumber = ((int)(OracleUdt.GetValue(conn, object, "MyNumber")));
}
第 2 步: 在数据库中创建您已经创建的自定义类型。所以这里就不重复了。
第 3 步: 现在我们已经设置好了。让我们试试吧:
//first create your SQL-Statement
String statement = "SELECT MY_CUSTOM_TYPE_COLUMN FROM MY_SUPER_TABLE";
//then set up the database connection
OracleConnection conn = new OracleConnection("connect string");
conn.Open();
OracleCommand cmd = new OracleCommand(statement, conn);
cmd.CommandType = CommandType.Text;
//execute the thing
OracleDataReader reader = cmd.ExecuteReader();
//get the results
while(reader.Read()){
MyCustomClass customObject = new MyCustomClass();
//get the Object, here the magic happens
customObject = (MyCustomClass)reader.GetValue(0);
//do something with your object
}
我需要在我的 C# 应用程序中映射一个 Oracle 对象类型。网上有大量示例,包括此处和其他 sites/blogs,但所有示例都包括使用存储过程,我不应该这样做。
我这两天一直在找,最接近的是docs.oracle.com上的一篇文章,但是没有例子
任何人都可以举例说明这是如何实现的吗?
我正在使用 Oracle.DataAccess
class 与我的数据库和下面给出的简单 UDT 进行通信:
create or replace
TYPE "MYNUMBER_TYPE" AS OBJECT (
MyNumber NUMBER(13)
)
INSTANTIABLE NOT FINAL;
如果你想执行PL/SQL你可以像下面这样做。这足以撕裂世界统治本身。差不多。
注意,这没有经过测试,因为我这里没有 Oracle 数据库。然而,我在我当前的一个项目中使用了这种方法。
cmd = New OracleCommand("declare " +
" lSomeVarchar2 varchar2(255); " +
" lSomeNumber number; " +
" lSomeLong long; " +
"begin " +
" loop " +
" --do something fancy here " +
" end loop; " +
" --you can pass variables from outside: " +
" :parameterNumber:= lSomeNumber ; " +
" :parameterVarChar := lSomeLong; " +
"end;", conn);
//make these of direction output and you can get values back
cmd.Parameters.Add("parameterNumber", OracleDbType.Integer).Direction = ParameterDirection.Output;
cmd.Parameters.Add("parameterVarChar", OracleDbType.VarChar).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
//now you can get the values using something like
int cNumber = (int)cmd.Parameters("parameterNumber").Value;
String myString = (String) cmd.Parameters("parameterNumber").Value;
编辑 3 又名回答你的评论:
对于 IOracleCustomType-Interface 的用法: 同样,我无法对其进行测试,因为我仍然无法访问 Oracle 数据库。不过,让我们来做点魔术吧。
步骤 1: 在 C# 代码中创建一个继承自 IOracleCustomType 的自定义类型:
[OracleCustomTypeMapping("C##USER.MYNUMBER_TYPE")]
public class MyCustomClass : IOracleCustomType
然后,您必须为每个 class 成员指定 Oracle 吊坠。在下文中,名称 "MyNumber" 来自您问题中的自定义类型规范。
[OracleObjectMappingAttribute("MyNumber")]
public virtual int cNumber{get; set;}
此外,您必须覆盖方法 FromCustomObject 和 ToCustomObject:
//this one is used to map the C# class-object to Oracle UDT
public virtual void FromCustomObject(OracleConnection conn, IntPtr object){
OracleUdt.SetValue(conn, object, "MyNumber", this.cNumber);
}
//and this one is used to convert Oracle UDT to C# class
public virtual void ToCustomObject(OracleConnection conn, IntPtr object){
this.cNumber = ((int)(OracleUdt.GetValue(conn, object, "MyNumber")));
}
第 2 步: 在数据库中创建您已经创建的自定义类型。所以这里就不重复了。
第 3 步: 现在我们已经设置好了。让我们试试吧:
//first create your SQL-Statement
String statement = "SELECT MY_CUSTOM_TYPE_COLUMN FROM MY_SUPER_TABLE";
//then set up the database connection
OracleConnection conn = new OracleConnection("connect string");
conn.Open();
OracleCommand cmd = new OracleCommand(statement, conn);
cmd.CommandType = CommandType.Text;
//execute the thing
OracleDataReader reader = cmd.ExecuteReader();
//get the results
while(reader.Read()){
MyCustomClass customObject = new MyCustomClass();
//get the Object, here the magic happens
customObject = (MyCustomClass)reader.GetValue(0);
//do something with your object
}