ASP.NET封装的数据库访问基类
2005-12-21 10:42
453 查看
--------------------------------------------------------------------------------
作者:[T-by] 浏览次数:9 更新日期:05-08-20 网页设计超市
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Db
{
///
/// Base 的摘要说明。
///
public class Base
{
public Base()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected static SqlConnection conn =new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
protected static SqlCommand cmd = new SqlCommand(strSp,conn);
protected static SqlDataAdapter da = new SqlDataAdapter();
protected static DataSet ds = new DataSet();
protected static DataView dv = new DataView();
protected static SqlDataReader dr;
protected static SqlParameter[] prams;
protected static string strSp;
protected static SqlDataReader drSelectAll(string strSp)
{
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}//返回一个SqlDataReader
protected static DataSet dsSelectAll(string strSp)
{
da.SelectCommand = new SqlCommand(strSp,conn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.Fill(ds);
conn.Open();
try
{
da.SelectCommand.ExecuteNonQuery();
return ds;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Dispose();
conn.Close();
}
}//返回一个SqlDataSet
protected static DataView dvSelectAll(string strSp)
{
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = new SqlCommand(strSp,conn);
da.Fill(ds);
conn.Open();
try
{
da.SelectCommand.ExecuteNonQuery();
dv = ds.Tables[0].DefaultView;
return dv;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Dispose();
conn.Close();
}
}//返回一个DataView
protected static string strCmd(string strSp,SqlParameter[] prams,SqlDataReader dr)
{
CreateCmd(strSp,prams,dr);
conn.Open();
try
{
cmd.ExecuteNonQuery();
return "1";
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Dispose();
conn.Close();
}
}//返回一个数据库操作
protected static SqlCommand CreateCmd(string strSp, SqlParameter[] prams,SqlDataReader dr)
{
cmd.CommandType = CommandType.StoredProcedure;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return cmd;
}//返回带参数的命令
protected static bool ExecuteSQLs()
{
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
try
{
int i = (int)cmd.ExecuteScalar();
if(i>0)
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Dispose();
conn.Close();
}
}//返回第一行的数据操作
}
}
作者:[T-by] 浏览次数:9 更新日期:05-08-20 网页设计超市
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Db
{
///
/// Base 的摘要说明。
///
public class Base
{
public Base()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected static SqlConnection conn =new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
protected static SqlCommand cmd = new SqlCommand(strSp,conn);
protected static SqlDataAdapter da = new SqlDataAdapter();
protected static DataSet ds = new DataSet();
protected static DataView dv = new DataView();
protected static SqlDataReader dr;
protected static SqlParameter[] prams;
protected static string strSp;
protected static SqlDataReader drSelectAll(string strSp)
{
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}//返回一个SqlDataReader
protected static DataSet dsSelectAll(string strSp)
{
da.SelectCommand = new SqlCommand(strSp,conn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.Fill(ds);
conn.Open();
try
{
da.SelectCommand.ExecuteNonQuery();
return ds;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Dispose();
conn.Close();
}
}//返回一个SqlDataSet
protected static DataView dvSelectAll(string strSp)
{
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = new SqlCommand(strSp,conn);
da.Fill(ds);
conn.Open();
try
{
da.SelectCommand.ExecuteNonQuery();
dv = ds.Tables[0].DefaultView;
return dv;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Dispose();
conn.Close();
}
}//返回一个DataView
protected static string strCmd(string strSp,SqlParameter[] prams,SqlDataReader dr)
{
CreateCmd(strSp,prams,dr);
conn.Open();
try
{
cmd.ExecuteNonQuery();
return "1";
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Dispose();
conn.Close();
}
}//返回一个数据库操作
protected static SqlCommand CreateCmd(string strSp, SqlParameter[] prams,SqlDataReader dr)
{
cmd.CommandType = CommandType.StoredProcedure;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return cmd;
}//返回带参数的命令
protected static bool ExecuteSQLs()
{
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
try
{
int i = (int)cmd.ExecuteScalar();
if(i>0)
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Dispose();
conn.Close();
}
}//返回第一行的数据操作
}
}
相关文章推荐
- 效果很好的asp.net的数据库访问模型(优化,封装一体化)
- 效果很好的asp.net的数据库访问模型(优化,封装一体化)
- 效果很好的asp.net的数据库访问模型(优化,封装一体化)
- [原创]用ASP.NETN层结构封装的数据层访问基类
- 效果很好的asp.net的数据库访问模型(优化,封装一体化)
- ASP.NET数据库编程之处理文件访问许可
- Asp.Net访问IIS元数据库失败
- 配置ASP.NET平台时遇到的“访问IIS元数据库失败”
- ASP.NET C# 访问Access、Xml等数据库
- 配置ASP.NET平台时遇到的“访问IIS元数据库失败”--解决方案
- ASP.NET配置程序无法访问数据库
- ASP.NET MVC- EF返回连接池用ADO.NET方式访问数据库
- heml Ajax 通过Asp.net访问数据库(以Sql Server为例)
- 用程序(asp,asp.net等任何可以访问数据库的语言)来自动建立FTP帐号(serv-u的odbc设置)
- ASP.NET的数据库基本访问
- Asp.net数据库操作基类
- MySql基于ADO.NET方式访问数据库ADOHelper封装MySqlHelper
- 部署Asp.net系统时页面访问Oracle 10G数据库失败
- ASP.NET下使用ADO.NET访问本地数据库
- 要访问本地IIS 网站,必须安装下列IIS组建: IIS6元数据库与IIS6配置兼容性ASP.NET