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
{
///
/// Oracle数据库访问类
///
public sealed class OracleDataAccess : BaseDataAccess
{
private OracleConnection OracleConnection;
private OracleTransaction trans;
private DataAccess da;
#region DataAccess 成员
///
/// 构造函数
///
public OracleDataAccess()
{
this.OracleConnection = new OracleConnection();
}
///
/// 构造函数
///
/// 连接数据库字符串
public OracleDataAccess(string connString)
{
this.OracleConnection = new OracleConnection(connString);
}
///
///
///
public bool ConvertToUppercase { get; set; }
///
/// 数据库连接字符串
///
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; }
}
///
/// 获取事务的值
///
public override DbTransaction Trans
{
get
{
if (this.trans != null)
return trans;
else
return null;
}
}
///
/// 获取数据库是否关闭
///
public override bool IsClosed
{
get
{
if (this.OracleConnection.IsNull())
return true;
if (this.OracleConnection.State == ConnectionState.Closed)
{
return true;
}
else
{
return false;
}
}
}
///
/// 数据库连接是否不可用
///
public override bool IsUnEnable
{
get
{
if (this.OracleConnection == null)
{
return true;
}
if (this.OracleConnection.ConnectionString.IsEmpty())
{
return true;
}
else
{
return false;
}
}
}
///
/// 打开数据库
///
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();
}
}
///
/// 关闭数据库
///
public override void Close()
{
if (this.OracleConnection.State != ConnectionState.Closed && trans == null)
{
this.OracleConnection.Close();
this.OracleConnection.Dispose();
this.OracleConnection = null;
}
}
///
/// 释放资源
///
public override void Dispose()
{
if (this.OracleConnection != null)
{
this.OracleConnection.Dispose();
this.OracleConnection = null;
}
if (this.trans != null)
{
this.trans.Dispose();
this.trans = null;
}
}
///
/// 开户数据库事务
///
public override void BeginTransaction()
{
Open();
if (trans == null)
trans = this.OracleConnection.BeginTransaction();
}
///
/// 提交数据库事务
///
public override void Commit()
{
try
{
if (null != this.trans)
{
this.trans.Commit();
this.trans.Dispose();
trans = null;
}
}
finally
{
this.Close();
}
}
///
/// 回滚数据库事务
///
public override void RollBack()
{
try
{
if (trans != null)
{
this.trans.Rollback();
this.trans.Dispose();
trans = null;
}
}
finally
{
this.Close();
}
}
///
///
///
///
///
///
///
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;
}
}
///
///
///
///
///
///
///
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
///
///
///
///
///
///
///
///
///
///
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
///
/// 返回DataSet数据集
///
///
///
///
///
///
///
///
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();
}
}
}