微软企业库5.0学习笔记(三十四)数据访问模块 各种获取数据的方式
2010-05-16 10:33
1131 查看
以对象形式从数据库获取数据
现代的很多编程技术都集中在“数据就是对象”这个概念。如果你在应用层之间使用Data Transfer Objects (DTOs)传输数据的话,这个方法就很有用,使用ORM实现一个数据访问层,或者是客户端查询技术,例如LINQ。
数据库访问模块实现了这个功能,允许你执行SQL或者是存储过程,可以返回一个对象序列,但是要求序列实现IEnumerable接口。
关于Accessors
模块提供了两个方法来实现这种查询要求,SprocAccessor和SqlStringAccessor。你可以使用Database类的ExecuteSprocAccessor和ExecuteSqlStringAccessor,或者是创建一个Accessor,然后调用它的Execute方法。
下面是一张查询示意图
代码
string oldDescription
= "Carries 4 bikes securely; steel construction, fits 2\" receiver hitch.";
string newDescription = "Bikes tend to fall off after a few miles.";
// Create command to execute the stored procedure and add the parameters.
DbCommand cmd = defaultDB.GetStoredProcCommand("UpdateProductsTable");
defaultDB.AddInParameter(cmd, "productID", DbType.Int32, 84);
defaultDB.AddInParameter(cmd, "description", DbType.String, newDescription);
// Execute the query and check if one row was updated.
if (defaultDB.ExecuteNonQuery(cmd) == 1)
{
// Update succeeded.
}
else
{
Console.WriteLine("ERROR: Could not update just one row.");
}
// Change the value of the second parameter
defaultDB.SetParameterValue(cmd, "description", oldDescription);
// Execute query and check if one row was updated
if (defaultDB.ExecuteNonQuery(cmd) == 1)
{
// Update succeeded.
}
else
{
Console.WriteLine("ERROR: Could not update just one row.");
}
未完待续。。。。。。。。。。。。。。。。。。。。。
现代的很多编程技术都集中在“数据就是对象”这个概念。如果你在应用层之间使用Data Transfer Objects (DTOs)传输数据的话,这个方法就很有用,使用ORM实现一个数据访问层,或者是客户端查询技术,例如LINQ。
数据库访问模块实现了这个功能,允许你执行SQL或者是存储过程,可以返回一个对象序列,但是要求序列实现IEnumerable接口。
关于Accessors
模块提供了两个方法来实现这种查询要求,SprocAccessor和SqlStringAccessor。你可以使用Database类的ExecuteSprocAccessor和ExecuteSqlStringAccessor,或者是创建一个Accessor,然后调用它的Execute方法。
下面是一张查询示意图
代码
string oldDescription
= "Carries 4 bikes securely; steel construction, fits 2\" receiver hitch.";
string newDescription = "Bikes tend to fall off after a few miles.";
// Create command to execute the stored procedure and add the parameters.
DbCommand cmd = defaultDB.GetStoredProcCommand("UpdateProductsTable");
defaultDB.AddInParameter(cmd, "productID", DbType.Int32, 84);
defaultDB.AddInParameter(cmd, "description", DbType.String, newDescription);
// Execute the query and check if one row was updated.
if (defaultDB.ExecuteNonQuery(cmd) == 1)
{
// Update succeeded.
}
else
{
Console.WriteLine("ERROR: Could not update just one row.");
}
// Change the value of the second parameter
defaultDB.SetParameterValue(cmd, "description", oldDescription);
// Execute query and check if one row was updated
if (defaultDB.ExecuteNonQuery(cmd) == 1)
{
// Update succeeded.
}
else
{
Console.WriteLine("ERROR: Could not update just one row.");
}
未完待续。。。。。。。。。。。。。。。。。。。。。
相关文章推荐
- 微软企业库5.0学习笔记(三十四)数据访问模块 各种获取数据的方式
- 微软企业库5.0学习笔记(三十四)数据访问模块 各种获取数据的方式
- 微软企业库5.0学习笔记(三十三)数据访问模块
- 微软企业库5.0学习笔记(三十五)数据访问模块 DataSet以及数据库事务
- 微软企业库5.0学习笔记(三十三)数据访问模块
- 微软企业库5.0学习笔记(三十三)数据访问模块
- 微软企业库5.0学习笔记(三十五)数据访问模块 DataSet以及数据库事务
- 微软企业库5.0学习笔记(三十五)数据访问模块 DataSet以及数据库事务
- 微软企业库4.1学习笔记(三十一)数据访问模块 简介
- 微软企业库4.1学习笔记(三十二)数据访问模块 在应用中使用数据访问模块
- 微软企业库5.0学习笔记实战数据验证模块
- 微软企业库5.0学习笔记(四十三)数据验证模块
- 微软企业库4.1学习笔记(三十二)数据访问模块 在应用中使用数据访问模块
- 微软企业库5.0学习笔记(四十四)实战数据验证模块
- 微软企业库5.0学习笔记(四十三)数据验证模块(转)
- 微软企业库4.1学习笔记(三十一)数据访问模块 简介
- 微软企业库5.0学习笔记实战数据验证模块----高级篇
- 微软企业库5.0学习笔记数据验证模块
- 微软企业库5.0学习笔记(四十五)实战数据验证模块----高级篇
- 微软企业库4.1学习笔记(三十二)数据访问模块 在应用中使用数据访问模块