实例介绍
【实例截图】
【核心代码】
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;
}
}
}
}
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论