如何在c#中为返回值编写oracle插入查询?
我编写的查询没有返回值,如下所示:如何在c#中为返回值编写oracle插入查询?,c#,sql,oracle,C#,Sql,Oracle,我编写的查询没有返回值,如下所示: con.Open(); OracleCommand cmd= CreateCommand(); cmd.CommandText = $"INSERT INTO {"Customer".GetDoubleQuoted()}({"City".GetDoubleQuoted()},{"CompanyName".GetDoubleQuoted()} , {"ContactName".GetDoubleQuoted()} ,{"Country".GetDo
con.Open();
OracleCommand cmd= CreateCommand();
cmd.CommandText = $"INSERT INTO {"Customer".GetDoubleQuoted()}({"City".GetDoubleQuoted()},{"CompanyName".GetDoubleQuoted()} , {"ContactName".GetDoubleQuoted()} ,{"Country".GetDoubleQuoted()},{"CustomerID".GetDoubleQuoted()},{"Phone".GetDoubleQuoted()}) VALUES ('Chicago', 'Amisys','Jwk', 'USA','aaa', '123456') \n Returning {"City".GetDoubleQuoted()} into :city";
cmd.Connection = con;
cmd.Parameters.Add(new OracleParameter
{
ParameterName = ":city",
OracleDbType = OracleDbType.Varchar2,
Direction = ParameterDirection.Output
});
cmd.ExecuteNonQuery();
var value= cmd.Parameters[":city"].Value.ToString();
有人能帮我找出为什么价值没有显现出来吗
提前谢谢。
joon错误是ODP.NET中的一个小错误,每次收到
varchar2
returnValue
参数时,必须指定大小。因此,只需在示例中添加一行Size=32000,
,它就可以工作了
请参阅下面的完整代码:
con.Open();
OracleCommand cmd= CreateCommand();
cmd.CommandText = $@"INSERT INTO {"Customer".GetDoubleQuoted()(
{"City".GetDoubleQuoted()},
{"CompanyName".GetDoubleQuoted()},
{"ContactName".GetDoubleQuoted()},
{"Country".GetDoubleQuoted()},
{"CustomerID".GetDoubleQuoted()},
{"Phone".GetDoubleQuoted()}
) VALUES ('Chicago', 'Amisys','Jwk', 'USA','aaa', '123456')
Returning {"City".GetDoubleQuoted()} into :city";
cmd.Connection = con;
cmd.Parameters.Add(new OracleParameter
{
ParameterName = ":city",
OracleDbType = OracleDbType.Varchar2,
Size = 32000,
Direction = ParameterDirection.ReturnValue
});
cmd.ExecuteNonQuery();
var value= cmd.Parameters[":city"].Value.ToString();
注意:
32000
是varchar2
列的最大大小。尝试将参数方向设置为ReturnValueThank Roy,但它没有做任何更改。感谢Roy,我得到了预期的返回值。再次感谢。