string statement = "SELECT TOP " + maxRecords + " * FROM Customers "; string whereConcatenator = "WHERE "; if (companyNameTextBox.Text.Length > 0) { statement += whereConcatenator; statement += "CompanyName like '" + companyNameTextBox.Text + "%' "; whereConcatenator = "AND "; } if (cityTextBox.Text.Length > 0) { statement += whereConcatenator; statement += "City like '" + cityTextBox.Text + "%' "; whereConcatenator = "AND "; } if (countryComboBox.SelectedItem != null) { statement += whereConcatenator; statement += "Country = '" + countryComboBox.SelectedItem + "' "; whereConcatenator = "AND "; } |
SelectQueryBuilder query = new SelectQueryBuilder(); query.SelectFromTable("Customers"); query.SelectAllColumns(); query.TopRecords = maxRecords; if (companyNameTextBox.Text.Length > 0) query.AddWhere("CompanyName", Comparison.Like,companyNameTextBox.Text + "%"); if (cityTextBox.Text.Length > 0) query.AddWhere("City", Comparison.Like, cityTextBox.Text + "%"); if (countryComboBox.SelectedItem != null) query.AddWhere("Country", Comparison.Equals, countryComboBox.SelectedItem); string statement = query.BuildQuery(); // or, have a DbCommand object built // for even more safety against SQL Injection attacks: query.SetDbProviderFactory( DbProviderFactories.GetFactory( "System.Data.SqlClient")); DbCommand command = query.BuildCommand(); |
SelectQueryBuilder query = new SelectQueryBuilder(); query.SelectFromTable("Orders"); query.AddWhere("OrderDate", Comparison.LessOrEquals,new SqlLiteral("getDate()")); |
SelectQueryBuilder query = new SelectQueryBuilder(); query.SelectFromTable("Orders"); query.AddJoin(JoinType.InnerJoin,"Customers", "CustomerID",Comparison.Equals,"Orders", "CustomerID"); query.AddWhere("Customers.City",Comparison.Equals, "London"); |
SELECT Orders.* FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID WHERE (Customers.City = 'London') |
SelectQueryBuilder query = new SelectQueryBuilder(); query.SelectColumns("count(*) AS Count", "ShipCity"); query.SelectFromTable("Orders"); query.GroupBy("ShipCity"); query.AddHaving("ShipCity", Comparison.NotEquals, "Amsterdam"); query.AddOrderBy("count(*)", Sorting.Descending); |
SELECT count(*) AS Count, ShipCity FROM Orders GROUP BY ShipCity HAVING (ShipCity <> 'Amsterdam') ORDER BY count(*) DESC |
SelectQueryBuilder query = new SelectQueryBuilder(); query.SelectFromTable("Orders"); // Add 'Criteria' column to level 1 query.AddWhere("CustomerID", Comparison.Equals,"VINET", 1); query.AddWhere("OrderDate", Comparison.LessThan,new DateTime(2005,1,1), 1); // Add first 'Or...' column to level 2 query.AddWhere("CustomerID", Comparison.Equals, "TOMSP", 2); query.AddWhere("OrderDate", Comparison.LessThan,new DateTime(2004,6,30), 2); // Add second 'Or...' column to level 3 query.AddWhere("CustomerID", Comparison.Equals,"TOMSP", 3); query.AddWhere("OrderDate", Comparison.GreaterThan,new DateTime(2006,1,1), 3); |
SelectQueryBuilder query = new SelectQueryBuilder(); query.SelectFromTable("Orders"); // Add 'Criteria' column to level 1 query.AddWhere("CustomerID", Comparison.Equals, "VINET", 1); query.AddWhere("OrderDate", Comparison.LessThan, new DateTime(2005,1,1), 1); // Add 'Or...' column to level 2 query.AddWhere("CustomerID", Comparison.Equals, "TOMSP", 2); // Add the date selection clause WhereClause clause =query.AddWhere("OrderDate", Comparison.LessThan, new DateTime(2004,6,30), 2); // Add a nested clause to the captured clause clause.AddClause(LogicOperator.Or, Comparison.GreaterThan, new DateTime(2006,1,1)); |
SELECT Orders.* FROM Orders WHERE ( (CustomerID = 'VINET') AND (OrderDate < '2005/01/01 12:00:00') ) OR ( (CustomerID = 'TOMSP') AND (OrderDate < '2004/06/30 12:00:00' OR OrderDate > '2006/01/01 12:00:00') ) |
欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛
https://bbs.txwb.com
关注天下网吧微信/下载天下网吧APP/天下网吧小程序,一起来超精彩
|
本文来源:vczx 作者:佚名