Subsonic Tutorial by Query Type: Insert The Record
Subsonic Tutorial by Query Type: Insert The Record
For example install subsonic 2.2 and setup as per following link to generate the code:
//Fill the object to save. Please make sure that you fill all must (not
null) object
Supplier sup = new Supplier();
sup.Address = "Test Address";
sup.City = "Ahmedabad";
sup.CompanyName = "IntelliPro";
sup.ContactName = "Test Contact";
sup.ContactTitle = "Manager";
sup.Country = "India";
sup.Fax = "01234567";
sup.HomePage = "http://www.xyz.com";
sup.Phone = "01293202";
sup.PostalCode = "380051";
sup.Region = "Test Region";
// call save
sup.Save();
// call save
sup.Save();
//In case you have IsDelete flag and you want to delete the function, use
following code.
Supplier.Destroy(30);
Select all Record
Select record by parameter other than primary key (but only one
parameter)
// You can also apply order by at the end. To generate order by, you must
use its static function Asc or Desc
// e.g. OrderBy.Asc or OrderBy.Desc and supply column name into the same.
colSup.LoadAndCloseReader(Supplier.FetchByParameter
(Supplier.Columns.Country, SubSonic.Comparison.NotEquals,"India",
OrderBy.Asc(Supplier.Columns.City) ) );
Following are the list of comparisions which we can use into comparing the parameter.
Select record by parameter other than primary key (but more than one
parameter)
// There are two methods for doing this. You can use any.
colSup.Where(Supplier.Columns.City,"Ahmedabad");
colSup.Where(Supplier.Columns.CompanyName, Comparison.Like ,"Intelli");
// you can also use order by before calling Load function to order the
result.
colSup.OrderByAsc(Supplier.Columns.PostalCode);
// Following statement load the records with above condition. Here one
thing to note that
// It only uses where and that also with AND default. you cannot do OR
here. For that
// you need to use second option
colSup.Load();
// Query has many function and you can use multiple function for your query
qry.AddWhere(Supplier.Columns.City,"Ahmedabad");
qry.OR(Supplier.Columns.CompanyName, Comparison.Like ,"Intelli");
qry.OrderBy = OrderBy.Asc(Supplier.Columns.City);
// Set order by. Its property. You can use order_by function as well.
qry.OrderBy
// Where condition
qry.AddWhere
You have other methods of query to execute other than execute reader.
You can use, Execute for update query, ExecuteDataSet for getting dataset and ExecuteScaler for
scalar values.
Suppose you wanted Invoices from the 5 tables joined, you can create the view Invoice and then you
can use that object same way like tables and also you can query view to filter the data like tables.
There are two ways. Either you can make stored procedure and generate the SP function and use it
or you can make the query as below.
// Make sql statement. But make sure that you do not hardcode any values.
Below query can be made with subsonic
// but it is used just only for example.
string query = "SELECT * FROM " + Supplier.Schema.TableName + " WHERE " +
Supplier.Columns.Address + " LIKE %abc%";
Aggregate Functions
If you want to use aggregate function on some table, you can use it following way.