Retrieve data from the database
The examples in this article are also implemented in the web service. The package that contains the web service implementation is attached in the Resources block.
The CreateJson
method processes query results in the examples. View the method below.
private string CreateJson(IDataReader dataReader) {
var list = new List <dynamic> ();
var cnt = dataReader.FieldCount;
var fields = new List <string> ();
for (int i = 0; i < cnt; i++) {
fields.Add(dataReader.GetName(i));
}
while (dataReader.Read()) {
dynamic exo = new System.Dynamic.ExpandoObject();
foreach(var field in fields) {
((IDictionary <String, Object> ) exo).Add(field, dataReader.GetColumnValue(field));
}
list.Add(exo);
}
return JsonConvert.SerializeObject(list);
}
Example 1
Select a number of records from the needed table (object schema).
public string SelectColumns(string tableName, int top) {
top = top > 0 ? top : 1;
var result = "{}";
var select = new Select(UserConnection)
.Top(top)
.Column(Column.Asterisk())
.From(tableName);
using(DBExecutor dbExecutor = UserConnection.EnsureDBConnection()) {
using(IDataReader dataReader = select.ExecuteReader(dbExecutor)) {
result = CreateJson(dataReader);
}
}
return result;
}
Example 2
Select the IDs, names, and birth dates of contacts whose birth dates are later than the required year.
public string SelectContactsYoungerThan(string birthYear) {
var result = "{}";
var year = DateTime.ParseExact(birthYear, "yyyy", CultureInfo.InvariantCulture);
var select = new Select(UserConnection)
.Column("Id")
.Column("Name")
.Column("BirthDate")
.From("Contact")
.Where("BirthDate").IsGreater(Column.Parameter(year))
.Or("BirthDate").IsNull()
.OrderByDesc("BirthDate")
as Select;
using(DBExecutor dbExecutor = UserConnection.EnsureDBConnection()) {
using(IDataReader dataReader = select.ExecuteReader(dbExecutor)) {
result = CreateJson(dataReader);
}
}
return result;
}
Example 3
Select the IDs, names, and birth dates of contacts whose birth dates are later than the specified year and who have the account specified.
public string SelectContactsYoungerThanAndHasAccountId(string birthYear) {
var result = "{}";
var year = DateTime.ParseExact(birthYear, "yyyy", CultureInfo.InvariantCulture);
var select = new Select(UserConnection)
.Column("Id")
.Column("Name")
.Column("BirthDate")
.From("Contact")
.Where()
.OpenBlock("BirthDate").IsGreater(Column.Parameter(year))
.Or("BirthDate").IsNull()
.CloseBlock()
.And("AccountId").Not().IsNull()
.OrderByDesc("BirthDate")
as Select;
using(DBExecutor dbExecutor = UserConnection.EnsureDBConnection()) {
using(IDataReader dataReader = select.ExecuteReader(dbExecutor)) {
result = CreateJson(dataReader);
}
}
return result;
}
Example 4
Select the IDs and names of all contacts by joining them to the IDs and names of the corresponding accounts.
public string SelectContactsJoinAccount() {
var result = "{}";
var select = new Select(UserConnection)
.Column("Contact", "Id").As("ContactId")
.Column("Contact", "Name").As("ContactName")
.Column("Account", "Id").As("AccountId")
.Column("Account", "Name").As("AccountName")
.From("Contact")
.Join(JoinType.Inner, "Account")
.On("Contact", "Id").IsEqual("Account", "PrimaryContactId")
as Select;
using(DBExecutor dbExecutor = UserConnection.EnsureDBConnection()) {
using(IDataReader dataReader = select.ExecuteReader(dbExecutor)) {
result = CreateJson(dataReader);
}
}
return result;
}
Example 5
Select the IDs and names of the primary account contacts.
public string SelectAccountPrimaryContacts() {
var result = "{}";
var select = new Select(UserConnection)
.Column("Id")
.Column("Name")
.From("Contact").As("C")
.Where()
.Exists(new Select(UserConnection)
.Column("A", "PrimaryContactId")
.From("Account").As("A")
.Where("A", "PrimaryContactId").IsEqual("C", "Id"))
as Select;
using(DBExecutor dbExecutor = UserConnection.EnsureDBConnection()) {
using(IDataReader dataReader = select.ExecuteReader(dbExecutor)) {
result = CreateJson(dataReader);
}
}
return result;
}
Example 6
Select the countries and the number of cities in a country if such number is greater than the specified number.
public string SelectCountriesWithCitiesCount(int count) {
var result = "{}";
var select = new Select(UserConnection)
.Column(Func.Count("City", "Id")).As("CitiesCount")
.Column("Country", "Name").As("CountryName")
.From("City")
.Join(JoinType.Inner, "Country")
.On("City", "CountryId").IsEqual("Country", "Id")
.GroupBy("Country", "Name")
.Having(Func.Count("City", "Id")).IsGreater(Column.Parameter(count))
.OrderByDesc("CitiesCount")
as Select;
using(DBExecutor dbExecutor = UserConnection.EnsureDBConnection()) {
using(IDataReader dataReader = select.ExecuteReader(dbExecutor)) {
result = CreateJson(dataReader);
}
}
return result;
}
Example 7
Retrieve the ID of the contact by their name.
public string SelectCountryIdByCityName(string CityName) {
var result = "";
var select = new Select(UserConnection)
.Column("CountryId")
.From("City")
.Where("Name").IsEqual(Column.Parameter(CityName)) as Select;
result = select.ExecuteScalar <Guid> ().ToString();
return result;
}