只需要写存储过程,其他的事情都交给Ng.Net--Ng.Net生成物操作数据库
一、预备知识
1)NgBook有三张表 Admin,Books,Replays(数据库结构见:http://www.cnblogs.com/adgjcn/archive/2009/04/25/1443408.html)
通过Ng.Net代码生成器(解决方案生成工具)生成后的生成物中实体层如下:
NgBook.Entities.Admin 管理员实体类
NgBook.Entities.Book 留言实体类
NgBook.Entites.Replay 留言回复实体类
NgBook项目下载地址:http://ngbook.ngadget.net
二、演示Ng.Net生成物如何操作数据库
1)往数据库中 Admins 表插入一条记录[非事务],通过对象操作,实例代码以及执行结果如下:
NgBook.Entities.Admin admin = new NgBook.Entities.Admin();
admin.ThemForld = "0";
admin.UserName = "ngadmin";
admin.UserPassword = NGadget.Common.Security.MD5.EnMD5("admin");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
admin.ID = Convert.ToInt32(adminProcess.Insert(admin));
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//记录日志
admin.ID = 0;//表示插入失败
tm.SetValue("operMess1", "插入失败"+ex.Message);
NGadget.Common.Log.ApplicationLog.WriteLine(ex.Message);
}
if (admin.ID > 0) //插入成功
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
tm.SetValue("operMess1", "插入成功=新产生的Id是="+admin.ID.ToString());
}
2)往数据库中 Admins 表插入一条记录[非事务],不通过对象操作,实例代码以及执行结果如下:
NgBook.Entities.Admin admin = new NgBook.Entities.Admin();
//操作返回值
int insertedKeyID = 0;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
insertedKeyID = Convert.ToInt32(adminProcess.Insert("ngadmin",NGadget.Common.Security.MD5.EnMD5("admin"),"0"));
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//记录日志
NGadget.Common.Log.ApplicationLog.WriteLine(ex.Message);
tm.SetValue("operMess2", "插入失败" + ex.Message);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
if (insertedKeyID > 0) //插入成功
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
tm.SetValue("operMess2", "插入成功=新产生的Id是=" + insertedKeyID.ToString());
}
3)
演示跨方法事务处理
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
===================提交事务================
using (NGadget.Transaction.LocalTransaction lt = new NGadget.Transaction.LocalTransaction("ngbook"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//向 Admins 表插入数据
adminProcess.Insert(lt, "admin", NGadget.Common.Security.MD5.EnMD5("admin"), "0");
//向 Books 表中插入数据
bookProcess.Insert(lt, "gg", "12121212", "122121", "221121", "121212", "121221","ddd", System.DateTime.Now, 1, false);
//提交事务
lt.Commit();
}
catch(Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//回滚事务
lt.Rollback();
NGadget.Common.Log.ApplicationLog.WriteLine(ex.Message);
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//销毁事务
lt.Dispose();
}
}
//===================不提交事务================
using (NGadget.Transaction.LocalTransaction lt = new NGadget.Transaction.LocalTransaction("ngbook"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//向 Admins 表插入数据
adminProcess.Insert(lt, "admin", NGadget.Common.Security.MD5.EnMD5("admin"), "0");
//向 Books 表中插入数据
bookProcess.Insert(lt, "gg", "12121212", "122121", "221121", "121212", "121221","ddd", System.DateTime.Now, 1, false);
throw new Exception("添加了一个错误");
//提交事务
lt.Commit();
}
catch(Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//回滚事务
lt.Rollback();
NGadget.Common.Log.ApplicationLog.WriteLine(ex.Message);
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//销毁事务
lt.Dispose();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
三、实体类操作数据库扩展
1. 实体.GetListsByProcName(...) ;//通过存储过程到实体对应表获取多条记录,不可扩展属性.事务方法(非)
2. 实体.GetListsByProcNameExAttr(...) ;//通过存储过程到实体对应表获取多条记录,可扩展属性.事务方法(非)
3. 实体.GetOneObjectByProcName(...);//通过存储过程到实体对应数据库表中获取一条记录,不可扩展属性.事务方法(非)
4. 实体.GetOneObjectByProcNameExAttr(...);//通过存储过程到实体对应数据库表中获取一条记录,可扩展属性.事务方法(非)
5. 实体.ExecuteProcScalar(...);//执行存储过程返回一行一列的结果集,事务方法(非)
6. 实体.ExecuteProcNonQuery(...);//执行存储过程不返回任何值,事务方法(非)
7. 实体.GetListsBySqlText(...);//可通过sql语句,带参数的sql语句,存储过程3种方式到实体类对应的数据表中获取多条记录,不可扩展属性.事务方法(非)
8. 实体.GetListsBySqlTextExAttr(...);//可通过sql语句,带参数的sql语句,存储过程3种方式到实体类对应的数据表中获取多条记录,可扩展属性.事务方法(非)
9. 实体.GetOneObjectBySqlText(...);//可通过sql语句,带参数的sql语句,存储过程3种方式到实体类对应的数据表中获取一条记录,不可扩展属性.事务方法(非)
10. 实体.GetOneObjectBySqlTextExAttr(...);//可通过sql语句,带参数的sql语句,存储过程3种方式到实体类对应的数据表中获取一条记录,可扩展属性.事务方法(非)
11. 实体.ExecuteSqlTextScalar(...);//可通过sql语句,带参数的sql语句,存储过程3种方式执行sql语句返回一行一列的结果集,事务方法(非)
12. 实体.ExecuteProcNonQuery(...);//可通过sql语句,带参数的sql语句,存储过程3种方式执行sql语句不返回任何结果集,事务方法(非)
真正达到用户只需要编写成出过程的目的。
详细操作请见NgBook项目中Example.aspx、PostBook.aspx、Index.aspx
Ng.Net解决方案生成工具(C#代码生成器)已经有715人下载,在此感谢各位的使用,有您们的使用我想Ng.Net解决方案生成工具会越来越完善,谢谢^_^
----------------------------------------------
NgBook下载地址:http://ngbook.ngadget.net/
Ng.Net官方网站:http://www.ngadget.net/