查询 SubSonic2.1版本 – 例出3种查询。 Product product = new Select().From<Product>() .Where(Product.ProductIDColumn).IsEqualTo(4) .ExecuteSingle<Product>(); Product product = DB.Select().From<Product>() .Where("ProductID").IsEqualTo(4) .ExecuteSingle<Product>(); Query query = new Query(Product.Schema); query.WHERE("ProductID=4").ORDER_BY("ProductID asc"); IDataReader dr = Product.FetchByQuery(query); DataSet product = new Query(Product.Schema).WHERE("ProductID=4"). ORDER_BY("Prod uctID asc").ExecuteDataSet(); 关联查询 DataSet product = new Select( Product.ProductNameColumn, Category.CategoryNameColumn ).From<Product>() .InnerJoin(Category.CategoryIDColumn, Product.CategoryIDColumn) .Where(Category.CategoryIDColumn).IsGreaterThan(4) .ExecuteDataSet(); 注意. InnerJoin(f2, f1) 的使用方法。 分页查询 List<Product> products = new Select().From<Product>() .Where(Product.ProductIDColumn).IsEqualTo(4) .Paged(1, 30) .ExecuteTypedList<Product>(); 分组 SubSonic.SqlQuery query = DB.Select(Aggregate.GroupBy("CompanyName")) .From("Customers") .LeftOuterJoin("Orders"); 查询—返回对象 ExecuteReader(); 返回DataReader ExecuteScalar(); 返回对象 ExecuteScalar<string>(); 返回泛型对象 ExecuteSingle<Product>(); 返回表实体对象 ExecuteTypedList<Product>(); 返回泛型表实休数据集 ExecuteDataSet(); 返回DataSet ExecuteJoinedDataSet<强数型数据集>(); 返回关联查询 DataSet Execute(); 返回执行后,数据更新条数 SQL关键词 IsEqualTo(obj) // 等于 value IsBetweenAnd(obj1, obj2) // [字段1] BETWEEN 值1 AND 值2 StartsWith // LIEK '1%‘ EndsWith // LIEK '%1‘ IsGreaterThan // [字段1] > 值1 IsGreaterThanOrEqualToIsGreaterThan // [字段1] >= 值1 IsLessThan // [字段1] < 值1 IsLessThanOrEqualToIsLessThan // [字段1] <= 值1 WhereExpression / AndExpression // Expression 表示括号 C#: .Where("1").IsGreaterThan(1) .And("2").IsGreaterThanOrEqualTo(2) .AndExpression("3").IsLessThan(3) .AndExpression("4").IsLessThanOrEqualTo(4).And("5").StartsWith("5") .AndExpression("6").EndsWith("6") .ExecuteSingle<Product>(); SQL:WHERE 1 > @1AND 2 >= @2 AND (3 < @3) AND (4 <= @4 AND 5 LIKE @5) AND (6 LIKE @6) 修改 Product product = new Product(4); product.ProductName = "amo"; product.Save(); int i = new Update(Product.Schema) .Set(Product.ProductNameColumn).EqualTo("Chai -- Amo") .Where(Product.ProductIdColumn).IsEqualTo(1) .Execute(); 添加 Product product = new Product(); product.ProductName = "my xiaoli"; product.SupplierID = 1; product.CategoryID = 1; product.Discontinued = true; //............ product.Save(); int i = new Insert().Into(Product.Schema, "ProductName", "SupplierID", "CategoryID", "Discontinued“ ) .Values("my amo", 1, 1, 1).Execute(); 删除 int i = new Delete ().From<Product>() .Where(Product.ProductIDColumn). IsEqualTo(1) .Execute(); //删除 Query qr = EaiSmsCustom.Query().AddWhere("age", 1).AddWhere("name", 2); qr.QueryType = QueryType.Delete; qr.Execute(); 事务1 List<Insert> queries = new List<Insert>(); queries.Add(new Insert().Into(Product.Schema).Values("product1")); queries.Add(new Insert().Into(Product.Schema).Values("product2")); queries.Add(new Insert().Into(Product.Schema).Values("product3")); queries.Add(new Insert().Into(Product.Schema).Values("product4")); queries.Add(new Insert().Into(Product.Schema).Values("product5")); queries.Add(new Insert().Into(Product.Schema).Values("product6")); queries.Add(new Insert().Into(Product.Schema).Values("product7")); //execute in a transaction SqlQuery.ExecuteTransaction(queries); 事务2 using (SharedDbConnectionScope sp = new SharedDbConnectionScope()) { using (TransactionScope scope = new TransactionScope()) { // coding.... scope.Complete(); } } 减少对ActiveRecord的依赖 MyProduct product = new Select( Product.ProductNameColumn, Category.CategoryNameColumn ).From<Product>() .InnerJoin(Category.CategoryIDColumn, Product.CategoryIDColumn) .Where(Category.CategoryIDColumn).IsGreaterThan(4) .ExecuteSingle<MyProduct>(); string cname = product.CategoryName; string pname = product.ProductName; 自定义MyProduct实体类 public class MyProduct : ActiveRecord<MyProduct> { public string CategoryName { get { return GetColumnValue<string>(Category.Columns.CategoryName); } set { SetColumnValue(Category.Columns.CategoryName, value); } } public string ProductName { get { return GetColumnValue<string>(Product.Columns.ProductName); } set { SetColumnValue(Product.Columns.ProductName, value); } } } string str=DB.Select(EaiService.NameColumn).Top("1").From<EaiService>.Where(EaiService.NameColumn).IsEquealTo(name).ExecuteScalar<string>(); string str = DB.Select(Aggregate.Count(EaiUserMap.ApplicationColumn, "Count")).From<EaiUserMap>().ExecuteScalar<string>(); EaiService es = DB.Select().From<EaiService>() .Where(EaiService.ApplicationColumn).IsEqualTo(title) .ExecuteSingle<EaiService>(); if (es != null) { bt = es.ServiceNameS.ToString(); } //删除 Query qr = EaiSmsCustom.Query().AddWhere("age", 1).AddWhere("name", 2); qr.QueryType = QueryType.Delete; qr.Execute(); 存储过程调用 以下是利用SubSonic2.0使用该存储过程的代码: -----------------------------------代码开始------------------------------------ public static int GetRUID(string tblName) { StoredProcedure spd = new StoredProcedure("GenerateRUID"); spd.Command.AddParameter("@tblName", tblName); spd.Command.AddOutputParameter("@currentnumber"); spd.Execute(); int currentnumber = int.Parse(spd.OutputValues[0].ToString()); return currentnumber; } Subsonic的使用之基本语法、操作(2) 新增、修改: Subsonic中的新增和修改可以共享一个方法(InsertOrUpdate): user.InsertOrUpdate(); //其中user是传入的用户实体 return user.Fid; 当然它们也有自己独立的方法: ² 新增: notice.Save();//其中notice是传入的公告实体 return NoticeId = notice.Fid; ² 修改 int i = new Update(CyReceiver.Schema) .Set(CyReceiver.FStateColumn).EqualTo(1) .Where(CyReceiver.FNoticeIDColumn).IsEqualTo(noticeId) .And(CyReceiver.FUserIDColumn).IsEqualTo(userId).Execute(); 删除: DB.Delete().From(CyUser.Schema).Where(CyUser.FidColumn).IsEqualTo(id).Execute(); SQL关键词: Select、Delete、Insert、Update、Form、Where这些和SQL一样的就不解释了 IsEqualTo:where条件中的“等于” 如:Where(CyReceiver.FNoticeIDColumn).IsEqualTo(noticeId) IsNotEqualTo: where条件中的“不等于” EqualTo:set语句中的“等于” 如: Set(CyReceiver.FStateColumn).EqualTo(1) IsGreaterThan :where中的“大于” IsGreaterThanOrEqualTo:大于等于 IsLessThan:where中的“小于” IsLessThan OrEqualTo:小于等于 IsBetweenAnd:区间 如:Where(CyNotice.FBeginDateTimeColumn).IsBetweenAnd(beginDate, endDate) OrderAsc:按某字段升序排序 如:OrderAsc(CyUser.Columns.FUsersSort) OrderDesc: 按某字段降序序排序 And:逻辑中的“且” Or: 逻辑中的“或” Like:模糊查询关键词 如:And(CyNotice.FTitleColumn).Like("%" + model.Title + "%") 更常用的是:title=string.Format("%{0}%", model.Title); And(CyNotice.FTitleColumn).Like(title); LeftOuterJoin:左外连接 (当然还有RightOuterJoin、Joins、LeftInnerJoin等等) 如:From(CyNotice.Schema) .LeftOuterJoin(CyUser.FidColumn, CyNotice.FUserIDColumn) 简单的Update使用save方法 没有用到update: Product product =new Product("Id",1); product.Name="Eason"; product.save(); 复杂一点的,就要用到 Update : Update product= new Update(product.Schema.TableName); product.From(product.Schema.TableName); product.Where(product.Columns.id).Like(1); product.And(product.Columns.Name).Like(“Eason”); product.Set(product.Columns.Name).EqualTo(“Hanson Eason”); product.Execute();