123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Data.Common;
- using System.Data.OracleClient;
- using System.Xml;
- using System.Configuration;
- using Ant.Common;
- namespace Ant.Data
- {
- /// <summary>
- /// Oracle数据库访问类
- /// </summary>
- public sealed class OracleDataAccess : BaseDataAccess
- {
- private OracleConnection OracleConnection;
- private OracleTransaction trans;
- private DataAccess da;
- #region DataAccess 成员
- /// <summary>
- /// 构造函数
- /// </summary>
- public OracleDataAccess()
- {
- this.OracleConnection = new OracleConnection();
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="connString">连接数据库字符串</param>
- public OracleDataAccess(string connString)
- {
- this.OracleConnection = new OracleConnection(connString);
- }
- /// <summary>
- ///
- /// </summary>
- public bool ConvertToUppercase { get; set; }
- /// <summary>
- /// 数据库连接字符串
- /// </summary>
- public override string ConnectionString
- {
- get
- {
- if (this.OracleConnection.IsNull())
- return "";
- else
- return this.OracleConnection.ConnectionString;
- }
- set
- {
- this.OracleConnection.ConnectionString = value;
- }
- }
- public override DatabaseType DatabaseType
- {
- get { return DatabaseType.Oracle; }
- }
- /// <summary>
- /// 获取事务的值
- /// </summary>
- public override DbTransaction Trans
- {
- get
- {
- if (this.trans != null)
- return trans;
- else
- return null;
- }
- }
- /// <summary>
- /// 获取数据库是否关闭
- /// </summary>
- public override bool IsClosed
- {
- get
- {
- if (this.OracleConnection.IsNull())
- return true;
- if (this.OracleConnection.State == ConnectionState.Closed)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- /// <summary>
- /// 数据库连接是否不可用
- /// </summary>
- public override bool IsUnEnable
- {
- get
- {
- if (this.OracleConnection == null)
- {
- return true;
- }
- if (this.OracleConnection.ConnectionString.IsEmpty())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- /// <summary>
- /// 打开数据库
- /// </summary>
- public override void Open()
- {
- if (OracleConnection.ConnectionString == null || OracleConnection.ConnectionString == "")
- {
- da = DataAccessFactory.GetWriteDataDefault;
- this.OracleConnection = new OracleConnection(da.ConnectionString);
- }
- if (this.OracleConnection.State == ConnectionState.Closed)
- {
- this.OracleConnection.Open();
- }
- }
- /// <summary>
- /// 关闭数据库
- /// </summary>
- public override void Close()
- {
- if (this.OracleConnection.State != ConnectionState.Closed && trans == null)
- {
- this.OracleConnection.Close();
- this.OracleConnection.Dispose();
- this.OracleConnection = null;
- }
- }
- /// <summary>
- /// 释放资源
- /// </summary>
- public override void Dispose()
- {
- if (this.OracleConnection != null)
- {
- this.OracleConnection.Dispose();
- this.OracleConnection = null;
- }
- if (this.trans != null)
- {
- this.trans.Dispose();
- this.trans = null;
- }
- }
- /// <summary>
- /// 开户数据库事务
- /// </summary>
- public override void BeginTransaction()
- {
- Open();
- if (trans == null)
- trans = this.OracleConnection.BeginTransaction();
- }
- /// <summary>
- /// 提交数据库事务
- /// </summary>
- public override void Commit()
- {
- try
- {
- if (null != this.trans)
- {
- this.trans.Commit();
- this.trans.Dispose();
- trans = null;
- }
- }
- finally
- {
- this.Close();
- }
- }
- /// <summary>
- /// 回滚数据库事务
- /// </summary>
- public override void RollBack()
- {
- try
- {
- if (trans != null)
- {
- this.trans.Rollback();
- this.trans.Dispose();
- trans = null;
- }
- }
- finally
- {
- this.Close();
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="commandText"></param>
- /// <param name="commandType"></param>
- /// <param name="Parameters"></param>
- /// <returns></returns>
- public override int ExecuteNonQuery(string commandText, CommandType commandType, QueryParameterCollection Parameters)
- {
- try
- {
- this.Open();
- OracleCommand cmd = this.OracleConnection.CreateCommand();
- cmd.CommandText = commandText;
- cmd.CommandType = commandType;
- if (trans != null)
- {
- cmd.Transaction = trans;
- }
- if (Parameters != null)
- {
- foreach (QueryParameter p in Parameters)
- {
- cmd.Parameters.Add(ToOracleParameter(p));
- }
- }
- return cmd.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- return -1;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="commandText"></param>
- /// <param name="commandType"></param>
- /// <param name="Parameters"></param>
- /// <returns></returns>
- public override DbDataReader ExecuteDataReader(string commandText, CommandType commandType, QueryParameterCollection Parameters)
- {
- try
- {
- this.Open();
- OracleCommand cmd = this.OracleConnection.CreateCommand();
- cmd.CommandText = commandText;
- cmd.CommandType = commandType;
- if (trans != null)
- {
- cmd.Transaction = trans;
- }
- if (Parameters != null)
- {
- foreach (QueryParameter p in Parameters)
- {
- cmd.Parameters.Add(ToOracleParameter(p));
- }
- }
- return cmd.ExecuteReader();
- }
- catch (Exception ex)
- {
- return null;
- }
- }
- #region ExecuteDataTable
- /// <summary>
- ///
- /// </summary>
- /// <param name="commandText"></param>
- /// <param name="commandType"></param>
- /// <param name="Parameters"></param>
- /// <param name="tableName"></param>
- /// <param name="startRecord"></param>
- /// <param name="maxRecords"></param>
- /// <returns></returns>
- public override DataTable ExecuteDataTable(string commandText, CommandType commandType, QueryParameterCollection Parameters, string tableName, int startRecord, int maxRecords)
- {
- try
- {
- this.Open();
- OracleCommand cmd = this.OracleConnection.CreateCommand();
- cmd.CommandText = commandText;
- cmd.CommandType = commandType;
- if (trans != null)
- {
- cmd.Transaction = trans;
- }
- if (Parameters != null)
- {
- foreach (QueryParameter p in Parameters)
- {
- cmd.Parameters.Add(ToOracleParameter(p));
- }
- }
- OracleDataAdapter DataAdapter = new OracleDataAdapter(cmd);
- DataTable dt = new DataTable();
- DataAdapter.Fill(startRecord, maxRecords, dt);
- if (tableName != null)
- {
- dt.TableName = tableName;
- }
- return dt;
- }
- catch (Exception ex)
- {
- return null;
- }
- finally
- {
- this.Close();
- }
- }
- #endregion
- #region ExecuteDataSet
- /// <summary>
- /// 返回DataSet数据集
- /// </summary>
- /// <param name="commandText"></param>
- /// <param name="commandType"></param>
- /// <param name="Parameters"></param>
- /// <param name="tableName"></param>
- /// <param name="startRecord"></param>
- /// <param name="maxRecords"></param>
- /// <returns></returns>
- public override DataSet ExecuteDataSet(string commandText, CommandType commandType, QueryParameterCollection Parameters, string tableName, int startRecord, int maxRecords)
- {
- try
- {
- DataSet ds = new DataSet();
- OracleDataAdapter sda = new OracleDataAdapter();
- this.Open();
- OracleCommand cmd = this.OracleConnection.CreateCommand();
- cmd.CommandText = commandText;
- cmd.CommandType = commandType;
- cmd.CommandTimeout = 60;
- if (trans != null)
- {
- cmd.Transaction = trans;
- }
- foreach (QueryParameter q in Parameters)
- {
- cmd.Parameters.Add(ToOracleParameter(q));
- }
- sda.SelectCommand = cmd;
- sda.Fill(ds, startRecord, maxRecords, tableName);
- //foreach (SqlParameter p in sda.SelectCommand.Parameters)
- //{
- // Parameters[p.ParameterName] = ToQueryParameter(p);
- //}
- return ds;
- }
- finally
- {
- this.Close();
- }
- }
- #endregion
- #region object ExecuteScalar
- public override object ExecuteScalar(string commandText, CommandType commandType, QueryParameterCollection parameters)
- {
- try
- {
- this.Open();
- OracleCommand cmd = this.OracleConnection.CreateCommand();
- cmd.CommandText = commandText;
- cmd.CommandType = commandType;
- if (trans != null)
- {
- cmd.Transaction = trans;
- }
- foreach (QueryParameter p in parameters)
- {
- cmd.Parameters.Add(ToOracleParameter(p));
- }
- return cmd.ExecuteScalar();
- }
- catch (Exception ex)
- {
- return null;
- }
- finally
- {
- this.Close();
- }
- }
- #endregion
- #endregion
- #region private method
- private OracleParameter ToOracleParameter(QueryParameter parameter)
- {
- OracleParameter oracleParameter = new OracleParameter();
- oracleParameter.DbType = parameter.DbType;
- oracleParameter.Direction = parameter.Direction;
- oracleParameter.IsNullable = parameter.IsNullable;
- oracleParameter.ParameterName = parameter.ParameterName;
- oracleParameter.Size = parameter.Size;
- oracleParameter.SourceColumn = parameter.SourceColumn;
- oracleParameter.SourceVersion = parameter.SourceVersion;
- oracleParameter.Value = parameter.Value;
- return oracleParameter;
- }
- private QueryParameter ToQueryParameter(OracleParameter parameter)
- {
- QueryParameter queryParameter = new QueryParameter();
- queryParameter.DbType = parameter.DbType;
- queryParameter.Direction = parameter.Direction;
- queryParameter.IsNullable = parameter.IsNullable;
- queryParameter.ParameterName = parameter.ParameterName;
- queryParameter.Value = parameter.Value;
- return queryParameter;
- }
- #endregion
- public override bool ValidateSQL(string sql)
- {
- throw new NotImplementedException();
- }
- public override DataView ExecuteDataView(string commandText, CommandType commandType, QueryParameterCollection Parameters, string tableName, int startRecord, int maxRecords)
- {
- throw new NotImplementedException();
- }
- public override DbDataReader ExecuteReader(string cmdText, DbParam[] parameters, CommandType cmdType)
- {
- throw new NotImplementedException();
- }
- public override int ExecuteNonQuery(string commandText, DbParam[] parameters, CommandType commandType)
- {
- throw new NotImplementedException();
- }
- public override object ExecuteScalar(string commandText, DbParam[] parameters, CommandType commandType)
- {
- throw new NotImplementedException();
- }
- }
- }
|