C#相当于java编写的声明 [英] C# equivalent to java prepared statement
本文介绍了C#相当于java编写的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想C#创建一份准备好的声明。
I am trying to create a prepared statement in c#.
由于某些原因的一切,我尝试用一个exeption结束。
For some reason everything I try ends up with an exeption.
这是我的代码现在:
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into daily_cdr_logs " +
"(message) " +
"values " +
"(:message)";
cmd.Parameters.Add(:message, msg);
//OracleDbType.Int32, postpaid_duration, ParameterDirection.Input);
cmd.Prepare();
cmd.ExecuteNonQuery();
}
我收到exeption:操作是无效的,由于目前的状态的对象。
I am getting exeption: "Operation is not valid due to the current state of the object."
推荐答案
一个典型的Oracle编写的语句如下所示。
A typical Oracle prepared statement looks like this.
(注意,只有在准备好的声明中的定义有:
冒号和一个在 cmd.Parameters.AddWithValue
呼叫没有)
(notice that only the definition in the prepared statement has the :
colon, and the one in the cmd.Parameters.AddWithValue
call does not)
String msg = "something here";
using (OracleConnection con = new OracleConnection(...insert connection params here...))
{
con.Open();
OracleCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"
insert into daily_cdr_logs
(message)
values
(:message)";
cmd.Parameters.AddWithValue("message", msg);
cmd.ExecuteNonQuery();
}
这篇关于C#相当于java编写的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文