如果不存在,如何在我的表中插入新记录?在C#(oracle)中 [英] How to insert new record in my table if not exists? In C# (oracle)

查看:92
本文介绍了如果不存在,如何在我的表中插入新记录?在C#(oracle)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

如果不存在,我想在我的表中插入一个新行。

当我编写此代码时,例如:

它给了我一个错误

Hi all
I want to insert a new row into my table if does not exist.
When I write this code for example:
It gives me an error

Quote:

(Oracle.DataAccess.Client.OracleException:'ORA-00911 :)

字符不合适')

(Oracle.DataAccess.Client.OracleException: 'ORA-00911:)
Characters are not appropriate ')





我的尝试:





What I have tried:

using (OracleConnection connection = new OracleConnection(strConnection))
            {
                char quote = '"';
              string strCommand = @"insert into tableinfo( "+quote+"name"+quote+","+quote+"age"+quote+") "+
                "select :name,:age from dual where not exists (select "+quote+"age"+quote+" from tableinfo where "+quote+"age"+quote+" = :age);";
                     
                OracleCommand command = new OracleCommand(strCommand, connection);
                command.Parameters.Add( new OracleParameter("name", "Zoxit"));
                command.Parameters.Add( new OracleParameter("age", 15));

                connection.Open();
                int res = command.ExecuteNonQuery();
//Problem is here(Oracle.DataAccess.Client.OracleException: 'ORA-00911:)
//Characters are not appropriate '
                Console.WriteLine("Result = " + res.ToString());
                Console.ReadKey();

                //insert into tableinfo( "name","age") values(:name,:age)
            }

推荐答案

问题在于
string strCommand = @"insert into tableinfo( "+quote+"name"+quote+","+quote+"age"+quote+") "+
                "select :name,:age from dual where not exists (select "+quote+"age"+quote+" from tableinfo where "+quote+"age"+quote+" = :age);";

不需要所有这些引用



有关如何正确执行此操作的信息,请参阅此CP文章 .NET Developer的Oracle参数化查询 [ ^ ]

There is no need for all of those quotes

See this CP article on how to do it properly Oracle Parameterized Queries for the .NET Developer[^]


Thank you and I solved this problem by using. I removed last semicolon than it worked.
strCommand = @"insert into tableinfo( "+quote+"name"+quote+","+quote+"age"+quote+") "+
                "select :name,:age from dual where not exists (select "+quote+"age"+quote+" from tableinfo where "+quote+"age"+quote+" = :age)";


这篇关于如果不存在,如何在我的表中插入新记录?在C#(oracle)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆