找传奇、传世资源到传世资源站!

NBearLite数据持久化 demo源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

NBear是一个基于.Net 2.0、C#2.0开放全部源代码的的软件开发框架类库。

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using TestModel;using TestProfile;using System.Data;using NBearLite;using NBearLite.DbProviders;using NBearLite.Web;using System.Collections;using System.Text;namespace Test_Web{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { /* 1.在执行SQL时,会将转换后的SQL中的参数变为参数化SQL * */ //TestSelect(); TestUpdate(); } private void TestSelect() { Database db = Databases.NBBS; List<t_Board> resList = null; DataSet resSet = Databases.NBBS.Select(NBBS.Tables.t_Board).ToDataSet(); Console.WriteLine(resSet.Tables.Count); //t_Board t_Board_Model = Databases.NBBS.Select(NBBS.Tables.t_Board).Where(NBBS.Tables.t_Board.Board_ID == 14 && NBBS.Tables.t_Board.Board_Place > 5).ToSingleObject<t_Board>(); //Console.WriteLine(t_Board_Model.Board_Name); string cmdText = Databases.NBBS.Select(NBBS.Tables.t_Board, NBBS.Tables.t_Board.Board_ID.Max()).Where(NBBS.Tables.t_Board.Board_Name.Like("nba")).ToDbCommandText(); cmdText = Databases.NBBS.Select(NBBS.Tables.t_Board, NBBS.Tables.t_Board.Board_ID.Max().Alias("Max_Id")).Where(NBBS.Tables.t_Board.Board_Name.Like("nb%")).ToDbCommandText(); cmdText = Databases.NBBS.Select(NBBS.Tables.t_Board, NBBS.Tables.t_Board.Board_ID.Max()).Where(NBBS.Tables.t_Board.Board_Name.Like("nb%")).ToDbCommandText(); //object obj = Databases.NBBS.Select(NBBS.Tables.t_Board, NBBS.Tables.t_Board.Board_ID.Max()).Where(NBBS.Tables.t_Board.Board_Name.Like("nb%")).ToDbCommand(); //obj = Databases.NBBS.Select(NBBS.Tables.t_Board, NBBS.Tables.t_Board.Board_ID.Max()).Where(NBBS.Tables.t_Board.Board_Name.Like("nb%")).ToDataReader(); int max_Board_ID = Databases.NBBS.Select(NBBS.Tables.t_Board, NBBS.Tables.t_Board.Board_ID.Max()).Where(NBBS.Tables.t_Board.Board_Name.Like("nb%")).ToScalar<int>();//返回单个字段时,不要用ToList方法 string cmdText2 = Databases.NBBS.Select(NBBS.Tables.t_Board, NBBS.Tables.t_Board.Board_ID.Max(), QueryColumn.All()).Where(NBBS.Tables.t_Board.Board_Name == 2).ToDbCommandText(); resList = db.Select(NBBS.Tables.t_Board, NBBS.Tables.t_Board.Board_Name).GroupBy(NBBS.Tables.t_Board.Board_Name).OrderBy(NBBS.Tables.t_Board.Board_Name.Desc).SetSelectRange(2, 2, NBBS.Tables.t_Board.Board_Name).ToList<t_Board>(); ShowListCount(resList); //级联查询 resSet = db.Select(NBBS.Tables.t_Board, QueryColumn.All(NBBS.Tables.t_Board), NBBS.Tables.t_MotherBoard.MotherBoard_Name) .LeftJoin(NBBS.Tables.t_MotherBoard, NBBS.Tables.t_MotherBoard.MotherBoard_ID == NBBS.Tables.t_Board.MotherBoard_ID) .OrderBy(NBBS.Tables.t_Board.Board_ID.Desc).ToDataSet(); ShowDataSet(resSet); #region MyRegion //ds = db.Select(Northwind.Categories, QueryColumn.All(Northwind.Categories), QueryColumn.All(Northwind.Products)) // .Join(Northwind.Products, Northwind.Products.CategoryID == Northwind.Categories.CategoryID) // .Where(Northwind.Categories.CategoryName.Length > 3 && Northwind.Products.UnitPrice > 100) // .OrderBy(Northwind.Categories.CategoryID.Asc, Northwind.Products.ProductID.Desc) // .ToDataSet(); //ds = db.Select(Northwind.Categories, QueryColumn.All(Northwind.Categories), QueryColumn.All(Northwind.Products)) // .LeftJoin(Northwind.Products, Northwind.Products.CategoryID == Northwind.Categories.CategoryID) // .Where(Northwind.Categories.CategoryName.Length > 3 && Northwind.Products.UnitPrice > 100) // .OrderBy(Northwind.Categories.CategoryID.Asc, Northwind.Products.ProductID.Desc) // .ToDataSet(); //ds = db.Select(Northwind.Categories, QueryColumn.All(Northwind.Categories), QueryColumn.All(Northwind.Products)) // .RightJoin(Northwind.Products, Northwind.Products.CategoryID == Northwind.Categories.CategoryID) // .Where(Northwind.Categories.CategoryName.Length > 3 && Northwind.Products.UnitPrice > 100) // .OrderBy(Northwind.Categories.CategoryID.Asc, Northwind.Products.ProductID.Desc) // .ToDataSet(); //ds = db.Select(Northwind.Categories, Northwind.Categories.__Alias("CategoriesAlias").CategoryName).Join(Northwind.Categories, "CategoriesAlias", Northwind.Categories.CategoryID == Northwind.Categories.__Alias("CategoriesAlias").CategoryID). // SetSelectRange(2, 2, Northwind.Categories.CategoryID).Where(Northwind.Categories.CategoryName.Length > 0 && Northwind.Categories.__Alias("CategoriesAlias").Description != null). // ToDataSet(); //int pageSize = 10; //int rowCount = (int)db.Select(Northwind.Categories, QueryColumn.All().Count()).ToScalar(); //int pageCount = (rowCount % 10 == 0 ? rowCount / 10 : (rowCount / 10) 1); //Console.WriteLine(string.Format("pageSize={0}, rowCount={1}, pageCount={2}", pageSize, rowCount, pageCount)); ////test select tosingleobject and tolist //_Category cat = db.Select(Northwind.Categories).ToSingleObject<_Category>(); ////Dictionary<string, object> dic = db.Select(Northwind.Categories).ToSingleObject<Dictionary<string, object>>(); ////Hashtable hash = db.Select(Northwind.Categories).ToSingleObject<Hashtable>(); ////NameValueCollection nameval = db.Select(Northwind.Categories).ToSingleObject<NameValueCollection>(); //_Category[] cats = db.Select(Northwind.Categories).ToArray<_Category>(); //NameValueCollection[] namevals = db.Select(Northwind.Categories).ToList<NameValueCollection>(); #endregion } private void ShowListCount(IList list) { if(list!=null) Console.WriteLine(list.Count); } private void ShowDataSet(DataSet resSet) { if (resSet == null || resSet.Tables.Count == 0) return; DataTable dt = resSet.Tables[0]; StringBuilder html = new StringBuilder(); StringBuilder html2 = new StringBuilder(); for (int j = 0; j < dt.Rows.Count; j ) { for (int i = 0; i < dt.Columns.Count; i ) { if (j == 0) { html2.Append(dt.Columns[i].ColumnName "<span style='margin-left:20px;'/>"); } html.Append(dt.Rows[j][i] "<span style='margin-left:20px;'/>"); } html.Append("<br/>"); } html2.Append("<br/>"); Response.Write(html2.ToString()); Response.Write(html.ToString()); } private void TestUpdate() { try { string cmdText = Databases.NBBS.Update(NBBS.Tables.t_Board).AddColumn(NBBS.Tables.t_Board.Board_Name, NBBS.Tables.t_Board.Board_Name "_" DateTime.Now).Where(NBBS.Tables.t_Board.Board_ID == null).ToDbCommandText(); int n = Databases.NBBS.Update(NBBS.Tables.t_Board).AddColumn(NBBS.Tables.t_Board.Board_Name, NBBS.Tables.t_Board.Board_Name "_" DateTime.Now).Where(NBBS.Tables.t_Board.Board_ID == 8).Execute(); n = Databases.NBBS.Update(NBBS.Tables.t_Board).AddColumn(NBBS.Tables.t_Board.Board_Name, NBBS.Tables.t_Board.Board_Name "_" DateTime.Now).Where(NBBS.Tables.t_Board.Board_ID == "7").Execute(); n = Databases.NBBS.Update(NBBS.Tables.t_Board).AddColumn(NBBS.Tables.t_Board.Board_Name, NBBS.Tables.t_Board.Board_Name "_" DateTime.Now).Where(NBBS.Tables.t_Board.Board_ID == "这不是数字").Execute();//此update 将会报错,类型不匹配 } catch (Exception ex) { throw ex; } } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复