ITPub博客

首页 > 数据库 > Oracle > c#连接oracle 的一个通用类(转)

c#连接oracle 的一个通用类(转)

Oracle 作者:s2015 时间:2011-05-13 11:27:42 0 删除 编辑
using System;
using System.Data;
using System.Data.OracleClient;
using System.Web.UI.WebControls; namespace SiFenManager.Util
{
///
/// 数据库通用操作类
///

public class Database
{
protected  OracleConnection con;//连接对象

public Database()
{
con
=new OracleConnection(DafangFramework.AppConfig.DataBaseConnectionString);
}
public Database(string constr)
{
con
=new OracleConnection(constr);
}
#region 打开数据库连接
///
/// 打开数据库连接
///

  private  void Open()
{
//打开数据库连接
   if(con.State==ConnectionState.Closed)
{
try
{
//打开数据库连接
     con.Open();
}
catch(Exception e)
{
throw e;
}
}
}
#endregion
#region 关闭数据库连接
///
/// 关闭数据库连接
///

  private  void Close()
{
//判断连接的状态是否已经打开
   if(con.State==ConnectionState.Open)
{
con.Close();
}
}
#endregion
#region 执行查询语句,返回OracleDataReader ( 注意:调用该方法后,一定要对OracleDataReader进行Close )
///
/// 执行查询语句,返回OracleDataReader ( 注意:调用该方法后,一定要对OracleDataReader进行Close )
///

/// 查询语句
/// OracleDataReader  
  public  OracleDataReader ExecuteReader(string sql)
{
OracleDataReader myReader;
Open();
OracleCommand cmd
= new OracleCommand(sql, con);
myReader
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
#endregion
#region 执行带参数的SQL语句  
///
/// 执行SQL语句,返回影响的记录数
///

/// SQL语句
/// 影响的记录数  
  public  int ExecuteSql(string sql, params OracleParameter[] cmdParms)
{
OracleCommand cmd
= new OracleCommand();
{
try
{
PrepareCommand(cmd, con,
null, sql, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (System.Data.OracleClient.OracleException e)
{
throw e;
}
}
}
#endregion

#region 执行带参数的SQL语句   
/// 
/// 执行不带参数的SQL语句
/// 
/// SQL语句     
  public  void ExecuteSql(string sql)
{
OracleCommand cmd = new OracleCommand(sql,con);
try
{
Open();
cmd.ExecuteNonQuery();
Close();
}
catch (System.Data.OracleClient.OracleException e)
{
Close();
throw e;
}
}
#endregion
#region 执行SQL语句,返回数据到DataSet中
/// 
/// 执行SQL语句,返回数据到DataSet中
/// 
/// sql语句
/// 返回DataSet
  public  DataSet GetDataSet(string sql)
{
DataSet ds=new DataSet();
try
{
Open();//打开数据连接
    OracleDataAdapter adapter=new OracleDataAdapter(sql,con);
adapter.Fill(ds);
}
catch//(Exception ex)
   {
}
finally
{
Close();//关闭数据库连接
   }
return ds;
}
#endregion
#region 执行SQL语句,返回数据到自定义DataSet中
/// 
/// 执行SQL语句,返回数据到DataSet中
/// 
/// sql语句
/// 自定义返回的DataSet表名
/// 返回DataSet
  public  DataSet GetDataSet(string sql,string DataSetName)
{
DataSet ds=new DataSet();
Open();//打开数据连接
   OracleDataAdapter adapter=new OracleDataAdapter(sql,con);
adapter.Fill(ds,DataSetName);
Close();//关闭数据库连接
   return ds;
}
#endregion
#region 执行Sql语句,返回带分页功能的自定义dataset
/// 
/// 执行Sql语句,返回带分页功能的自定义dataset
/// 
/// Sql语句
/// 每页显示记录数
/// 当前页
/// 返回dataset表名
/// 返回DataSet
  public  DataSet GetDataSet(string sql,int PageSize,int CurrPageIndex,string DataSetName)
{
DataSet ds=new DataSet();
Open();//打开数据连接
   OracleDataAdapter adapter=new OracleDataAdapter(sql,con);
adapter.Fill(ds,PageSize * (CurrPageIndex - 1), PageSize,DataSetName);
Close();//关闭数据库连接
   return ds;
}
#endregion
#region 执行SQL语句,返回记录总数
/// 
/// 执行SQL语句,返回记录总数
/// 
/// sql语句
/// 返回记录总条数
  public  int GetRecordCount(string sql)
{
int recordCount = 0;
Open();//打开数据连接
   OracleCommand command = new OracleCommand(sql,con);
OracleDataReader dataReader = command.ExecuteReader();
while(dataReader.Read())
{
recordCount++;
}
dataReader.Close();
Close();//关闭数据库连接
   return recordCount;
}
#endregion

#region 统计某表记录总数 
/// 
/// 统计某表记录总数
/// 
/// 主键/索引键
/// 数据库.用户名.表名
/// 查询条件
/// 返回记录总数 
  public  int GetRecordCount(string keyField, string tableName, string condition)
{
int RecordCount = 0;
string sql = "select count(" + keyField + ") as count from " + tableName + " " + condition;
DataSet ds = GetDataSet(sql);
if (ds.Tables[0].Rows.Count > 0)
{
RecordCount =Convert.ToInt32(ds.Tables[0].Rows[0][0]);
}
ds.Clear();
ds.Dispose();
return RecordCount;
}
/// 
/// 统计某表记录总数
/// 
/// 可重复的字段
/// 数据库.用户名.表名
/// 查询条件
/// 字段是否主键
/// 返回记录总数 
  public  int GetRecordCount(string Field, string tableName, string condition, bool flag)
{
int RecordCount = 0;
if (flag)
{
RecordCount = GetRecordCount(Field, tableName, condition);
}
else
{
string sql = "select count(distinct(" + Field + ")) as count from " + tableName + " " + condition;
DataSet ds = GetDataSet(sql);
if (ds.Tables[0].Rows.Count > 0)
{
RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
}
ds.Clear();
ds.Dispose();
}
return RecordCount;
}
#endregion
#region 统计某表分页总数 
/// 
/// 统计某表分页总数
/// 
/// 主键/索引键
/// 表名
/// 查询条件
/// 页宽
/// 记录总数
/// 返回分页总数 
  public  int GetPageCount(string keyField, string tableName, string condition, int pageSize, int RecordCount)
{
int PageCount = 0;
PageCount = (RecordCount % pageSize) > 0 ? (RecordCount / pageSize) + 1 : RecordCount / pageSize;
if (PageCount < 1) PageCount = 1;
return PageCount;
}
/// 
/// 统计某表分页总数
/// 
/// 主键/索引键
/// 表名
/// 查询条件
/// 页宽
/// 返回页面总数 
  public  int GetPageCount(string keyField, string tableName, string condition, int pageSize, ref int RecordCount)
{
RecordCount = GetRecordCount(keyField, tableName, condition);
return GetPageCount(keyField, tableName, condition, pageSize, RecordCount);
}
/// 
/// 统计某表分页总数
/// 
/// 可重复的字段
/// 表名
/// 查询条件
/// 页宽
/// 是否主键
/// 返回页页总数 
  public  int GetPageCount(string Field, string tableName, string condition, ref int RecordCount, int pageSize, bool flag)
{
RecordCount = GetRecordCount(Field, tableName, condition, flag);
return GetPageCount(Field, tableName, condition, pageSize, ref RecordCount);
}
#endregion 

#region Sql分页函数 
/// 
/// 构造分页查询SQL语句
/// 
/// 主键
/// 所有需要查询的字段(field1,field2...)
/// 库名.拥有者.表名
/// 查询条件1(where ...)
/// 排序条件2(order by ...)
/// 当前页号
/// 页宽
/// SQL语句 
  public  string JoinPageSQL(string KeyField, string FieldStr, string TableName, string Where, string Order, int CurrentPage, int PageSize)
{
string sql = null;
if (CurrentPage == 1)
{
sql = "select  " + CurrentPage * PageSize + " " + FieldStr + " from " + TableName + " " + Where + " " + Order + " ";
}
else
{
sql = "select * from (";
sql += "select  " + CurrentPage * PageSize + " " + FieldStr + " from " + TableName + " " + Where + " " + Order + ") a ";
sql += "where " + KeyField + " not in (";
sql += "select  " + (CurrentPage - 1) * PageSize + " " + KeyField + " from " + TableName + " " + Where + " " + Order + ")";
}
return sql;
}
/// 
/// 构造分页查询SQL语句
/// 
/// 字段名(非主键)
/// 库名.拥有者.表名
/// 查询条件1(where ...)
/// 排序条件2(order by ...)
/// 当前页号
/// 页宽
/// SQL语句 
  public  string JoinPageSQL(string Field, string TableName,string Where, string Order, int CurrentPage, int PageSize)
{
string sql = null;
if (CurrentPage == 1)
{
sql = "select rownum " + CurrentPage * PageSize + " " + Field + " from " + TableName + " " + Where + " " + Order + " group by " + Field;
}
else
{
sql = "select * from (";
sql += "select rownum " + CurrentPage * PageSize + " " + Field + " from " + TableName + " " + Where + " " + Order + " group by " + Field + " ) a ";
sql += "where " + Field + " not in (";
sql += "select rownum " + (CurrentPage - 1) * PageSize + " " + Field + " from " + TableName + " " + Where + " " + Order + " group by " + Field + ")";
}
return sql;
}
#endregion
#region 根据系统时间动态生成各种查询语句(现已经注释掉,以备以后使用)
//  #region 根据查询时间的条件,动态生成查询语句
//  /// 
//  /// 根据查询时间的条件,动态生成查询语句
//  /// 
//  /// 开始时间
//  /// 结束时间
//  /// 单位
//  /// 开始线损率
//  /// 结束线损率
//  /// 单位字段
//  /// 线损率字段
//  /// 表后缀
//  /// SQL语句
//  public  string SQL(DateTime starttime,DateTime endtime,string dw,float startxsl,float endxsl,string danwei,string xiansunlv,string tablehz)
//  {
//
//   string sql=null;
//   //将输入的时间格式转换成固定的格式"yyyy-mm-dd"
//   string zstarttime=starttime.GetDateTimeFormats('D')[1].ToString();
//   string zendtime=endtime.GetDateTimeFormats('D')[1].ToString();
//   string nTime=DateTime.Now.GetDateTimeFormats('D')[1].ToString();
//
//
//   //取日期值的前六位,及年月值
//   string sTime=zstarttime.Substring(0,4)+zstarttime.Substring(5,2);
//   string eTime=zendtime.Substring(0,4)+zendtime.Substring(5,2);
//   string nowTime=nTime.Substring(0,4)+nTime.Substring(5,2);
//   //分别取日期的年和月
//   int sy=Convert.ToInt32(zstarttime.Substring(0,4));
//   int ey=Convert.ToInt32(zendtime.Substring(0,4));
//   int sm=Convert.ToInt32(zstarttime.Substring(5,2));
//   int em=Convert.ToInt32(zendtime.Substring(5,2));
//   //相关变量定义
//   int s;
//   int e;
//   int i;
//   int j;
//   int js;
//   int nz;
//   string x;
//   //一,取当前表生成SQL语句
//   if(sTime==nowTime&&eTime==nowTime)
//   {
//    sql="select  * from "+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" ";
//   }
//    //二,取当前表和其他表生成SQL语句
//   else if(sTime==nowTime&&eTime!=nowTime)
//   {
//    sql="select  * from "+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" "+"union"+" ";
//    //如果年份相等
//    if(sy==ey)
//    {
//     s=Convert.ToInt32(sTime);
//     e=Convert.ToInt32(eTime);
//     for(i=s+1;i//     {
//      i=i++;
//      sql+="select  * from "+i.ToString()+'_'+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" "+"union"+" ";
//     }
//     sql+="select  * from "+e.ToString()+'_'+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" ";
//    }
//     //结束年份大于开始年份
//    else
//    {
//     //1,先循环到起始时间和起始时间的12月
//     s=Convert.ToInt32(sTime);
//     x=zstarttime.Substring(0,4)+"12";
//     nz=Convert.ToInt32(x);
//     for(i=s+1;i<=nz;i++)
//     {
//      i=i++;
//      sql+="select  * from "+i.ToString()+'_'+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" "+"union"+" ";
//     }
//     //2,循环两者相差年份
//     for(i=sy+1;i//     {
//
//      for(j=1;j<=12;j++)
//      {
//       if(j<10)
//       {
//        sql+="select  * from "+i.ToString()+"0"+j.ToString()+'_'+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" "+"union"+" ";
//       }
//       else
//       {
//        sql+="select  * from "+i.ToString()+j.ToString()+'_'+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" "+"union"+" ";
//       }
//      }
//     }
//     //3,循环到结束的月份
//     js=Convert.ToInt32(zendtime.Substring(0,4)+"00");
//     for(i=js;i//     {
//      i++;
//      sql+="select  * from "+i.ToString()+'_'+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" "+"union"+" ";
//
//     }
//     sql+="select  * from "+eTime.ToString()+'_'+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" ";
//
//    }
//   }
//    //三,取其他表生成生成SQL语句
//   else
//   {
//    //1,先循环到起始时间和起始时间的12月
//    s=Convert.ToInt32(sTime);
//    x=zstarttime.Substring(0,4)+"12";
//    nz=Convert.ToInt32(x);
//    for(i=s;i<=nz;i++)
//    {
//     i=i++;
//     sql+="select  * from "+i.ToString()+'_'+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" "+"union"+" ";
//    }
//    //2,循环两者相差年份
//    for(i=sy+1;i//    {
//
//     for(j=1;j<=12;j++)
//     {
//      if(j<10)
//      {
//       sql+="select  * from "+i.ToString()+"0"+j.ToString()+'_'+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" "+"union"+" ";
//      }
//      else
//      {
//       sql+="select  * from "+i.ToString()+j.ToString()+'_'+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" "+"union"+" ";
//      }
//     }
//    }
//    //3,循环到结束的月份
//    js=Convert.ToInt32(zendtime.Substring(0,4)+"00");
//    for(i=js;i//    {
//     i++;
//     sql+="select  * from "+i.ToString()+'_'+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" "+"union"+" ";
//
//    }
//    sql+="select  * from "+eTime.ToString()+'_'+tablehz+" "+"where"+" "+danwei+"="+dw+" "+" "+"and"+" "+xiansunlv+">="+startxsl+" "+"and"+" "+xiansunlv+"<="+endxsl+" ";
//
//   }
//   return sql;
//  }
//  #endregion

//
//  #region 根据查询时间的条件,动态生成查询语句
//  /// 
//  /// 根据查询时间的条件,动态生成查询语句
//  /// 
//  /// 开始时间
//  /// 结束时间
//  /// 查询字段
//  /// 查询条件
//  /// 表后缀
//  /// SQL语句
//  public  string SQL(DateTime starttime,DateTime endtime,string zhiduan,string tiaojiao,string tablehz)
//  {
//
//   string sql=null;
//   //将输入的时间格式转换成固定的格式"yyyy-mm-dd"
//   string zstarttime=starttime.GetDateTimeFormats('D')[1].ToString();
//   string zendtime=endtime.GetDateTimeFormats('D')[1].ToString();
//   string nTime=DateTime.Now.GetDateTimeFormats('D')[1].ToString();
//
//
//   //取日期值的前六位,及年月值
//   string sTime=zstarttime.Substring(0,4)+zstarttime.Substring(5,2);
//   string eTime=zendtime.Substring(0,4)+zendtime.Substring(5,2);
//   string nowTime=nTime.Substring(0,4)+nTime.Substring(5,2);
//   //分别取日期的年和月
//   int sy=Convert.ToInt32(zstarttime.Substring(0,4));
//   int ey=Convert.ToInt32(zendtime.Substring(0,4));
//   int sm=Convert.ToInt32(zstarttime.Substring(5,2));
//   int em=Convert.ToInt32(zendtime.Substring(5,2));
//   //相关变量定义
//   int s;
//   int e;
//   int i;
//   int j;
//   int js;
//   int nz;
//   string x;
//   //一,取当前表生成SQL语句
//   if(sTime==nowTime&&eTime==nowTime)
//   {
//    sql="select"+" "+zhiduan+" "+"from"+" "+tablehz+" "+"where"+" "+tiaojiao+" ";
//
//   }
//    //二,取当前表和其他表生成SQL语句
//   else if(sTime==nowTime&&eTime!=nowTime)
//   {
//    sql="select"+" "+zhiduan+" "+"from"+" "+tablehz+" "+"where"+" "+tiaojiao+" "+"union"+" ";
//
//    //如果年份相等
//    if(sy==ey)
//    {
//     s=Convert.ToInt32(sTime);
//     e=Convert.ToInt32(eTime);
//     for(i=s+1;i//     {
//      i=i++;
//      sql+="select"+" "+zhiduan+" "+"from"+" "+i.ToString()+'_'+tablehz+" "+"where"+" "+tiaojiao+" "+"union"+" ";
//
//     }
//     sql+="select"+" "+zhiduan+" "+"from"+" "+e.ToString()+'_'+tablehz+" "+"where"+" "+tiaojiao+" ";
//
//    }
//     //结束年份大于开始年份
//    else
//    {
//     //1,先循环到起始时间和起始时间的12月
//     s=Convert.ToInt32(sTime);
//     x=zstarttime.Substring(0,4)+"12";
//     nz=Convert.ToInt32(x);
//     for(i=s+1;i<=nz;i++)
//     {
//      i=i++;
//      sql+="select"+" "+zhiduan+" "+"from"+" "+i.ToString()+'_'+tablehz+" "+"where"+" "+tiaojiao+" "+"union"+" ";
//
//     }
//     //2,循环两者相差年份
//     for(i=sy+1;i//     {
//
//      for(j=1;j<=12;j++)
//      {
//       if(j<10)
//       {
//        sql+="select"+" "+zhiduan+" "+"from"+" "+i.ToString()+"0"+j.ToString()+'_'+tablehz+" "+"where"+" "+tiaojiao+" "+"union"+" ";
//
//       }
//       else
//       {
//        sql+="select"+" "+zhiduan+" "+"from"+" "+i.ToString()+j.ToString()+'_'+tablehz+" "+"where"+" "+tiaojiao+" "+"union"+" ";
//       }
//      }
//     }
//     //3,循环到结束的月份
//     js=Convert.ToInt32(zendtime.Substring(0,4)+"00");
//     for(i=js;i//     {
//      i++;
//      sql+="select"+" "+zhiduan+" "+"from"+" "+i.ToString()+'_'+tablehz+" "+"where"+" "+tiaojiao+" "+"union"+" ";
//
//     }
//     sql+="select"+" "+zhiduan+" "+"from"+" "+eTime.ToString()+'_'+tablehz+" "+"where"+" "+tiaojiao+" ";
//
//    }
//   }
//    //三,取其他表生成生成SQL语句
//   else
//   {
//    //1,先循环到起始时间和起始时间的12月
//    s=Convert.ToInt32(sTime);
//    x=zstarttime.Substring(0,4)+"12";
//    nz=Convert.ToInt32(x);
//    for(i=s;i<=nz;i++)
//    {
//     i=i++;
//     sql+="select"+" "+zhiduan+" "+"from"+" "+i.ToString()+'_'+tablehz+" "+"where"+" "+tiaojiao+" "+"union"+" ";
//
//    }
//    //2,循环两者相差年份
//    for(i=sy+1;i//    {
//
//     for(j=1;j<=12;j++)
//     {
//      if(j<10)
//      {
//       sql+="select"+" "+zhiduan+" "+"from"+" "+i.ToString()+"0"+j.ToString()+'_'+tablehz+" "+"where"+" "+tiaojiao+" "+"union"+" ";
//
//      }
//      else
//      {
//       sql+="select"+" "+zhiduan+" "+"from"+" "+i.ToString()+j.ToString()+'_'+tablehz+" "+"where"+" "+tiaojiao+" "+"union"+" ";
//      }
//     }
//    }
//    //3,循环到结束的月份
//    js=Convert.ToInt32(zendtime.Substring(0,4)+"00");
//    for(i=js;i//    {
//     i++;
//     sql+="select"+" "+zhiduan+" "+"from"+" "+i.ToString()+'_'+tablehz+" "+"where"+" "+tiaojiao+" "+"union"+" ";
//
//    }
//    sql+="select"+" "+zhiduan+" "+"from"+" "+eTime.ToString()+'_'+tablehz+" "+"where"+" "+tiaojiao+" ";
//
//   }
//   return sql;
//  }
//  #endregion
<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21831725/viewspace-1115793/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2009-06-06