Asp.net 一次执行多条oralce语句

    Asp.net要执行一次执行多条语句,主要是将多条SQl语句组合成oracle可以执行的语句。

    组合方法可以参考网址:http://www.cnblogs.com/scottckt/archive/2009/03/30/1425300.html

    要执行的语句可以是Delete语句、Upadate语句,也可以是Insert语句,但不能是查询语句。

示例代码:

 string sSQl = "delete from HP_SatisfactionItem where sfiID='{0}';";
            sSQl 
= string.Format(sSQl, "ID");
            
string sSubSql = "delete from hp_satisfactionresult where sfrID='{0}';";
            sSubSql 
= string.Format(sSubSql, "ID");

            StringBuilder sbBuilder 
= new StringBuilder();
            sbBuilder.Append(
"begin").Append("\n");
            sbBuilder.Append(sSQl).Append(
"\n");
            sbBuilder.Append(sSubSql).Append(
"\n");
            sbBuilder.Append(
" end;");

            
//执行多条语句
            ExecuteNonSQl(sbBuilder.ToString());

ExecuteNonSQl方法代码:

 /// <summary>
    
/// 执行语句
    
/// </summary>
    
/// <param name="sSqlString"></param>
    
/// <returns></returns>
    public static int ExecuteNonSQl(string sSqlString)
    {
        
int iRetrunCount = 0;
        
try
        {
            sConnectionString 
= ConfigurationManager.AppSettings.Get("UserConnection");
            OracleConnection ocConnection 
= new OracleConnection(sConnectionString);
            OracleCommand ocCommand 
= ocConnection.CreateCommand();
            
//命令类型为存储过程
            ocCommand.CommandType = CommandType.Text;
            ocCommand.CommandText 
= sSqlString;
            ocConnection.Open();
            iRetrunCount 
= ocCommand.ExecuteNonQuery();
            ocCommand.Dispose();
            ocConnection.Close();

        }
        
catch (Exception ex)
        {
            
throw ex;
        }

        
return iRetrunCount;
    }

注:

  例中的Update语句不能放在多行。如:

sSQl = @"update HP_SatisfactionItem set sfiTitle='test',sfiStyle=0,sfiIsDescription='1',
sfiDescriptionTitle
='test',sfiOrder=1,sfiDeptID='D024',sfiNote='11' where sfiID='d6857915-d3ae-4d7f-b908-876d80bce909';
                        
update hp_satisfactionresult set sfrValueType=1,sfrItemLength=10,sfrItemOne='11',sfrItemTwo='22',
sfrItemThree
='33',sfrItemFour='44',sfrItemFive='55',sfrItemSix='66',sfrItemSeven='77',sfrItemEight='88',sfrItemNine='99',sfrItemTen='110' where sfrID='d6857915-d3ae-4d7f-b908-876d80bce909';";

这样在运行时会报下边的错误。

PLS-00103: 出现符号 ""在需要下列之一时:
 begin case declare end
   exception exit for goto if loop mod null pragma raise return
   select update while with <an identifier>
   <a double-quoted delimited-identifier> <a bind variable> <<
   close current delete fetch lock insert open rollback
   savepoint set sql execute commit forall merge
   <a single-quoted SQL string> pipe


原文地址:https://www.cnblogs.com/scottckt/p/1459478.html