[C#] 自己封装的一个数据库访问类 让ado.net 用起来和ado一样

news/2024/7/1 23:16:16

using System;
using System.Data;
using System.Collections;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace Government.Common
{
 ///


 /// CDataBase 的摘要说明。
 ///

 public class CDataBase
 {
  public CDataBase()
  {
  }


  
  ///


  /// 取单值
  ///

  /// SQL 命令
  /// 结果
  public object GetSingleValue( string strSQL )
  {
   OleDbConnection conn = null;
   OleDbCommand cmd = null;
   
   try
   {
    conn = new OleDbConnection(GV.s_strConnection);
    conn.Open();

    cmd = conn.CreateCommand();
    cmd.CommandText = strSQL;
    cmd.CommandType = CommandType.Text;
    cmd.CommandTimeout = 5;

    return cmd.ExecuteScalar();
   }
   catch(Exception e)
   {
                GV.Assert( false
     , "CDataBase::GetSingleValue"
     , strSQL + "/r/n" + e.Message
     );

    cmd.Cancel();
    return "";
   }
   finally
   {
    if( cmd != null )
    {
     cmd.Dispose();
    }
        
    if( conn != null )
    {    
     if( conn.State != ConnectionState.Closed )
     {
      conn.Close();
     }
     conn.Dispose();
    }
   }
  }// GetSingleValue

  ///


  /// 判断结果是否为空
  ///

  /// SQL 命令
  /// 是否成功
  public bool IsNull( string strSQL )
  {
   return ( GetSingleValue(strSQL) == null );
  }// IsNull

  ///


  /// 执行单条SQL命令
  ///

  /// SQL 命令
  /// 是否成功
  public bool ExecuteSQL(string strSQL)
  {
   OleDbConnection conn = null;
   OleDbCommand cmd = null;

      
   try
   {   
    conn = new OleDbConnection(GV.s_strConnection);
    conn.Open();

    cmd = conn.CreateCommand();
    cmd.CommandText = strSQL;
    cmd.CommandType = CommandType.Text;
    cmd.CommandTimeout = 5;
    cmd.ExecuteNonQuery();

    return true;
   }
   catch(Exception e)
   {
    GV.Assert( false
     , "CDataBase::ExecuteSQL"
     , strSQL + "/r/n" + e.Message
     );

                cmd.Cancel();
    return false;
   }
   finally
   {
    if( cmd != null )
    {
     cmd.Dispose();
    }
        
    if( conn != null )
    {    
     if( conn.State != ConnectionState.Closed )
     {
      conn.Close();
     }
     conn.Dispose();
    }
   }
  }// ExecuteSQL

  ///


  /// 在一个事务中依次执行多条SQL
  ///

  /// SQL 命令组
  /// 是否成功
  public bool ExecuteMultiSQL(ref ArrayList arySQL )
  {
   OleDbConnection conn = null;
   OleDbCommand cmd = null;
   OleDbTransaction tran = null;
   int index = 0;


   try
   {
    conn = new OleDbConnection(GV.s_strConnection);
    conn.Open();

    tran = conn.BeginTransaction();
    cmd = conn.CreateCommand();
    cmd.Transaction = tran;
    cmd.CommandType = CommandType.Text;
    cmd.CommandTimeout = 5;

    for( index = 0; index < arySQL.Count; index++)
    {
     cmd.CommandText = arySQL[index].ToString();
     cmd.ExecuteNonQuery();
    }

    tran.Commit();
    return true;
   }
   catch(Exception e)
   {
    GV.Assert( false
     , "CDataBase::ExecuteMultiSQL"
     , arySQL[index].ToString() + "/r/n" + e.Message
     );

    cmd.Cancel();
    tran.Rollback();
    return false;
   }
   finally
   {
    if( cmd != null )
    {
     cmd.Dispose();
    }
        
    if( conn != null )
    {    
     if( conn.State != ConnectionState.Closed )
     {
      conn.Close();
     }
     conn.Dispose();
    }
   }
  }// ExecuteMultiSQL


  ///


  /// 获取查询视图(不可更新)
  ///

  /// SQL命令
  /// 获取DataSet视图
  public DataSet GetView(string strSQL)
  {
   OleDbConnection conn = null;
   OleDbDataAdapter adapter = null;
   DataSet ds = null;

   try
   {
    conn = new OleDbConnection(GV.s_strConnection);
    conn.Open();

    ds = new DataSet();
    adapter = new OleDbDataAdapter( strSQL, conn);
    adapter.Fill(ds);

    return ds;
   }
   catch(Exception e)
   {
    GV.Assert( false
     , "CDataBase::GetView"
     , strSQL.ToString() + "/r/n" + e.Message
     );

    if( ds != null )
     ds.Dispose();
    return null;
   }
   finally
   {
    if( adapter != null )
    {
     adapter.Dispose();
    }

    if( conn != null )
    {    
     if( conn.State != ConnectionState.Closed )
     {
      conn.Close();
     }
     conn.Dispose();
    }
   }
  }// GetView


  ///


  /// 获取SQL Server存储过程的记录集
  ///

  public DataSet GetProcView( string strSQL)
  {
   SqlConnection conn = null;
   SqlCommand cmd = null;
   SqlDataAdapter adapter = null;
   DataSet ds = null;

   try
   {
    conn = new SqlConnection(GV.s_strSqlConnection);
    cmd = new SqlCommand( strSQL, conn);
    conn.Open();

    ds = new DataSet();
    adapter = new SqlDataAdapter(cmd);
    adapter.Fill(ds);

    return ds;
   }
   catch(Exception e)
   {
    GV.Assert( false
     , "CDataBase::GetProcView"
     , strSQL.ToString() + "/r/n" + e.Message
     );

    if( ds != null )
     ds.Dispose();
    return null;
   }
   finally
   {
    if( adapter != null )
    {
     adapter.Dispose();
    }

    if( cmd != null )
    {
     cmd.Dispose();
    }

    if( conn != null )
    {    
     if( conn.State != ConnectionState.Closed )
     {
      conn.Close();
     }
     conn.Dispose();
    }
   }

  }

  ///


  /// 获取查询记录集(可以更新)
  ///

  /// SQL命令
  /// 记录集
  public CRecordSet GetRecordSet( string strSQL )
  {
   OleDbConnection conn = null;

   try
   {
    conn = new OleDbConnection(GV.s_strConnection);
    
    return ( new CRecordSet( ref conn, strSQL) );
   }
   catch( Exception e )
   {
    GV.Assert( false
     , "CDataBase::GetRecordSet"
     , strSQL + "/r/n" + e.Message);

    if( conn != null )
    {    
     if( conn.State != ConnectionState.Closed )
     {
      conn.Close();
     }
     conn.Dispose();
    }

    return null;
   }
  }// GetRecordSet end

 }// CDataBase end
}

using System;
using System.Data;
using System.Data.OleDb;
using  System.Diagnostics;


namespace Government.Common
{
 ///


 /// RecordSet 用于模拟实现ADO的使用
 ///

 public class CRecordSet
 {

  // Members
  OleDbDataAdapter m_Adapter = null;
  OleDbCommandBuilder m_CommandBuilder = null;
  DataSet    m_DataSet = null;
  Int32    m_iCurPtr = 0;
  

  ///


  /// 构造函数
  ///

  /// 连接对象
  /// 表名
  /// WHERE 子句
  public CRecordSet( ref OleDbConnection objConn
        ,string strSQL)
  {
   m_Adapter = new OleDbDataAdapter( strSQL, objConn);
   m_CommandBuilder = new OleDbCommandBuilder(m_Adapter);

   m_DataSet = new DataSet();
   m_Adapter.Fill( m_DataSet );
   this.MoveFirst();
  }// CRecordSet end


  // 获取记录集信息
  //
  
  public int GetRecordCount()
  {
   if( m_DataSet == null )
    return 0;

   return m_DataSet.Tables[0].Rows.Count;
  }

  public bool IsNull()
  {
   return ( this.GetRecordCount() == 0 );
  }

  public bool IsBOF()
  {
   return ( this.IsNull() || m_iCurPtr == 0 );
  }

  public bool IsEOF()
  {
   return ( this.IsNull() || m_iCurPtr >= this.GetRecordCount() );
  }

  public int GetFieldCount()
  {
   if( m_DataSet != null )
   {
    return m_DataSet.Tables[0].Columns.Count;
   }
   return 0;
  }

  public string GetFieldName( int nColumn )
  {
   GV.Assert( ( nColumn >= 0) && (nColumn < m_DataSet.Tables[0].Columns.Count)
    ,  "CRecordSet::GetFieldName"
    , "索引越界!");


   if( m_DataSet != null &&
     nColumn >= 0 &&
    nColumn < m_DataSet.Tables[0].Columns.Count )
   {
    return m_DataSet.Tables[0].Columns[nColumn].ColumnName;
   }

   return null;
  }

  public Type GetFieldType( int nColumn )
  {
   GV.Assert( ( nColumn >= 0) && (nColumn < m_DataSet.Tables[0].Columns.Count)
    ,  "CRecordSet::GetFieldType"
    , "索引越界!");


   if( m_DataSet != null &&
     nColumn >= 0 &&
    nColumn < m_DataSet.Tables[0].Columns.Count )

   {
    return m_DataSet.Tables[0].Columns[nColumn].DataType;
   }

   return null;
  }

  // 移动光标
  //

  public bool MoveFirst()
  {
   m_iCurPtr = 0;
   return true;
  }

  public bool MoveLast()
  {
   if( !this.IsNull() )
   {
    m_iCurPtr = this.GetRecordCount() - 1;
    return true;
   }
   
   return false;      
  }

  public bool MovePre()
  {
   GV.Assert( !this.IsBOF()
    , "CRecordSet::MovePre"
    , "已经到头 !");


   if( !this.IsBOF() )
   {
    m_iCurPtr --;
    return true;
   }

   return false;
  }

  public bool MoveNext()
  {
   GV.Assert( !this.IsEOF()
    , "CRecordSet::MoveNext"
    , "已经到头 !");


   if( !this.IsEOF() )
   {
    m_iCurPtr ++;
    return true;
   }

   return false;
  }


  // 取值
  //  

  public string GetCollect( int nColumn )
  {
   GV.Assert( ( nColumn >= 0) && (nColumn < m_DataSet.Tables[0].Columns.Count)
    ,  "CRecordSet::GetCollect"
    , "索引越界!");


   if( this.IsNull() )
    return null;

   try
   {
    if(  nColumn >= 0 &&
     nColumn < m_DataSet.Tables[0].Columns.Count )
    {
     return m_DataSet.Tables[0].Rows[m_iCurPtr][nColumn].ToString();
    }
   }
   catch( Exception e )
   {
    GV.Assert( false, "CRecordSet::GetCollect", e.Message);
   }
   return null;   
  }

  public string GetCollect( string strField )
  {
   GV.Assert( strField.Length > 0
    ,  "CRecordSet::GetCollect"
    , "字段名为空!");


   if( this.IsNull() )
    return null;

   try
   {
    if( strField.Length > 0 )
    {
     return m_DataSet.Tables[0].Rows[m_iCurPtr][strField].ToString();
    }
   }
   catch( Exception e )
   {
    GV.Assert( false, "CRecordSet::GetCollect", e.Message);
   }
   return null; 
  }


  // 赋值
  //
  
  public bool PutCollect( int nColumn, string strValue)
  {
   GV.Assert( ( nColumn >= 0) && (nColumn < m_DataSet.Tables[0].Columns.Count)
    ,  "CRecordSet::PutCollect"
    , "索引越界!");


   if( this.IsNull() )
    return false;

   try
   {
    if(  nColumn >= 0 &&
     nColumn < m_DataSet.Tables[0].Columns.Count )
    {
     if( strValue.Length == 0 )
      m_DataSet.Tables[0].Rows[m_iCurPtr][nColumn] = DBNull.Value;
     else
      m_DataSet.Tables[0].Rows[m_iCurPtr][nColumn] = strValue;

     return true;
    }
   }
   catch( Exception e )
   {
    GV.Assert( false, "CRecordSet::PutCollect", e.Message);
   }
   
   return false;
  }

  public bool PutCollect( string strField, string strValue)
  {
   GV.Assert( strField.Length > 0
    ,  "CRecordSet::PutCollect"
    , "字段名为空!");


   if( this.IsNull() )
    return false;

   try
   {
    if( strValue.Length == 0 )
                    m_DataSet.Tables[0].Rows[m_iCurPtr][strField] = DBNull.Value;
    else
     m_DataSet.Tables[0].Rows[m_iCurPtr][strField] = strValue;

    return true;
   }
   catch(Exception e)
   {
    GV.Assert( false, "CRecordSet::PutCollect", e.Message);
    return false;
   }   
  }


  // 动作
  //

  // 添加
  public bool AddNew()
  {
   DataRow dr = m_DataSet.Tables[0].NewRow();
   m_DataSet.Tables[0].Rows.Add(dr);
   m_iCurPtr = this.GetRecordCount() - 1;
   return true;
  }

  // 删除
  public bool Delete()
  {
   if( this.IsNull() )
    return false;

   m_DataSet.Tables[0].Rows[m_iCurPtr].Delete();

   if( m_iCurPtr > this.GetRecordCount() - 1 )
   {
    m_iCurPtr = this.GetRecordCount() - 1;
   }
   return true;
  }

  // 更新数据
  public bool Update()
  {
   try
   {
    m_Adapter.Update( m_DataSet.GetChanges() );
    return true;
   }
   catch(Exception e)
   {
    m_DataSet.RejectChanges();
    GV.Assert( false, "CRecordSet::Update", e.Message);
    return false;
   }
  }


  // 取消更新
  public bool CancelUpdate()
  {
   m_DataSet.RejectChanges();
   return true;
  }

  // 关闭
  public bool Close()
  {
   m_Adapter.Dispose();
   m_CommandBuilder.Dispose();
   m_DataSet.Dispose();
   return true;
  }
 }
}





http://www.niftyadmin.cn/n/3654642.html

相关文章

Axure RP6学习笔记(1)

无意中得知交互原型设计工具Axure RP&#xff0c;觉得对目前的工作可能会有很大的助力&#xff0c;从网上搜索了很多资料&#xff0c;发现大部分都是基于5.X或4.X版本的文档&#xff0c;但是也给了我很多的启示&#xff0c;非常感谢网友们的无私奉献&#xff0c; 从今天开始写上…

[C#] 生成略缩图

// 生成略缩图 string strImage null; string strRegex ".*?)(""|)?(/s|>)";MatchCollection mc Regex.Matches( FreeTextBox.Text, strRegex,RegexOptions.IgnoreCase | RegexOptions.Singleline);foreach(Match m in mc){strImage m.Groups…

Axure RP6 学习笔记(2)

学习资料【Axure 快速原型设计Axure Rapid Prototyping 作者&#xff1a;陈良泳】 以学习的习惯&#xff0c;先来一个例子~~~ 第一个实例 简单登陆界面 案例描述 这是一个较为简单的登陆界面&#xff0c;输入用户名、密码后&#xff0c;点击登陆按钮进行登陆校验&#xff1a;…

央行发布第三方支付业务系统检测认证新规

中国人民银行2011年6月21日发布《非金融机构支付服务业务系统检测认证管理规定》&#xff0c;明确将对因认证问题造成不良后果的机构给予处罚&#xff0c;保障非金融机构支付服务业务系统检测认证工作规范有序开展&#xff0c;并即日起开始施行。金融机构支付服务业务系统检测认…

[MSSQL]将用户表 存储过程 变成系统的

将用户表变成系统表exec sp_configure allow updates,1 reconfigure with overrideUPDATE sysobjects SET status status | 0x80000000 WHERE [NAME] 表名称exec sp_configure allow updates,0 reconfigure with override还原exec sp_configure allow updates,1 reconfigure …

城市一卡通系统走进线上支付“芯”时代

互联网的快速发展极大地丰富了人们的生活&#xff0c;催生了大量在线支付应用吸引了用户的极大热情&#xff0c;这从第三方支付市场的快速增长就可见一斑&#xff0c;在首批27家企业获得牌照之后&#xff0c;二批牌照申请工作正在紧锣密鼓进行之中。与此同时&#xff0c;城市一…

IntelliJ IDEA中tomcat启动项目一直在build问题

用到的工具和配置&#xff1a; IntelliJ IDEA 2019.2.4 x64Tomcat 8.5.51JDK 1.8maven包是同事给的。 最近接触到一个难搞的jsp项目&#xff0c;之所以说它难搞是因为&#xff0c;在配置好所有配置后&#xff0c;居然启动不了&#xff0c;一直在build。 排查错误过程 删除本…