单个查询中的多个删除语句

时间:2017-10-24 10:52:01

标签: oracle ado.net

我正在尝试执行以下功能。但由于命令未正确结束,我收到错误。但是,当我在数据库中执行时,它正在正确执行:

 string sql = @"Delete FROM table1  WHERE idtest in (Select istest from table12 Where case_id=:caseID); 
                           Delete from table12 Where case_id=:caseID ;
                           Delete from table13 Where case_id=:caseID ;
                           Delete from table14 Where case_id=:caseID ;
                           Delete from table15 Where case_id=:caseID ;
                           ";

            using (OracleConnection Conn = new OracleConnection())
            {
                try
                {
                    oh.InitOraConn(Conn);
                    using (OracleCommand cmd = new OracleCommand("", Conn))
                    {
                        cmd.Connection = Conn;
                        cmd.CommandText = sql;
                        cmd.Parameters.Add("caseID", caseId);
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
            }

那么请指导它出了什么问题?

1 个答案:

答案 0 :(得分:0)

  • 您只能执行单个SQL命令
  • ;不是必需的
像这样:

        using (OracleConnection Conn = new OracleConnection())
        {
            try
            {
                oh.InitOraConn(Conn);
                using (OracleCommand cmd = new OracleCommand("", Conn))
                {
                    cmd.Connection = Conn;
                    cmd.CommandText = @"Delete FROM table1  WHERE idtest in (Select istest from table12 Where case_id=:caseID)";
                    cmd.Parameters.Add("caseID", caseId);
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = @"Delete from table12 Where case_id=:caseID ";
                    cmd.Parameters.Add("caseID", caseId);
                    cmd.ExecuteNonQuery();
                    ... and so on

                }
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

如果你想要一些具有更多表和更多逻辑的事务性的东西,那么你可以考虑在数据库中构建一个存储过程并用你的参数调用它。