DALFactory 和SQLServerDAL
来源:互联网 发布:2015年网络销售数据 编辑:程序博客网 时间:2024/06/07 14:09
首先建立一个类 为Product 如下:
namespace PetShop.SQLServerDAL {
public class Product : IProduct {
//Static constants
}
注意上述的命名空间是PetShop.SQLServerDAL 类名为Product
这里的命名空间要在配职文件中用到的.
如下:
<appSettings>
<add key="WebDAL" value="PetShop.SQLServerDAL"/>
</appSettings>
然后在新的类中如何根据配职文件关联到上述的Product类呢
在DALFactory 中 用如下代码即可
namespace PetShop.DALFactory {
public sealed class DataAccess {
//去读Appconfig下的WebDAL 得到的是value=PetShop.SQLServerDAL;
private static readonly string path = ConfigurationManager.AppSettings["WebDAL"];
private static readonly string orderPath = ConfigurationManager.AppSettings["OrdersDAL"];
//够造函数
private DataAccess() { }
//利用反射获取类对象去找PetShop.SQLServerDAL下的Product类
public static PetShop.IDAL.IProduct CreateProduct() {
string className = path + ".Product";
return (PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);
}
}
}
上述有几点要注意:
1:命名空间和配之文件的一致性,类名和利用反射中使用的类名的一致性
2:利用工厂方法根据配只文件去找相应的类,便于维护,
下面讲下PetShop.SQLServerDAL
该命名空间下包含的是和SQL数据操作相关的增删改
namespace PetShop.SQLServerDAL {
//继承自IProduct接口实现该接口下的所有方法
public class Product : IProduct {
//一些常量定义 如SQL语句,传入的参数等等
private const string SQL_SELECT_PRODUCTS_BY_CATEGORY = "SELECT Product.ProductId, Product.Name, Product.Descn, Product.Image, Product.CategoryId FROM Product WHERE Product.CategoryId = @Category";
private const string SQL_SELECT_PRODUCTS_BY_SEARCH3 = ") OR (";
private const string SQL_SELECT_PRODUCTS_BY_SEARCH4 = "))";
private const string SQL_SELECT_PRODUCT = "SELECT Product.ProductId, Product.Name, Product.Descn, Product.Image, Product.CategoryId FROM Product WHERE Product.ProductId = @ProductId";
private const string PARM_CATEGORY = "@Category";
private const string PARM_KEYWORD = "@Keyword";
private const string PARM_PRODUCTID = "@ProductId";
/// <summary>
/// Query for products by category
/// </summary>
/// <param name="category">category name</param>
/// <returns>A Generic List of ProductInfo</returns>
public IList<ProductInfo> GetProductsByCategory(string category) {
IList<ProductInfo> productsByCategory = new List<ProductInfo>();
SqlParameter parm = new SqlParameter(PARM_CATEGORY, SqlDbType.VarChar, 10);
parm.Value = category;
//Execute a query to read the products
//注意using 语句的使用
using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_PRODUCTS_BY_CATEGORY, parm)) {
while (rdr.Read()) {
ProductInfo product = new ProductInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetString(4));
productsByCategory.Add(product);
}
}
return productsByCategory;
}
- DALFactory 和SQLServerDAL
- DALFactory
- DALFactory
- DALFactory出现"未能加载文件或程序集“Flashlm.SQLServerDAL”或它的某一个依赖项。系统找不到指定的文件”的解决方案
- 不使用DalFactory和IDAL,支持多种数据库应用
- Maticsoft.SQLServerDAL
- DALFactory--DataAccess---
- PDF.NET不使用DalFactory和IDAL支持多种数据库应用方案
- PetShop学习--SQLServerDAL
- PetShop学习--SQLServerDAL
- 八、创建DALFactory
- DALFactory有什么作用
- DALFactory有什么作用
- DALFactory有什么作用
- DALFactory有什么作用
- PetShop4中SQLServerDAL是如何引用的?
- BLL、DAL、IDAL、MODEL、DBUtility、DALFactory层级 (
- 深入宠物店PetShop-SQLServerDAL数据访问与Sample-Duwamish比较
- J2EE基础:j2ee所包含的13种核心技术
- FC6下,我的fstab配置
- 如何学j2ee
- KDE3简述(摘抄自《KDE综缆》)
- 迅驰四于昨日发布
- DALFactory 和SQLServerDAL
- ssl
- Socket连接出现大量的FIN_WAIT_2状态
- 迅驰4的功耗
- 二元查找树转变成排序的双向链表
- Java矢量画图工具
- 对话框中使用自定义的列表项(带RadioButton)
- Qt4中QCanvas的替代品
- Android