首页 > 开发 > 综合 > 正文

发布一个操作oracle的c#类

2024-07-21 02:29:45
字体:
来源:转载
供稿:网友

商业源码热门下载www.html.org.cn

/***********************************************************************
 * module:  orahelper.cs
 * author:  hellopj
 * purpose: definition of the class orahelper
 ***********************************************************************/

using system;
using system.data;
using oracle.dataaccess.client;
using oracle.dataaccess.types;
using system.collections;
using system.configuration;
namespace orahelper
{
 public abstract class orahelper
 {
   public static readonly string conn_string_non_dtc = configurationsettings.appsettings["connstr"].trim();
  public static oracleconnection conn = new oracleconnection(conn_string_non_dtc);

  public static void openconnection()
  {
   if (conn.state != connectionstate.open)
    conn.open();
  }
  public static void closeconnection()
  {
   if (conn.state == connectionstate.open)
    conn.close();
  }
  public static dataset getdataset(string cmdtext)
  {
   
   oracledataadapter adapter = new oracledataadapter(cmdtext,conn);
   dataset data = new dataset();
   adapter.fill(data);
   return data;
  }
  public static oracledatareader executereader(string cmdtext)
  {
   
   oraclecommand cmd = new oraclecommand();
   cmd.connection=conn;
   cmd.commandtext=cmdtext;
   oracledatareader rdr = cmd.executereader();
   cmd.parameters.clear();
   return rdr;
  }
  public static int executenonquery(string cmdtext)
  {
   try
   {
    oraclecommand comm=new oraclecommand(cmdtext,conn);
    comm.executenonquery();
    return 1;
   }
   catch
   {
    return 0;
   }
  }
  public static void executenonquery(string cmdtext,oracleconnection conn)
  {
   try
   {
    oraclecommand comm=new oraclecommand(cmdtext,conn);
    comm.executenonquery();
   }
   catch
   {
    
   }
  }
  public static string executescalar(string cmdtext)
  {
   string status="";
   try
   {
    oraclecommand comm=new oraclecommand(cmdtext,conn);
    status=comm.executescalar().tostring();
    return status;
   }
   catch(exception err)
   {
    //return "-1";
    return err.message.tostring();
    
   }
  }
  private static hashtable parmcache = hashtable.synchronized(new hashtable());
  public static int executenonquery(commandtype cmdtype, string cmdtext, params oracleparameter[] cmdparms)
  {
   oraclecommand cmd = new oraclecommand();
   cmd.connection = conn;
   cmd.parameters.clear();
   cmd.commandtext=cmdtext;
   foreach (oracleparameter parm in cmdparms)
    cmd.parameters.add(parm);
   int val=cmd.executenonquery();
   cmd.parameters.clear();
   return val;
  }

  public static int executenonquery(oracletransaction trans, commandtype cmdtype, string cmdtext, params oracleparameter[] cmdparms)
  {
   oraclecommand cmd = new oraclecommand();
   preparecommand(cmd, trans.connection, trans, cmdtype, cmdtext, cmdparms);
   int val = cmd.executenonquery();
   cmd.parameters.clear();
   return val;
  }

  
  public static oracledatareader executereader(commandtype cmdtype, string cmdtext, params oracleparameter[] cmdparms)
  {
   
   oraclecommand cmd = new oraclecommand();
   try
   {
    preparecommand(cmd, conn, null, cmdtype, cmdtext, cmdparms);
    oracledatareader rdr = cmd.executereader(commandbehavior.closeconnection);
    cmd.parameters.clear();
    return rdr;
   
   }
   catch (exception e)
   {
    conn.close();
    throw e;
   } 
  }
  
 
  public static object executescalar(string connstring, commandtype cmdtype, string cmdtext, params oracleparameter[] cmdparms)
  {
   oraclecommand cmd = new oraclecommand();
   preparecommand(cmd, conn, null, cmdtype, cmdtext, cmdparms);
   object val = cmd.executescalar();
   cmd.parameters.clear();
   return val;
  }

  
  public static object executescalar(commandtype cmdtype, string cmdtext, params oracleparameter[] cmdparms)
  {
   
   oraclecommand cmd = new oraclecommand();
   preparecommand(cmd, conn, null, cmdtype, cmdtext, cmdparms);
   object val = cmd.executescalar();
   cmd.parameters.clear();
   return val;
  }
  
  public static int getscalar(commandtype cmdtype, string cmdtext, params oracleparameter[] cmdparms)
  {
   
   oraclecommand cmd = new oraclecommand();
   preparecommand(cmd, conn, null, cmdtype, cmdtext, cmdparms);
   object val = cmd.executescalar();
   cmd.parameters.clear();
   int nval  = convert.toint32(val);
   return nval;
   
  }

  
  
  public static void cacheparameters(string cachekey, params oracleparameter[] cmdparms)
  {
   parmcache[cachekey] = cmdparms;
  }

  
  public static oracleparameter[] getcachedparameters(string cachekey)
  {
   oracleparameter[] cachedparms = (oracleparameter[])parmcache[cachekey];
   
   if (cachedparms == null)
    return null;
   
   
   oracleparameter[] clonedparms = new oracleparameter[cachedparms.length];

   
   for (int i = 0, j = cachedparms.length; i < j; i++)
    clonedparms[i] = (oracleparameter)((icloneable)cachedparms[i]).clone();

   return clonedparms;
  }

  
  private static void preparecommand(oraclecommand cmd, oracleconnection conn, oracletransaction trans, commandtype cmdtype, string cmdtext, oracleparameter[] cmdparms)
  {
   
   if (conn.state != connectionstate.open)
    conn.open();
   cmd.connection = conn;
   cmd.commandtext = cmdtext;
   cmd.commandtype = cmdtype;
   //if (trans != null)
   //cmd.transaction = trans;
   if (cmdparms != null)
   {
    foreach (oracleparameter parm in cmdparms)
     cmd.parameters.add(parm);
   }
  }

  public static int getid(string seqname) //得到序列的nextval
  {
   oraclecommand cmd = new oraclecommand();
   using (oracleconnection conn = new oracleconnection(conn_string_non_dtc))
   {
    string sqltext="select "+seqname+".nextval from dual";
    
    preparecommand(cmd, conn, null, commandtype.text,sqltext,null);
    int val = convert.toint32(cmd.executescalar());
    cmd.parameters.clear();
    return val;
   }
  }
   }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表