使用NSun(架構)快速開發項目
NSun簡介:
NSun基于.Net 2.0 快速開發架構設計,使用NbearLite作為基礎進行二次封裝,加入對實體的操作。
釋出網站:
http://www.cnblogs.com/duanseven (對于NSun的更新以及教程)
核心DLL:
NSun.Core.dll
NBearLite.dll
NBear.Core.dll
下載下傳位址:
NSunSample.rar(執行個體)
nsun.rar(引用)
Nbearlite查詢:
http://www.cnblogs.com/teddyma(參考teddyma相關Nbearlite文章以及相關幫助下載下傳)
以下方法都有相關條件的重載:
添加操作:
public int AddDept(DeptInfo info)
{
return DBQuery<DeptInfo>.Default.Save(Sample.Dept, info);
}
修改操作:
public int ModifyDept(DeptInfo info)
{
return DBQuery<DeptInfo>.Default.Save(Sample.Dept, info);
}
删除:
主鍵
public int DeleteDept(object key)
{
return DBQuery<DeptInfo>.Default.Delete(Sample.Dept, key);
}
對象
public int DeleteDept(DeptInfo info)
{
return DBQuery<DeptInfo>.Default.Delete(Sample.Dept,info);
}
傳回資料:
public IList<DeptInfo> GetAll()
{
return DBQuery<DeptInfo>.Default.SelectToIList(Sample.Dept);
}
組織查詢進行傳回資料
public IList<DeptInfo> GetAll(SelectSqlSection select)
{
return DBQuery<DeptInfo>.Default.SelectToIList(select);
}
傳回單獨實體:
public DeptInfo GetFirst(int id)
{
return DBQuery<DeptInfo>.Default.SelectToEntity(Sample.Dept, id);
}
分頁查詢:
public DataTable GetAll(SelectSqlSection section, int pagesize, int page, out int count)
{
return DBQuery<UsersInfo>.Default.SelectPageToDataTable(section, pagesize, page, Sample.Users.id, out count);
}
事務使用:
public int Add(UsersInfo info, ClientInfo info2)
{
info.noPersistence();
using (DbTransaction tran = DBQuery<ClientInfo>.Default.GetDbTransaction())
{
try
{
int cid = DBQuery<ClientInfo>.Default.SaveReturnIDEntity(Sample.Client, info2, tran);
info.cid = cid;
DBQuery<UsersInfo>.Default.Save(Sample.Users, info, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
}
finally
{
tran.Dispose();
}
return 1;
}
}
使用存儲過程:
DeptInfo info=
DBQuery<DeptInfo>.Default.StoredProcedureToEntity("getdept");
使用自定義sql語句:
DeptInfo info=
DBQuery<DeptInfo>.Default.CustomSqlToEntity("select top 1 * from dept");
NSun 5分鐘使用說明:
1、進行實體生成:
解壓nsun.rar 出現nsun檔案夾 SumSqlToEntity.exe,輕按兩下執行。
填入伺服器以及登入名和密碼點選登入進入。

圖1
進入主界面,在路徑框中填寫生成存放路徑,在命名空間中填寫項目的命名空間,
連接配接伺服器按鈕是對伺服器重新選擇連接配接。
圖2
主窗體左面的表選擇,進入可以選擇資料庫中所要生成的實體類,輕按兩下進行選擇
圖3
選擇後可以在上方看到所選的表
圖4
選擇好要生成的表後點選生成實體按鈕。提示生成成功!
圖5
會在所填目錄下以資料庫名稱命名的檔案夾中出現2個cs檔案
圖6
放入所在填命名空間的項目中。在實體類庫中引用Nbearlite.dll和NSun.Core.dll,就可以了。
2、進行config配置。
在web.config或者app.config中配置 connectionStrings 節點
<connectionStrings>
<add name="DB" connectionString="server=192.168.1.57\sqlexpress;database=nsunsample;uid=sa;pwd=tiger"/>
</connectionStrings>
進行資料庫連接配接串配置
DBQuery<DeptInfo> db= DBQuery<DeptInfo>.Default;(取得預設的連接配接字元串)
DBQuery<DeptInfo> db = new DBQuery<DeptInfo>("DB");(取得指定名稱的連接配接字元串)
DBQuery<DeptInfo> db = new DBQuery<DeptInfo>(SunDBType.Sqlserver9,"server=192.168.1.57\sqlexpress;database=nsunsample;uid=sa;pwd=tiger");(這裡可以是從配置檔案中解密讀出來的來連接配接字元串)
這樣就可以從db中調用NSun的方法了。
更多操作詳解NSunSample.rar。