Silverlight practice example 3 (and the use of set attributes)-database access layer and data access layer

Source: Internet
Author: User
Tags silverlight

These two layers are actually the layers of most entity frameworks. For these two layers, you can refer to the dynamic and soft methods. Of course, you can also build them by yourself, you can also use the existing mature Entity Framework. However, for large projects or product projects, it is best not to use complex physical frameworks, because updates, maintenance, and upgrades are not controllable, and many times there are some restrictions, it is not conducive to building efficient and dynamic business applications (how powerful is it? I still haven't used SQL statements to deal with the database directly. If the cache is used when the framework is used, the stored procedure is used, data Synchronization is a big problem when other SQL statements are used)

The following code is an example of the two layers:

1) database access layer:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Data;
Using system. Data. Common;
Using system. Data. sqlclient;
Namespace dbhelper
{
Public class dbhelper
{
/// <Summary>
/// Execute the query statement and return Dataset
/// </Summary>
/// <Param name = "sqlstring"> query statement </param>
/// <Returns> dataset </returns>
Public dataset querydata (string sqlstring, dbconnection Conn, dbtransaction trans)
{
Sqlconnection connection = conn as sqlconnection;
Connection. open ();
Try
{
Dataset DS = new dataset ();
Sqldataadapter command = new sqldataadapter (sqlstring, connection );
Command. Fill (DS, "ds ");
Command. Dispose ();
Return Ds;
}
Catch
{
Return NULL;
}
}
}
}
To support multiple types of databases, you only need to create an abstraction layer here, and then use the corresponding database access layer instance based on the dynamic decision of the user's database type.

2) data access layer:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Data;
Using dbhelper;
Using system. Data. Common;

Namespace hdatabase
{
Public class dynamicdataaccess
{
Public datatable getdatatable (string strsql, dbconnection conn)
{
Dataset theds = new dbhelper (). querydata (strsql, Conn, null );
If (theds! = NULL & theds. Tables. Count> 0)
{
Return theds. Tables [0];
}
Return NULL;
}
}
}
Here, you can add simple data access methods, such as adding, modifying, and deleting data. If you want to process a transaction, add a Database Transaction parameter to the parameter and use the business logic to determine the transaction, rather than at the underlying layer. In addition, if the business logic layer calls the data access layer method and does not provide database connections or transactions, the default configuration should be used to create database connections without transaction processing. After all, most applications of a system do not need to change the database connection from time to time. Remember: The purpose of providing connection and transaction parameters in data access methods is to provide a data connection and transaction method that can be used by the business logic layer.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.