C# 如何编写参数化的oracle插入查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12812634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write parameterized oracle insert query?
提问by Sreedhar goud
I am using oracle as my back end and i write insert query as follows, i don't know is it correct way are not,
我使用 oracle 作为我的后端,我编写插入查询如下,我不知道它是否正确,不是,
insert into Emp_table (SL_NO,empane,empid,salaray) values(1,'sree',1002 ,:salary);
here in query i am calculating salary with stored procedure and getting as my out parameter salary so i has to pass that to my insert query so how can i write. if i write like as shown i am getting below errors
在查询中,我正在使用存储过程计算薪水并获取作为我的输出参数薪水,因此我必须将其传递给我的插入查询,以便我如何编写。如果我写成如图所示,我会遇到以下错误
ORA-06550: line 1, column 175:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 1, column 7:
PL/SQL: SQL Statement ignored
kindly help me.
请帮助我。
采纳答案by Furqan Safdar
Assuming salary amount is 20000, You can try this code:
假设薪水是 20000,你可以试试这个代码:
var commandText = "insert into Emp_table (SL_NO,empane,empid,salaray) values(:SL_NO,:empane,:empid,:salary)";
using (OracleConnection connection = new OracleConnection(connectionString))
using (OracleCommand command = new OracleCommand(commandText, connection))
{
command.Parameters.AddWithValue("SL_NO", 1);
command.Parameters.AddWithValue("empane", "sree");
command.Parameters.AddWithValue("empid", 1002);
command.Parameters.AddWithValue("salaray", 20000);
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
}
回答by James Lawruk
Microsoft deprecated their Oracle provider(System.Data.OracleClient) so use a third party provider such as Oracle's Data Provider for .NET. The code example below is essentially the same as FSX's answer, just without the convenience of the AddWithValuemethod.
Microsoft弃用了他们的 Oracle 提供程序(System.Data.OracleClient),因此请使用第三方提供程序,例如Oracle 的 Data Provider for .NET。下面的代码示例与FSX 的 answer基本相同,只是没有AddWithValue方法的方便。
command.Parameters.Add(new OracleParameter("SL_NO", 1));
command.Parameters.Add(new OracleParameter("empane", "sree"));
回答by Ashish Modi
strin sql= "insert into Emp_table (SL_NO,empane,empid,salaray) values(:SL_NO,:empane,:empid,:salary)";
OracleCommand command = new OracleCommand(sql, connection)
command.Parameters.Add(new OracleParameter("SL_NO", 1);
command.Parameters.Add(new OracleParameter("empane", "sree"));
command.Parameters.Add(new OracleParameter(("empid", 1002));
command.Parameters.Add(new OracleParameter(("salaray", 20000));
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();

