public void Batch_Should_Execute_Reader_And_Return_Typed_Lists_Using_Linq() { BatchQuery qry = new BatchQuery(provider); var pquery = new Query <Product>(provider); qry.Queue(from p in pquery where p.ProductID == 1 select p); qry.Queue(from p in pquery where p.ProductID == 2 select p); qry.Queue(from p in pquery where p.ProductID == 3 select p); bool canRead = false; List <Product> result1 = null; List <Product> result2 = null; List <Product> result3 = null; using (IDataReader rdr = qry.ExecuteReader()) { result1 = rdr.ToList <Product>(); canRead = true; if (rdr.NextResult()) { result2 = rdr.ToList <Product>(); } if (rdr.NextResult()) { result3 = rdr.ToList <Product>(); } canRead = rdr.NextResult(); } Assert.True(result1.Count > 0); Assert.True(result2.Count > 0); Assert.True(result3.Count > 0); Assert.False(canRead); }
public void Batch_Should_Execute_Reader_And_Return_Typed_Lists() { BatchQuery qry = new BatchQuery(provider); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(1)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(2)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(3)); int sets = 1; bool canRead = false; List <Product> result1 = null; List <Product> result2 = null; List <Product> result3 = null; using (IDataReader rdr = qry.ExecuteReader()) { result1 = rdr.ToList <Product>(); canRead = true; if (rdr.NextResult()) { result2 = rdr.ToList <Product>(); } if (rdr.NextResult()) { result3 = rdr.ToList <Product>(); } canRead = rdr.NextResult(); } Assert.True(result1.Count > 0); Assert.True(result2.Count > 0); Assert.True(result3.Count > 0); Assert.False(canRead); }
public void Batch_Should_Execute_Batched_SQL_With_Replaced_Command_Parameters_For_In_Operator() { BatchQuery qry = new BatchQuery(provider); qry.Queue(new Select(provider).From("Products").Where("ProductID").In(new int[] { 1, 2, 3 })); qry.Queue(new Select(provider).From("Products").Where("ProductID").In(new int[] { 1, 2, 3 })); qry.Queue(new Select(provider).From("Products").Where("ProductID").In(new int[] { 1, 2, 3 })); int sets = 1; bool canRead = false; using (IDataReader rdr = qry.ExecuteReader()) { canRead = true; if (rdr.NextResult()) { sets = 2; } if (rdr.NextResult()) { sets = 3; } canRead = rdr.NextResult(); } Assert.Equal(3, sets); Assert.False(canRead); }
public void Batch_Should_Execute_Reader() { BatchQuery qry = new BatchQuery(provider); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(1)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(2)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(3)); int sets = 1; bool canRead = false; using (IDataReader rdr = qry.ExecuteReader()) { canRead = true; if (rdr.NextResult()) { sets = 2; } if (rdr.NextResult()) { sets = 3; } canRead = rdr.NextResult(); } Assert.Equal(3, sets); Assert.False(canRead); }
public void Queue <T>(IQueryable <T> qry) { if (_batch == null) { _batch = new BatchQuery(Provider, QueryProvider); } _batch.Queue(qry); }
public void Batch_Should_Build_Batched_SQL_With_Replaced_Command_Parameters() { BatchQuery qry = new BatchQuery(provider); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(1)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(2)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(3)); QueryCommand cmd = qry.GetCommand(); Assert.Equal("@0", cmd.Parameters[0].ParameterName); Assert.Equal("@1", cmd.Parameters[1].ParameterName); Assert.Equal("@2", cmd.Parameters[2].ParameterName); }
public void Batch_Should_Build_Batched_SQL() { BatchQuery qry = new BatchQuery(provider); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(1)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(2)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(3)); string sql = qry.BuildSqlStatement(); Assert.Equal(3, sql.FindMatches("SELECT").Count); }
public void Batch_Should_Build_Batched_SQL_With_Replaced_SQL_Parameters() { BatchQuery qry = new BatchQuery(provider); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(1)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(2)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(3)); string sql = qry.BuildSqlStatement(); Assert.True(sql.FindMatches("@0").Count == 1); Assert.True(sql.FindMatches("@1").Count == 1); Assert.True(sql.FindMatches("@2").Count == 1); }
public void Batch_Should_Build_Batched_SQL_Using_Linq() { BatchQuery qry = new BatchQuery(provider); var pquery = new Query <Product>(provider); qry.Queue(from p in pquery where p.ProductID == 1 select p); qry.Queue(from p in pquery where p.ProductID == 2 select p); qry.Queue(from p in pquery where p.ProductID == 3 select p); string sql = qry.BuildSqlStatement(); Assert.Equal(3, sql.FindMatches("SELECT").Count); }
/// <summary> /// Adds a bunch of T items ///</summary> public void Add(IEnumerable <T> items, IDataProvider provider) { BatchQuery bQuery = new BatchQuery(provider); foreach (T item in items) { var query = item.ToInsertQuery(provider); bQuery.Queue(query); } bQuery.Execute(); }
/// <summary> /// Deletes the passed-in T items /// </summary> public int Delete(IEnumerable <T> items, IDataProvider provider) { BatchQuery bQuery = new BatchQuery(provider); int result = 0; foreach (T item in items) { var query = item.ToDeleteQuery(provider); bQuery.Queue(query); } result = bQuery.Execute(); return(result); }
public void Batch_Query_Should_Allow_Single_Quotes_In_Query() { var containsNames = new[] { "Products's One and Only", "Products's Second" }; Query <Product> products = new Query <Product>(provider); var query = from product in products where containsNames.Contains(product.ProductName) select product; var result = query.ToList(); BatchQuery batch = new BatchQuery(provider); batch.Queue(query); Assert.DoesNotThrow(() => batch.Execute()); }
private static void BatchInsertInto() { Console.WriteLine("Batch Inserting Genres using Insert.Into with unescaped values."); IList<Genre> genres = new List<Genre> { new Genre { Name = "Genre's Third" }, new Genre { Name = "Genre's Fourth" } }; BatchQuery bq = new BatchQuery(db.Provider); foreach (Genre genre in genres) { bq.Queue(db.Insert.Into<Genre>(g => g.Name).Values(genre.Name)); } ExecuteQuery(bq); }
public void Batch_Should_Execute_Reader_And_Return_Typed_Lists() { BatchQuery qry = new BatchQuery(provider); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(1)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(2)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(3)); bool canRead = false; List<Product> result1 = null; List<Product> result2 = null; List<Product> result3 = null; using(IDataReader rdr = qry.ExecuteReader()) { result1 = rdr.ToList<Product>(); canRead = true; if(rdr.NextResult()) result2 = rdr.ToList<Product>(); if(rdr.NextResult()) result3 = rdr.ToList<Product>(); canRead = rdr.NextResult(); } Assert.True(result1.Count > 0); Assert.True(result2.Count > 0); Assert.True(result3.Count > 0); Assert.False(canRead); }
protected void Page_Load(object sender, EventArgs e) { //--------------------------------------------------------- // 1、新增记录 //--------------------------------------------------------- var branch = new Branch(); branch.DeptCode = SPs.P_Branch_GetMaxBranchDeptCode(2, 1).ExecuteScalar() + ""; branch.Name = "客服部"; branch.Comment = "客户售后服务维护部门"; branch.ParentId = 1; branch.Sort = 7; branch.Depth = 2; //AddDate这个属性不用赋值,ORM框架生成更新语句时会自动过滤该字段,更新时将会使用数据库中设置的默认值 //branch.AddDate = DateTime.Now; //保存数据入库 branch.Save(); WriteLine("记录添加成功,新增Id为:" + branch.Id); //---------------------------------------------------------- // 2、修改记录 //---------------------------------------------------------- var branchModel = Branch.SingleOrDefault(x => x.Id == branch.Id); //也可以这样用 //var branchModel = new Branch(x => x.Id == branch.Id); //打印读取出来的数据 WriteLine(branchModel.ToString()); //修改名称为“售后服务部” branchModel.Name = "售后服务部"; //保存 branchModel.Save(); //重新从数据库中读取出来并打印到输出窗口 WriteLine((new Branch(x => x.Id == branch.Id)).ToString()); //---------------------------------------------------------- // 3、判断刚刚修改的记录是否存在 //---------------------------------------------------------- if (Branch.Exists(x => x.Id == branch.Id)) { WriteLine("Id为" + branch.Id + "的记录存在!"); } else { WriteLine("Id为" + branch.Id + "的记录不存在!"); } //--------------------------------------------------------- // 4、删除记录 //--------------------------------------------------------- //删除当前记录 Branch.Delete(x => x.Id == branch.Id); //也可以这样操作: //branchModel.Delete(); WriteLine("删除Id为" + branch.Id + "的记录成功!"); //-------------------------------------------------------- // 5、判断刚刚删除的记录是否存在 //-------------------------------------------------------- if (Branch.Exists(x => x.Id == branch.Id)) { WriteLine("Id为" + branch.Id + "的记录存在!"); } else { WriteLine("Id为" + branch.Id + "的记录不存在!"); } //-------------------------------------------------------- // 6、使用类实体附带的函数查询 //-------------------------------------------------------- //查找出所有记录 -- 使用All() var list = Branch.All(); foreach (var item in list) { //打印记录到输出窗口 WriteLine(item.ToString()); } //查询指定条件的记录 -- 使用Find() IList <Branch> il = Branch.Find(x => x.Id > 2); foreach (var item in il) { WriteLine(item.ToString()); } //获取第二页记录(每页3条记录) il = Branch.GetPaged("Id Asc", 2, 3); foreach (var item in il) { WriteLine(item.ToString()); } //使用Id倒序排序,获取第三页记录(每页3条记录) il = Branch.GetPaged("Id Desc", 3, 3); foreach (var item in il) { WriteLine(item.ToString()); } //-------------------------------------------------- // 7、使用Select类查询 //-------------------------------------------------- //创建Select对象 //var select = new Select(); //显示指定的列 var select = new Select(new string[] { BranchTable.Id, BranchTable.Name, BranchTable.DeptCode }); //指定查询表 select.From <Branch>(); //运行完上面这条语句后,SQL已经生成出来了,在Debug状态将鼠标指向select就可以看到,往下继续执行时,每添加一个属性都会添加在生成的SQL语句中 //添加查询条件 -- Id大于2且编号头两位为01的部门 select.Where(BranchTable.Id).IsGreaterThanOrEqualTo(2).And(BranchTable.DeptCode).StartsWith("01"); //查询时括号添加列子 //select.OpenExpression().Where("").IsEqualTo(0).Or("").IsEqualTo(11).CloseExpression().And("").IsEqualTo(3); //设置去重复 -- SubSonic没有去重复选项,需要自己手动修改DLL源码 select.Distinct(true); //或 //select.IsDistinct = true; //设置查询数量 select.Top("5"); //添加排序 -- 倒序 select.OrderDesc(BranchTable.Sort); //或 //List<string> orderByList = new List<string>(); //orderByList.Add(BranchTable.Sort + " Desc"); //orderByList.Add(BranchTable.Id + " Desc"); //select.OrderBys = orderByList; //设置分页,获取第一页记录(每页10条记录) select.Paged(1, 10); //执行查询 -- 返回DataTable var dt = select.ExecuteDataTable(); if (dt != null && dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { WriteLine(dt.Rows[i][BranchTable.Id] + " " + dt.Rows[i][BranchTable.Name]); } } //执行查询 -- 返回List var bl = select.ToList <Branch>(); foreach (var item in bl) { WriteLine(item.ToString()); } //查询总记录数 var recordCount = select.GetRecordCount(); WriteLine("记录数量:" + recordCount.ToString()); //------------------------------------------------------------- // 8、HotelDBDB查询类的使用方式 //------------------------------------------------------------- //使用数据库名称+DB作为名称的类,可以直接调用聚合函数 var db = new SubSonicTestDB(); //平均值 WriteLine("平均值:" + db.Avg <Branch>(x => x.Id).Where <Branch>(x => x.Id < 10).ExecuteScalar() + ""); //计算数量 WriteLine("计算数量:" + db.Count <Branch>(x => x.Id).ExecuteScalar() + ""); //计算合计数量 WriteLine("计算合计数量:" + db.Sum <Branch>(x => x.Id).ExecuteScalar() + ""); //最大值 WriteLine("最大值:" + db.Max <Branch>(x => x.Id).ExecuteScalar() + ""); //最小值 WriteLine("最小值:" + db.Min <Branch>(x => x.Id).ExecuteScalar() + ""); //--------------------------------------------------------------- // 9、存储过程调用方法 //--------------------------------------------------------------- //使用SPs.存储过程名称(参数1,参数2,参数3)就可以调用存储过程 var obj = SPs.P_Branch_GetMaxBranchDeptCode(1, 0).ExecuteScalar(); WriteLine(obj + ""); //--------------------------------------------------------------- // 10、直接执行QueryCommand的方式 //--------------------------------------------------------------- //获取数据源 -- 主要用于绑定连接的服务器,如果有多台服务器多个数据库时,可使用不同的数据源来进行绑定查找 var provider = SubSonic.DataProviders.ProviderFactory.GetProvider(); //定义事务,给后面的事务调用 var batch = new BatchQuery(provider); var sql = string.Format("select * from {0}", BranchTable.TableName); //例一:获取影响记录数 QueryCommand qcCommand = new QueryCommand(sql, provider); WriteLine(qcCommand.Provider.ExecuteQuery(qcCommand) + ""); //例二:获取DataTable var q = new SubSonic.Query.QueryCommand(sql, provider); var table = q.Provider.ExecuteDataSet(q).Tables[0]; if (dt != null && table.Rows.Count > 0) { for (int i = 0; i < table.Rows.Count; i++) { WriteLine(table.Rows[i][BranchTable.Id] + " " + table.Rows[i][BranchTable.Name]); } } //例三:使用事务执行 batch.QueueForTransaction(qcCommand); batch.ExecuteTransaction(); //例四:直接使用数据源执行 provider.ExecuteQuery(qcCommand); //---------------------------------------------------------------- // 11、Linq查询 //---------------------------------------------------------------- //Linq查询方式 var query = new Query <Branch>(db.provider); var posts = from p in query where p.DeptCode.StartsWith("0101") select p; foreach (var item in posts) { WriteLine(item.ToString()); } query = db.GetQuery <Branch>(); posts = from p in query where p.Id > 3 && p.Id < 6 select p; foreach (var item in posts) { WriteLine(item.ToString()); } //获取查询结果2 List <Branch> li2 = query.ToList <Branch>(); foreach (var item in li2) { WriteLine(item.ToString()); } //--------------------------------------------------------------------- // 12、Linq多表关联查询 //--------------------------------------------------------------------- //方法一 var query5 = from p in Position.All() join b in Branch.All() on p.Branch_Id equals b.Id where b.DeptCode == "0101" select p; foreach (var item in query5) { WriteLine(item.ToString()); } //方法二 var qry = (from p in db.Position join b in db.Branch on p.Branch_Id equals b.Id where b.DeptCode == "0101" select new ListView { PositionName = p.Name, BranchName = p.Branch_Name, DeptCode = b.DeptCode }); foreach (var item in qry) { WriteLine(item.ToString()); } //------------------------------------------------------------------------- // 13、使用事务 //------------------------------------------------------------------------- //例一 //从部门表中查询出编号为0102的Id、名称与说明三列 var query1 = new SubSonic.Query.Select(provider, BranchTable.Id, BranchTable.Name, BranchTable.Comment).From( BranchTable.TableName).Where <Branch>(x => x.DeptCode == "0102"); //加入事务 batch.QueueForTransaction(query1); //查询部门编号为0102且职位名称后面两个字为主管的所有职位 var query2 = new SubSonic.Query.Select(provider).From <Position>().Where <Position>(x => x.Branch_DeptCode == "0102").And( PositionTable.Name).EndsWith("主管"); //加入事务 batch.QueueForTransaction(query2); //运行事务,不返回任何信息 batch.ExecuteTransaction(); //例二 batch = new BatchQuery(); batch.Queue(query1); batch.Queue(query2); //执行事务,并返回数据 using (IDataReader rdr = batch.ExecuteReader()) { if (rdr.Read()) { //query1 results for (int i = 0; i < rdr.FieldCount; i++) { WriteLine(rdr[i] + ""); } } rdr.NextResult(); if (rdr.Read()) { //query2 results for (int i = 0; i < rdr.FieldCount; i++) { WriteLine(rdr[i] + ""); } } } //例三 batch = new BatchQuery(provider); var query3 = from p in db.Branch where p.Id > 1 && p.Id < 10 select p; batch.Queue(query3); var query4 = from p in db.Position where p.Branch_DeptCode == "0103" select p; batch.Queue(query4); //执行事务,并返回数据 using (var rdr = batch.ExecuteReader()) { if (rdr.Read()) { //query3 results for (int i = 0; i < rdr.FieldCount; i++) { WriteLine(rdr[i] + ""); } } rdr.NextResult(); if (rdr.Read()) { //query4 results for (int i = 0; i < rdr.FieldCount; i++) { WriteLine(rdr[i] + ""); } } } }
public void Batch_Should_Execute_Reader_And_Return_Typed_Lists_Using_Linq() { BatchQuery qry = new BatchQuery(provider); var pquery = new Query<Product>(provider); qry.Queue(from p in pquery where p.ProductID == 1 select p); qry.Queue(from p in pquery where p.ProductID == 2 select p); qry.Queue(from p in pquery where p.ProductID == 3 select p); bool canRead = false; List<Product> result1 = null; List<Product> result2 = null; List<Product> result3 = null; using(IDataReader rdr = qry.ExecuteReader()) { result1 = rdr.ToList<Product>(); canRead = true; if(rdr.NextResult()) result2 = rdr.ToList<Product>(); if(rdr.NextResult()) result3 = rdr.ToList<Product>(); canRead = rdr.NextResult(); } Assert.True(result1.Count > 0); Assert.True(result2.Count > 0); Assert.True(result3.Count > 0); Assert.False(canRead); }
public void Batch_Query_Should_Allow_Single_Quotes_In_Query() { var containsNames = new[] { "Products's One and Only", "Products's Second" }; Query<Product> products = new Query<Product>(provider); var query = from product in products where containsNames.Contains(product.ProductName) select product; var result = query.ToList(); BatchQuery batch = new BatchQuery(provider); batch.Queue(query); Assert.DoesNotThrow(() => batch.Execute()); }
private static void BatchToInsert() { Console.WriteLine("Batch Inserting Genres using Insert.Into with unescaped values."); IList<Genre> genres = new List<Genre> { new Genre { Name = "Genre's First" }, new Genre { Name = "Genre's Second" } }; BatchQuery bq = new BatchQuery(db.Provider); foreach (Genre genre in genres) { bq.Queue(genre.ToInsertQuery<Genre>(db.Provider)); } ExecuteQuery(bq); }
public void Batch_Should_Build_Batched_SQL_Using_Linq() { BatchQuery qry = new BatchQuery(provider); var pquery = new Query<Product>(provider); qry.Queue(from p in pquery where p.ProductID == 1 select p); qry.Queue(from p in pquery where p.ProductID == 2 select p); qry.Queue(from p in pquery where p.ProductID == 3 select p); string sql = qry.BuildSqlStatement(); Assert.Equal(3, sql.FindMatches("SELECT").Count); }
private static void SelectUsingContains(IEnumerable<string> genreContains) { Console.WriteLine("Loading genres that contain {0}.", genreContains); Query<Genre> genres = new Query<Genre>(db.Provider); var query = from genre in genres where genreContains.Contains(genre.Name) select genre; BatchQuery batch = new BatchQuery(db.Provider); batch.Queue(query); ExecuteQuery(batch); }
public void Batch_Should_Execute_Reader() { BatchQuery qry = new BatchQuery(provider); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(1)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(2)); qry.Queue(new Select(provider).From("Products").Where("ProductID").IsEqualTo(3)); int sets = 1; bool canRead = false; using(IDataReader rdr = qry.ExecuteReader()) { canRead = true; if(rdr.NextResult()) sets = 2; if(rdr.NextResult()) sets = 3; canRead = rdr.NextResult(); } Assert.Equal(3, sets); Assert.False(canRead); }
public void Batch_Should_Execute_Batched_SQL_With_Replaced_Command_Parameters_For_In_Operator() { BatchQuery qry = new BatchQuery(provider); qry.Queue(new Select(provider).From("Products").Where("ProductID").In(new int[] { 1, 2, 3 })); qry.Queue(new Select(provider).From("Products").Where("ProductID").In(new int[] { 1, 2, 3 })); qry.Queue(new Select(provider).From("Products").Where("ProductID").In(new int[] { 1, 2, 3 })); int sets = 1; bool canRead = false; using (IDataReader rdr = qry.ExecuteReader()) { canRead = true; if (rdr.NextResult()) sets = 2; if (rdr.NextResult()) sets = 3; canRead = rdr.NextResult(); } Assert.Equal(3, sets); Assert.False(canRead); }