describes how to work with databases and how database access
access to the database
Technology Flexberry the following methods access the database:
- Service data. To read data you can use as an
- Parental control,
- and LINQProvider).
- ADO.NET. Allows you to execute SQL queries from the code.
Note: ADO.NET НЕ ИСПОЛЬЗУЕТСЯ
in projects in cases when you can do without it. Even if there is an urgent need to use ADO.NET one should check whether it is possible to do without it. Perhaps the use funcSQL.
If to speak about the applicability of possible methods of reading data: ADO.NET - use only in cases where not enough features of the data service.
a request in the service data
What constitutes the text of the SQL query generated by the service data:
- Settings LoadingCustomizationStruct (work on sorting, overlay limitFunction, podjeto aggregating values and other similar actions you need to offload to the server by setting parameters
LoadingCustomizationStruct
. - Attribute markup (e.g. attribute
Storage
as classes and relations). - Inheritance using a typeusage.
- The interception of the query to the database.
If you look at what requests sends the service data to the server, then there are constants of type StormMainObjectKey
and STORMJoinedMasterKey
.
StormMainObjectKey
- primary key (primarykey) of the object being read from the database. ConstantStormMainObjectKey
defined asICSSoft.STORMNET.FunctionalLanguage.SQLWhere.SQLWhereLanguageDef.StormMainObjectKey
.STORMJoinedMasterKey
. The organization of the proofreading of the database foreign keys are given names according to the typeSTORMJoinedMasterKey1
. PstrfSTORMJoinedMasterKey` to use when constructing constraints in code not recommended, because their order may change.
ADO.NET
Below is an example of execution of SQL queries through technology ADO.Net:
public virtual decimal СуммаОплаченныхПокупок
{
get
{
var connection = (SqlConnection)((SQLDataService)DataServiceProvider.DataService).GetConnection(); //get connection
var command = new SqlCommand("SELECT SUM(purchase.\"Amount\") "+
"FROM \"Customer\" customer join \"Purchase\" purchase on customer.\"primaryKey\" = purchase.\"Buyer\" "+
"WHERE purchase.\"Buyer\"=@Customer AND purchase.\"Status\" = \'Paid\' ", connection); //make a request
var parameter = new SqlParameter("@Customer", SqlDbType.UniqueIdentifier);
parameter.Value = ((KeyGuid)this.__PrimaryKey).Guid; //define parameter values
command.Parameters.Add(parameter);
try
{
connection.Open();
var value = (decimal)command.ExecuteScalar(); //execution of query
}
finally
{
connection.Close();
}
return value;
}
//...
}