DAM Simple cross-database ado.net components

Source: Internet
Author: User
Tags add execution mssql sql net sqlite access connectionstrings
It's characteristic:

When you guarantee SQL statement compatibility, access to the database through it, without the need to change the DAL layer a bit of code, as long as you modify the configuration file, it can for you across different types of databases.
Make your DAL layer code more concise. It automatically creates the database connection for you based on the connection information configured by the profile, and performs the operation. Close the connection, release the connection, and so on.
It is more convenient to have different modules of the DAL layer accessing different types or different databases.
Good execution efficiency, it is based on native ado.net. It only reflects the assembly of the ADO provider that accesses different databases (the dam ultimately accesses different types of databases through these providers).
It's configured:
Copy CodeThe code is as follows:
<appsettings>
<add key= "rpsconnection" value= "MSSQL"/>
<add key= "damconnection" value= "SQLite"/>
</appsettings>
<connectionstrings>
<add name= "SQLite" connectionstring= "providername=" System.Data.SQLite.SQLiteConnection; System.Data.SQLite "/>
<add name= "MSSQL" connectionstring= "Providername=" System.Data.SqlClient.SqlConnection; System.Data "/>
</connectionstrings>

appsettings
1. Add a kv. key= "Damconnection" value= "one of the name's in connectionstrings below". " Damconnection "for the default dam read key, key name please do not replace." Value can be changed depending on the database connection that you default to connect to.
2. If you want to apply multiple database connections, you can access different databases. You can add other KV items to the appsettings. In your code, you can create different database connections using Dam Config or Factroy to access different or different types of databases.
connectionstrings
1. The ConnectionString value in the item does not need to change
2. The ProviderName value in the item is divided into three parts

The full class name of the connection; The assembly name where the connection class resides
(namespace + class name) (Intermediate use ";" Separate) (note No. dll)

Test code:
DAL Layer Test Code
Copy CodeThe code is as follows:
<summary>
Data Access Layer
Access to database through Dam
</summary>
public class Daltest
{
<summary>
The simplest insert operation
Execute will help you automatically create a connection to the corresponding database, perform actions, Release command, close the connection, release the link, and so on
</summary>
Number of affected rows after <returns> execution </returns>
public int Insert ()
{
Return Execute.sexecutenonquery ("Sp_name", null);
Return Execute.sexecutenonquery ("INSERT INTO TableName (..) Values (..) ", CommandType.Text, NULL);
}
<summary>
Insert operation with parameters
Execute will help you automatically create a connection to the corresponding database
and help you to convert the parameter type to the corresponding database type of parameters for database operations.
</summary>
<returns></returns>
public int Insert_paras ()
{
Dbparameter[] Paras =
{
New Mysqlparameter ("P1", "P1value")
, New SqlParameter ("P2", "P2value")
};
Return Execute.sexecutenonquery ("Sp_name", paras);
}
<summary>
Insert operation with parameter, and can get parameter return value
Execute will help you automatically create a connection to the corresponding database
and help you to convert the parameter type to the corresponding database type of parameters for database operations.
Execution complete You can get the return value
</summary>
<returns></returns>
public int Insert_paras (ref string Outputvalue)
{
int resultint = 0;
Dbparameter[] Paras =
{
New Mysqlparameter ("P1", "P1value")
, New SqlParameter ("P2_output", "")
{
Direction = ParameterDirection.Output
}
};
ResultInt = Execute.sexecutenonquery ("Sp_name", paras);
if (paras[1). Value!= null) Outputvalue = paras[1]. Value.tostring ();
return resultint;
}
<summary>
Insert operation with parameter, and can get parameter return value, this parameter comes from BLL layer
Ditto...
</summary>
<returns></returns>
public int Insert_paras (Dbparameter[] paras)
{
Return Execute.sexecutenonquery ("Sp_name", paras);
This allows the return argument to be obtained at the BLL layer via paras[1].
}
<summary>
Performing transactions with multiple operations on the same connection
This uses 1 stored procedures with parameters and an SQL statement operation with no parameters
Using is a static calling method
</summary>
<returns></returns>
public bool Insert_transaction ()
{
BOOL Resultbool = false;
Dbparameter[] Paras =
{
New Mysqlparameter ("P1", "P1value")
, New SqlParameter ("P2", "P2value")
};
DbConnection connection = null;
Dbtransaction transaction = NULL;
Try
{
Connection = Factory.createconnection ();
Connection. Open ();
Transaction = connection. BeginTransaction ();
int resultInt1 = Execute.sexecutenonquery (connection, transaction, "Sp_name1", paras);
int resultInt2 = Execute.sexecutenonquery (connection, transaction, insert INTO TableName (...) VALUES (...) ", null);
if (resultInt1 > 0 && resultInt2 > 0)
{
Transaction.commit ();
Resultbool = true;
}
Else
{
Transaction. Rollback ();
}
}
catch (Exception)
{
if (transaction!= null)
{
Transaction. Rollback ();
}
}
Finally
{
if (transaction!= null)
{
Transaction. Dispose ();
}
if (connection!= null)
{
We need to close the connection manually!!
Connection. Close ();
Connection. Dispose ();
}
}
return resultbool;
}
<summary>
Perform the same-connection multitasking (do not use transactions)
Here we use the object method. This can be a stable and efficient way to perform multitasking operations.
</summary>
<returns></returns>
public void Insert_much ()
{
Execute exec = Execute.newexecute;
DbConnection connection = null;
Try
{
Connection = Factory.createconnection ();
Exec. ExecuteNonQuery (connection, NULL, "Sp_name", null);
Exec. ExecuteNonQuery (connection, NULL, "sp_name2", null);
}
Finally
{
if (connection!= null)
{
Because the method with the transaction was invoked (but we set the transaction to null and the transaction was invalid)
So the connection does not automatically turn off the release, we need to manually release
Connection. Close ();
Connection. Dispose ();
}
}
}
}

Dam:Dam.rar
Interested can take a look at:) There is a help document inside.

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.