zoukankan      html  css  js  c++  java
  • 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


  • 相关阅读:
    Android之上下文context
    如果简单的记录,就可以为这个世界创造更多的财富,那么还有什么理由不去写博客呢? — 读<<黑客与画家>> 有感
    【最新最全】为 iOS 和 Android 的真机和模拟器编译 Luajit 库
    【Graphql实践】使用 Apollo(iOS) 访问 Github 的 Graphql API
    【最新】LuaJIT 32/64 位字节码,从编译到使用全纪录
    简陋的swift carthage copy-frameworks 辅助脚本
    【自问自答】关于 Swift 的几个疑问
    【读书笔记】The Swift Programming Language (Swift 4.0.3)
    【读书笔记】A Swift Tour
    【趣味连载】攻城狮上传视频与普通人上传视频:(一)生成结构化数据
  • 原文地址:https://www.cnblogs.com/scottckt/p/1459478.html
Copyright © 2011-2022 走看看