ASP.NET封装的数据库访问基类

该博客为转载内容,转载自https://www.cnblogs.com/nrq/archive/2005/12/30/308514.html ,原内容可能与数据库相关。
None.gifusing System; 
None.gif
using System.Data; 
None.gif
using System.Data.SqlClient; 
None.gif
using System.Configuration; 
None.gif
namespace Db 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Base 的摘要说明。 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifpublic class Base 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
public Base() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
// 
InBlock.gif
// TODO: 在此处添加构造函数逻辑 
InBlock.gif
// 
ExpandedSubBlockEnd.gif
}
 
InBlock.gif
protected static SqlConnection conn =new SqlConnection(ConfigurationSettings.AppSettings["dsn"]); 
InBlock.gif
protected static SqlCommand cmd = new SqlCommand(strSp,conn); 
InBlock.gif
protected static SqlDataAdapter da = new SqlDataAdapter(); 
InBlock.gif
protected static DataSet ds = new DataSet(); 
InBlock.gif
protected static DataView dv = new DataView(); 
InBlock.gif
protected static SqlDataReader dr; 
InBlock.gif
protected static SqlParameter[] prams; 
InBlock.gif
protected static string strSp; 
InBlock.gif
protected static SqlDataReader drSelectAll(string strSp) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifcmd.CommandType 
= CommandType.StoredProcedure; 
InBlock.gifconn.Open(); 
InBlock.gifdr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection); 
InBlock.gif
return dr; 
ExpandedSubBlockEnd.gif}
//返回一个SqlDataReader 
InBlock.gif
protected static DataSet dsSelectAll(string strSp) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifda.SelectCommand 
= new SqlCommand(strSp,conn); 
InBlock.gifda.SelectCommand.CommandType 
= CommandType.StoredProcedure; 
InBlock.gifda.Fill(ds); 
InBlock.gifconn.Open(); 
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifda.SelectCommand.ExecuteNonQuery(); 
InBlock.gif
return ds; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch(Exception ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
throw new Exception(ex.Message); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifconn.Dispose(); 
InBlock.gifconn.Close(); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
//返回一个SqlDataSet 
InBlock.gif
protected static DataView dvSelectAll(string strSp) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifcmd.CommandType 
= CommandType.StoredProcedure; 
InBlock.gifda.SelectCommand 
= new SqlCommand(strSp,conn); 
InBlock.gifda.Fill(ds); 
InBlock.gif
InBlock.gifconn.Open(); 
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifda.SelectCommand.ExecuteNonQuery(); 
InBlock.gifdv 
= ds.Tables[0].DefaultView; 
InBlock.gif
return dv; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch(Exception ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
throw new Exception(ex.Message); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifconn.Dispose(); 
InBlock.gifconn.Close(); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
//返回一个DataView 
InBlock.gif
protected static string strCmd(string strSp,SqlParameter[] prams,SqlDataReader dr) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifCreateCmd(strSp,prams,dr); 
InBlock.gifconn.Open(); 
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifcmd.ExecuteNonQuery(); 
InBlock.gif
return "1"
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch(Exception ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
throw new Exception(ex.Message); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifconn.Dispose(); 
InBlock.gifconn.Close(); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
//返回一个数据库操作 
InBlock.gif
protected static SqlCommand CreateCmd(string strSp, SqlParameter[] prams,SqlDataReader dr) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifcmd.CommandType 
= CommandType.StoredProcedure; 
InBlock.gif
if (prams != null
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
foreach (SqlParameter parameter in prams) 
InBlock.gifcmd.Parameters.Add(parameter); 
ExpandedSubBlockEnd.gif}
 
InBlock.gifcmd.Parameters.Add( 
InBlock.gif
new SqlParameter("ReturnValue", SqlDbType.Int, 4
InBlock.gifParameterDirection.ReturnValue, 
false00
InBlock.gif
string.Empty, DataRowVersion.Default, null)); 
InBlock.gif
return cmd; 
ExpandedSubBlockEnd.gif}
//返回带参数的命令 
InBlock.gif
protected static bool ExecuteSQLs() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifcmd.CommandType 
= CommandType.StoredProcedure; 
InBlock.gifconn.Open(); 
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
int i = (int)cmd.ExecuteScalar(); 
InBlock.gif
if(i>0
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return true
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return false
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch(Exception ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
throw new Exception(ex.Message); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifconn.Dispose(); 
InBlock.gifconn.Close(); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
//返回第一行的数据操作 
ExpandedSubBlockEnd.gif
}
 
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/nrq/archive/2005/12/30/308514.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值