现在的位置: 首页 > 综合 > 正文

.net数据库操作类(C#)

2011年12月24日 ⁄ 综合 ⁄ 共 3516字 ⁄ 字号 评论关闭
       ASP.NET中一般都是使用SQL Server作为后台数据库。一般的ASP.NET数据库操作示例程序都是使用单独的数据访问,就是说每个页面都写连接到数据库,存取数据,关闭数据库的代码。这种方式带来了一些弊端,一个就是如果你的数据库改变了,你必须一个页面一个页面的去更改数据库连接代码。第二个弊端就是代码冗余,很多代码都是重复的,不必要的。因此,我试图通过一种一致的数据库操作类来实现ASP.NET种的数据访问,现写出来希望哪位大虾帮助指正!

.net数据库操作类代码:
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
namespace DataOperate
{
    
/// <summary>
    
/// DataAccess 的摘要说明。
    
/// </summary>

    public class DataAccess
    
{
        
protected SqlCommand Comm;
        
protected SqlDataAdapter Adap;    
        
protected SqlConnection Conn;    //SQL连接
        private string _connectString;    //连接串
        private string _commandString;    //SQL命令
        private Hashtable _dict, _result,_mapTable;            
        
private DataSet _ds;    //返回结果数据集
        private DataRow _recordSet;        //纪录集
        private string _tableName;        //表名
        private int _recordCount;            //纪录集的行数
        private bool _eOF;                //结果集是否为空,是否已经到了结尾
        private string DB;
        
private string _deleteOP;
        
private string _path;
        
private StreamWriter SWCreate,SWApp;
        
private string _errorMessage;
        
private bool _writeLog;
        
        
///属性集
        
/// <summary>
        
/// 出错信息
        
/// </summary>

        public string ErrorMessage
        
{
            
get return this._errorMessage; }
            
set this._errorMessage = value; }
        }


        
/// <summary>
        
/// 设置或者取得删除的操作者
        
/// </summary>

        public string DeleteOP
        
{
            
get return this._deleteOP; }
            
set this._deleteOP = value; }
        }


        
/// <summary>
        
/// 取得是否溢出
        
/// </summary>

        public bool EOF
        
{
            
get return this._eOF; }
            
set this._eOF = value;}
        }


        
/// <summary>
        
/// 取得执行语句后得出的纪录条数
        
/// </summary>

        public int RecordCount
        
{
            
get return this._recordCount; }
            
set this._recordCount = value; }
        }


        
/// <summary>
        
/// 数据库中的表名
        
/// </summary>

        public string TableName
        
{
            
get return this._tableName; }
            
set this._tableName = value; }
        }


        
/// <summary>
        
/// 返回的记录集
        
/// </summary>

        public DataRow RecordSet
        
{
            
get return this._recordSet; }
            
set this._recordSet = value; }
        }


        
/// <summary>
        
/// 返回的数据集
        
/// </summary>

        public DataSet DS
        
{
            
get return this._ds; }
            
set this._ds = value; }
        }


        
/// <summary>
        
/// 字段和控件的映射表
        
/// </summary>

        public Hashtable MapTable
        
{
            
get return this._mapTable; }
            
set this._mapTable = value; }
        }


        
/// <summary>
        
/// 修改数据时,作为修改结果
        
/// </summary>

        public Hashtable Result
        
{
            
get return this._result; }
            
set this._result = value;}
        }


        
/// <summary>
        
/// 保存数据用的字段和值对应的哈希表,修改数据时用作条件
        
/// </summary>

        public Hashtable Dict
        
{
            
get return this._dict;}
            
set this._dict = value;}
        }


        
/// <summary>
        
/// 查询语句
        
/// </summary>

        public string CommandString
        
{
            
get return this._commandString;}
            
set this._commandString = value;}
【上篇】
【下篇】

抱歉!评论已关闭.