C#+SQL 存储过程在三层架构中的使用
2011-10-19 17:24
435 查看
从11年6月份毕业到现在(1月份我就在这家公司上班了 ),一直没时间对存储过程进行学习。
之前一直不敢使用存储过程进行SQL的操作,虽然知道存储过程有很多好处,比如查询速度快等优点。但是因为存储过程不知道在代码中如何使用,
就一直没对他进行学习。最近一段时间,因为工作不忙,就抽时间把LittleItalyVineyard的例子好好研究了一下。
通过对其中列子的学习,才知道存储过程怎样使用。
语言描述就不说了,存储过程和三层架构(表示层、业务逻辑层、数据访问层)相信大家都了解。该例子中处理的是商品目录的实现。
下面直接从处理过程来实现他们之间的调用关系。
首先,在SQL server 2005对应的数据库(MyDataTest)-->可编程性-->存储过程,右击新建一个存储过程:
代码如下
/*---------------------------------------商品目录------------------*/
create procedure Products_Select
as
select
ProductID,
ProductName,
ProductCategoryName,
ProductImageID,
substring(Description,1,150)+'...' as Description,
Price
from Products inner join ProductCategory on ProductCategory.ProductCategoryID=Products.ProductCategoryID
1. 在数据访问层DataAccess建立一个ProductSelectData 类,代码如下所示
public class ProductSelectData : DataAccessBase
{
public ProductSelectData()
{
StoredProcedureName = StoredProcedure.Name.Products_Select.ToString();
}
public DataSet Get()
{
DataSet ds;
DataBaseHelper dbhelper = new DataBaseHelper( StoredProcedureName );
ds = dbhelper.Run( ConnectionString );
return ds;
}
}
其中DataAccessBase中实现的是存储过程名StoredProcedureName 和数据库连接字符串ConnectionString;代码如下:
public class DataAccessBase
{
private string _storedprocedureName;
/// <summary>
/// 存储过程
/// </summary>
protected string StoredProcedureName
{
get { return _storedprocedureName; }
set { _storedprocedureName = value; }
}
/// <summary>
/// 数据库连接字符串
/// </summary>
protected string ConnectionString
{
get { return ConfigurationManager.ConnectionStrings[ "SQLCONN" ].ToString(); }
}
}
2. 在业务逻辑层建立一个类ProcessGetProductByID,该类实现接口IBusinessLogic。
public class ProcessGetProductByID : IBusinessLogic
{
private Product _product;
private DataSet _resultset;
public ProcessGetProductByID()
{
}
public void Invoke()
{
ProductSelectByIDData selectproduct = new ProductSelectByIDData();
selectproduct.Product = Product;
ResultSet = selectproduct.Get();
Product.Name = ResultSet.Tables[0].Rows[0]["ProductName"].ToString();
Product.Description = ResultSet.Tables[0].Rows[0]["Description"].ToString();
Product.Price = Convert.ToDecimal( ResultSet.Tables[0].Rows[0]["Price"].ToString() );
Product.ImageID = int.Parse( ResultSet.Tables[0].Rows[0]["ProductImageID"].ToString() );
Product.ProductCategory.ProductCategoryName = ResultSet.Tables[0].Rows[0]["ProductCategoryName"].ToString();
}
public Product Product
{
get { return _product; }
set { _product = value; }
}
private DataSet ResultSet
{
get { return _resultset; }
set { _resultset = value; }
}
}
IBusinessLogic中定义了Ivoke()方法;
3. 建立页面,页面中通过Table在DataList中布局,通过 <%# Eval("ProductName") %>的形式进行数据绑定,
后台代码如下:
private void LoadProducts()
{
ProcessGetProducts processproducts = new ProcessGetProducts();
try
{
processproducts.Invoke();
}
catch
{
Response.Redirect( "ErrorPage.aspx" );
}
datalistProducts.DataSource = processproducts.ResultSet;
datalistProducts.DataBind();
}
本文代码参考LittleItalyVineyard。希望对存储过程的使用不熟悉的人有帮助,谢谢。
之前一直不敢使用存储过程进行SQL的操作,虽然知道存储过程有很多好处,比如查询速度快等优点。但是因为存储过程不知道在代码中如何使用,
就一直没对他进行学习。最近一段时间,因为工作不忙,就抽时间把LittleItalyVineyard的例子好好研究了一下。
通过对其中列子的学习,才知道存储过程怎样使用。
语言描述就不说了,存储过程和三层架构(表示层、业务逻辑层、数据访问层)相信大家都了解。该例子中处理的是商品目录的实现。
下面直接从处理过程来实现他们之间的调用关系。
首先,在SQL server 2005对应的数据库(MyDataTest)-->可编程性-->存储过程,右击新建一个存储过程:
代码如下
/*---------------------------------------商品目录------------------*/
create procedure Products_Select
as
select
ProductID,
ProductName,
ProductCategoryName,
ProductImageID,
substring(Description,1,150)+'...' as Description,
Price
from Products inner join ProductCategory on ProductCategory.ProductCategoryID=Products.ProductCategoryID
1. 在数据访问层DataAccess建立一个ProductSelectData 类,代码如下所示
public class ProductSelectData : DataAccessBase
{
public ProductSelectData()
{
StoredProcedureName = StoredProcedure.Name.Products_Select.ToString();
}
public DataSet Get()
{
DataSet ds;
DataBaseHelper dbhelper = new DataBaseHelper( StoredProcedureName );
ds = dbhelper.Run( ConnectionString );
return ds;
}
}
其中DataAccessBase中实现的是存储过程名StoredProcedureName 和数据库连接字符串ConnectionString;代码如下:
public class DataAccessBase
{
private string _storedprocedureName;
/// <summary>
/// 存储过程
/// </summary>
protected string StoredProcedureName
{
get { return _storedprocedureName; }
set { _storedprocedureName = value; }
}
/// <summary>
/// 数据库连接字符串
/// </summary>
protected string ConnectionString
{
get { return ConfigurationManager.ConnectionStrings[ "SQLCONN" ].ToString(); }
}
}
2. 在业务逻辑层建立一个类ProcessGetProductByID,该类实现接口IBusinessLogic。
public class ProcessGetProductByID : IBusinessLogic
{
private Product _product;
private DataSet _resultset;
public ProcessGetProductByID()
{
}
public void Invoke()
{
ProductSelectByIDData selectproduct = new ProductSelectByIDData();
selectproduct.Product = Product;
ResultSet = selectproduct.Get();
Product.Name = ResultSet.Tables[0].Rows[0]["ProductName"].ToString();
Product.Description = ResultSet.Tables[0].Rows[0]["Description"].ToString();
Product.Price = Convert.ToDecimal( ResultSet.Tables[0].Rows[0]["Price"].ToString() );
Product.ImageID = int.Parse( ResultSet.Tables[0].Rows[0]["ProductImageID"].ToString() );
Product.ProductCategory.ProductCategoryName = ResultSet.Tables[0].Rows[0]["ProductCategoryName"].ToString();
}
public Product Product
{
get { return _product; }
set { _product = value; }
}
private DataSet ResultSet
{
get { return _resultset; }
set { _resultset = value; }
}
}
IBusinessLogic中定义了Ivoke()方法;
3. 建立页面,页面中通过Table在DataList中布局,通过 <%# Eval("ProductName") %>的形式进行数据绑定,
后台代码如下:
private void LoadProducts()
{
ProcessGetProducts processproducts = new ProcessGetProducts();
try
{
processproducts.Invoke();
}
catch
{
Response.Redirect( "ErrorPage.aspx" );
}
datalistProducts.DataSource = processproducts.ResultSet;
datalistProducts.DataBind();
}
本文代码参考LittleItalyVineyard。希望对存储过程的使用不熟悉的人有帮助,谢谢。
相关文章推荐
- T-SQL 存储过程中使用事务并回滚出错的SQL语句(C#事务处理)
- easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)
- SQL2000系统表、存储过程、函数的功能介绍及应用2009年01月21日 星期三 11:38虽然使用系统存储过程、系统函数与信息架构视图已经可以为我们提供了相当丰富的元数据信息,但是对于某些特殊的元数据信息,我们仍然需要直接对系统表进行查询。因为SQL
- asp.net(c#) 下SQL存储过程使用详细实例
- 三层架构中使用实体类填充泛型集合代替DataTable解决方案(ASP.NET+C#)
- 技巧:使用 C# 编写扩展存储过程
- sql2005与sql2008存储过程中 变量使用的一处区别
- sql 修改表、视图、存储过程的架构
- C#与SQL Server存储过程之一(创建):使用C#创建SQL Server的存储过程
- C#遍历SQL服务器上的存储过程和表
- C#中使用Oracle 存储过程笔记 2004.11
- 使用SqlDataAdapter在存储过程中获取记录集填充DataSet
- C#存储过程使用例子
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- C# 调用sql 2000存储过程
- PL/SQL块或者存储过程中不能执行DDL,可以使用动态SQL
- C#使用ORACLE中的存储过程
- 存储过程和SQL语句比较及存储过程在C#中调用方法(转)
- 使用系统存储过程实现的通用分页存储过程.sql
- sql2005与sql2008存储过程中 变量使用的一处区别