为数据访问层编写一个基类 〔zz〕

该博客为转载内容,转载自https://www.cnblogs.com/penboy/archive/2005/05/11/152830.html ,涉及数据库相关知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  http://www.cnblogs.com/esshs/archive/2005/04/12/135883.html  
      编写数据访问层代码时,总要一边又一边的重复编写:读数据库连接字符串,建立数据库连接对象,打开连接,创建Command对象,创建数据适配器,创建数据集,填充数据集,关闭连接。这种重复的代码写一两次到还行,写多了就免不了有一些烦了。
  在总结以前的的代码以后,决定重构数据方法层的代码。数据访问层无非进行两种操作:查询返回DataTable,进行插入、更新、删除等无返回值的操作。只要增加一个数据访问层基类包含这些繁琐的代码,其余的数据访问层代码继承数据访问层基类,在调用基类函数时给出存储过程名称和存储过程参数即可。

数据访问层基类代码:
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Collections; 
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  DAL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// DALBase 的摘要说明。
InBlock.gif    
/// 数据层访问基类,定义数据层访问公共的变量,方法
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class DALBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//定义该类共用变量
InBlock.gif
        private SqlConnection conn;        //
InBlock.gif
        private SqlCommand mycm;        //
InBlock.gif
        private DataSet myds;            //
InBlock.gif
        private SqlDataAdapter myda;    //
InBlock.gif
        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从web.config中读取数据库连接字符串
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private string CONNSTR = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
InBlock.gif
InBlock.gif        
public DALBase()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//构造函数,创建对象实例
InBlock.gif
            conn = new SqlConnection(CONNSTR);
InBlock.gif            mycm 
= conn.CreateCommand();
InBlock.gif            myds 
= new DataSet();
InBlock.gif            myda 
= new SqlDataAdapter();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 通过存储过程返回查询表的信息
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sprocName">存储过程名称</param>
ExpandedSubBlockEnd.gif        
/// <returns>DataTable</returns>

InBlock.gif        protected DataTable GetTable(string sprocName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            conn.Open();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mycm.CommandText 
= sprocName;
InBlock.gif                mycm.CommandType 
= CommandType.StoredProcedure;
InBlock.gif                myda.SelectCommand 
= mycm;
InBlock.gif                myda.Fill(myds);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//无论语句执行正确与否,都关闭连接释放资源
InBlock.gif
                conn.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return myds.Tables[0];
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 通过存储过程和参数返回查询表的信息
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sprocName"></param>
InBlock.gif        
/// <param name="parameters"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        protected DataTable GetTable(string sprocName, SqlParameter[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            conn.Open();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mycm.CommandText 
= sprocName;
InBlock.gif                mycm.CommandType 
= CommandType.StoredProcedure;
InBlock.gif                SqlParameterCollection sqlParams 
= mycm.Parameters;
InBlock.gif                
//先清空原有的参数
InBlock.gif
                mycm.Parameters.Clear();
InBlock.gif                
//给Command添加参数
InBlock.gif
                foreach ( SqlParameter parameter in parameters )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    mycm.Parameters.Add( parameter );
ExpandedSubBlockEnd.gif                }

InBlock.gif                myda.SelectCommand 
= mycm;
InBlock.gif                myda.Fill(myds);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//无论语句执行正确与否,都关闭连接释放资源
InBlock.gif
                conn.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return myds.Tables[0];
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 通过存储过程及存储过程参数执行对数据库无返回值的操作(如:新增,更新,删除等)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sprocName">存储过程名称</param>
ExpandedSubBlockEnd.gif        
/// <param name="parameters">存储过程参数</param>

InBlock.gif        protected void SaveTale(string sprocName, SqlParameter[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mycm.CommandText 
= sprocName; 
InBlock.gif            mycm.CommandType 
= CommandType.StoredProcedure; 
InBlock.gif            SqlParameterCollection sqlParams 
= mycm.Parameters;
InBlock.gif            
//先清空原有的参数
InBlock.gif
            mycm.Parameters.Clear();
InBlock.gif            
//给Command添加参数
InBlock.gif
            foreach ( SqlParameter parameter in parameters )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mycm.Parameters.Add( parameter );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//打开连接
InBlock.gif
            conn.Open();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//执行
InBlock.gif
                mycm.ExecuteNonQuery(); 
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//关闭连接
InBlock.gif
                conn.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


数据访问层代码:
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Collections; 
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  DAL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Test : DALBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Test()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public DataTable GetTestTable()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          
return base.GetTable("存储过程名称");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public DataTable GetTestTableByXName(string XName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            SqlParameter[] parameters 
= dot.gifnew SqlParameter( "@XName",SqlDbType.NVarChar,10 ) };     
InBlock.gif            
return base.GetTable("存储过程名称",parameters);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public void AddTestTable(string XName, string Description)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlParameter[] parameters 
= 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
new SqlParameter( "@XName",SqlDbType.NVarChar,10 ),
InBlock.gif                
new SqlParameter( "@Description",SqlDbType.NVarChar,100)
ExpandedSubBlockEnd.gif            }
;
InBlock.gif    
InBlock.gif            
//设置参数值
InBlock.gif
            parameters[0].Value = XName;
InBlock.gif            parameters[
1].Value = Description;
InBlock.gif            
base.SaveTale("存储过程名称",parameters);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

  大家有兴趣还可以帮忙扩充一下这个基类,比如增加通过SQL语句返回DataTable,返回单值结果(如:查询合计)……

转载于:https://www.cnblogs.com/penboy/archive/2005/05/11/152830.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值