基本信息
源码名称:NBearLite数据持久化 demo源码
源码大小:1.12M
文件格式:.rar
开发语言:C#
更新时间:2013-08-04
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
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;
            }
        }
    }
}