-1

我在这里慢慢发疯了……为什么这种和平的 SQL 代码在 SQL Developer 中工作而不是在 C# 中工作?

declare x integer;
begin
insert into aspa2_counters (shortname, description, devicegroupid) values ('TEST0', 'DESC0', 61) returning counterid into x;
INSERT ALL  
INTO aspa2_priceclass (from_num, to_num, price, fk_counterid) VALUES (0,100,0.22,x) 
INTO aspa2_priceclass (from_num, to_num, price, fk_counterid) VALUES (101,200,0.23,x) 
SELECT * FROM dual; 
end;

在代码中动态生成多个插入。请帮助...我在这个上花了两天时间,它正在减慢项目的速度。如果需要,请确实需要更多信息。

谢谢!

编辑:我忘了写下一个错误:

ORA-06550: line 1, column 19:
PLS-00103: Encountered the symbol "" when expecting one of the following:

   begin function package pragma procedure subtype type use
   <an identifier> <a double-quoted delimited-identifier> form
   current cursor
4

2 回答 2

2

好的,我终于解决了这个问题。

看起来 Oracle 的 odp.net 对换行符(“\r\n”和“\n”)非常敏感,因此请确保不要将 @ 用于多行字符串,或者尝试使用 .replace() 方法替换“\r \n" 与 "\n"。

这就是它最终的样子:

string anonymousBlock = "declare x integer;" +
                        "begin " +
                        "insert into aspa2_counters (shortname, description, devicegroupid) values (:p1, :p2, :p3) returning counterid into x;" +
                          buildInsert.ToString() + 
                        " end;";
于 2012-02-17T10:11:48.860 回答
0

这是硬编码的部分:

string query = @"declare x integer;
                      begin
                      insert into aspa2_counters (shortname, description, devicegroupid) values ('TEST', 'OPIS', 61) returning counterid into x;
                      INSERT ALL  
                          INTO aspa2_priceclass (from_num, to_num, price, fk_counterid) VALUES (0,100,0.22,x) 
                          INTO aspa2_priceclass (from_num, to_num, price, fk_counterid) VALUES (101,200,0.23,x) 
                      SELECT * FROM dual; 
                      end;";

和代码 Oracle 命令代码:

            using (OracleCommand cmnd = new OracleCommand(cmndText, con))
            {
                if (parameters != null)
                {
                    foreach (OracleParameter p in parameters)
                    {
                        cmnd.Parameters.Add(p);
                    }
                }

                try
                {
                    cmnd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {

                }
            }
于 2012-02-17T08:36:31.307 回答