我们正在尝试使用Devart Oracle适配器,但与使用Oracle的DataAccess DLL的现有代码存在问题。我们得到command.ExecuteNonQuery()
下面的例子错误:Devart Oracle。插入并返回ID
ORA-01400:无法将NULL插入 ( “数据库名” “表格名” “COL3”)
下面是一些示例代码:
const string query = @"INSERT INTO table_name (table_name_id, col1, col2, col3)
VALUES(table_name_id_seq.nextval, :col1, :col2, :col3)
RETURNING table_name_id INTO :output_id";
OracleParameter outputParam = new OracleParameter(":output_id", OracleDbType.Long, ParameterDirection.Output);
OracleParameter[] parameters = new OracleParameter[]
{
outputParam,
new OracleParameter(":col1", OracleDbType.VarChar, col1, ParameterDirection.Input),
new OracleParameter(":col2", OracleDbType.VarChar, col2, ParameterDirection.Input),
new OracleParameter(":col3", OracleDbType.Long, col3, ParameterDirection.Input)
}
using (OracleCommand command = connection.CreateCommand())
{
command.CommandText = query;
command.CommandType = CommandType.Text;
command.Parameters.AddRange(parameters);
command.ExecuteNonQuery();
int outputId = Convert.ToInt32(outputParam.Value.ToString());
}
我们做错了什么?我们试图使用PK序列插入一行,并在一个查询中返回该行的PK。
此外,查询运行良好,如果我删除输出参数和查询中的返回行。
我发生了两种可能性。使用命名参数有时无法按预期工作,并且必须按正确的顺序给它们 - 所以将输出参数移动到数组的末尾可能会有所帮助。但是更可能的是,从[this](http://www.devart.com/dotconnect/oracle/articles/parameters.html)看起来也许你不应该在第一个参数的'OracleParameter'中冒号?不是我用过的东西,尽管如此猜测真的... – 2013-02-11 22:22:44
@AlexPoole感谢您的想法。我尝试按顺序添加参数并收到相同的错误。我认为按命名绑定并不重要?我还尝试在创建参数时删除冒号,但仍收到错误消息。有趣的是,我们有数以百计的其他查询都以相同的方式创建了这些参数,并且它们似乎都起作用。只是这个查询中有RETURNING关键字。 – xbrady 2013-02-12 00:05:07
顺序应该不重要,但我看到它不起作用(现在无法找到链接)。无论如何,这不是问题。对不起,我忍不住了! – 2013-02-12 10:12:36