博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

数据库访问(一)

Posted on 2005-12-29 19:38  欧阳另类  阅读(172)  评论(0)    收藏  举报
网上有很多数据库访问的代码,我也整理了一点,希望有高手可以指点一二

访问基类
namespace Alawn.ClassLibrary.Database
{
    
/// <summary>
    
/// BasicClass 为数据库联接基类。
    
/// </summary>

    
    
using System;
    
using System.ComponentModel;
    
using System.Collections;
    
using System.Diagnostics;
    
using System.Data;
    
using System.Data.SqlClient;
    
using System.Configuration;

    
public class BasicClass :IDisposable
    
{
        
        
protected SqlConnection            m_oConnection;
        
protected SqlCommand            m_oCommad;
        
protected SqlDataReader            m_oSqlDataReader;
        
protected DataSet                m_oDataSet;
        
protected SqlDataAdapter        m_oSqlDataAdapter;
        
protected DataView                m_oDataView;
        
protected string                m_ConnectionString ;

        
public BasicClass()
        
{
            m_ConnectionString            
= ConfigurationSettings.AppSettings["ConnectionString"];
            
this.m_oCommad                = new SqlCommand();
            
this.m_oSqlDataAdapter        = new SqlDataAdapter();
            
this.m_oDataSet                = new DataSet();
        }


        
/// <summary>
        
/// 打开数据库连接
        
/// </summary>

        protected void Open()
        
{
            
if(this.m_oConnection == null)
                
this.m_oConnection = new SqlConnection(this.m_ConnectionString);
            
if(this.m_oConnection.State == System.Data.ConnectionState.Closed)
                
this.m_oConnection.Open();
        }


        
/// <summary>
        
/// 关闭数据库连接
        
/// </summary>

        protected void Close()
        
{
            
if(this.m_oConnection != null)
                
this.m_oConnection.Close();
        }


        
/// <summary>
        
/// 释放资源
        
/// </summary>

        public  void Dispose() 
        
{
            
if (m_oConnection != null
            
{
                m_oConnection.Dispose();
                m_oConnection 
= null;
            }
                
        }


    }

}