DAM simple cross-database ADO. NET Component

Source: Internet
Author: User

Its features:

When you use SQL statements to access the database without changing the code at the DAL layer, you only need to modify the configuration file, it can be used across different types of databases.
Make your DAL-Layer Code more concise. It automatically creates a database connection for you based on the connection information configured in the configuration file. Close the connection and release the connection.
It is easier for different modules on the DAL layer to access different types or databases.
Good execution efficiency. It is based on native ADO. NET. It only reflects the assembly of the ADO provider that Accesses Different databases at a time (DAM eventually Accesses Different Types of databases through these providers ).
Its Configuration:Copy codeThe Code is as follows: <etettings>
<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 names in connectionstrings below ". "damconnection" is the default KEY read by DAM. Do not change the KEY name. the VALUE can be changed based on the database connection you connect to by default.
2. if you want to apply multiple database connections to access different databases. you can add other key-value items in the deleettings. in the code, use the Config or Factroy in DAM to create different database connections, and access different or different types of databases.
Connectionstrings
1. The connectionString value in the item does not need to be changed
2. The providerName value in the item is divided into three parts.

The complete Class Name of the connection; The Set Name of the connection class
(Namespace + class name) (separated by ";" in the middle) (Note that. dll is not added)

Test code:
DAL layer test codeCopy codeThe Code is as follows: // <summary>
/// Data access layer
/// Access the database through Dam
/// </Summary>
Public class DALTest
{
/// <Summary>
/// The simplest insert operation
/// Execute will automatically create the connection, Execute the operation, release the Command, close the connection, and release the link of the corresponding database.
/// </Summary>
/// <Returns> Number of affected rows after execution </returns>
Public int Insert ()
{
Return Execute. SExecuteNonQuery ("sp_name", null );
// Return Execute. SExecuteNonQuery ("insert into tablename (...) values (...)", CommandType. Text, null );
}
/// <Summary>
/// Insert with Parameters
/// Execute will automatically create the connection to the corresponding database
/// Converts the parameter type to the parameter of the corresponding database type 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 with parameters and obtain the return value of the Parameter
/// Execute will automatically create the connection to the corresponding database
/// Converts the parameter type to the parameter of the corresponding database type for database operations.
/// After the execution is complete, you can get the returned 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 parameters and obtain the return value of the parameter. this parameter is sent from the BLL layer.
/// Same as above...
/// </Summary>
/// <Returns> </returns>
Public int Insert_Paras (DbParameter [] paras)
{
Return Execute. SExecuteNonQuery ("sp_name", paras );
// In this way, the response parameters can be obtained through paras [1] On The BLL layer.
}
/// <Summary>
/// Execute the transaction processing for multiple operations in the same connection
/// Here we use a stored procedure with parameters and an SQL statement without parameters.
/// Static call 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)
{
// Manually close the connection !!
Connection. Close ();
Connection. Dispose ();
}
}
Return resultBool;
}
/// <Summary>
/// Execute multi-task operations with the same connection (do not use transactions)
/// Here we use the object method. In this way, multi-task operations may be stable and efficient.
/// </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 is called (but the transaction is set to NULL, the transaction is invalid)
// The connection will not be automatically closed for release. We need to manually release the connection.
Connection. Close ();
Connection. Dispose ();
}
}
}
}

Dam: Dam.rar
If you are interested, you can check out the internal help documentation.

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.