DAM simple cross database ADO.NET component

  • 2020-05-10 18:03:43
  • OfStack

Its features:

If you want to access the database through SQL, you can change the configuration file for you across different types of databases without having to change the DAL layer 1 bit of code.
Make your DAL layer code more concise. It will automatically create a database connection for you and perform the operation according to the connection information configured in the configuration file. Close the connection, release the connection, etc.
Make it easier for different modules in the DAL layer to access different types or different databases.
Good execution efficiency, it is based on native ADO.NET. It only reflects the assembly of ADO providers that access different databases once (DAM eventually implements access to different types of databases through these providers).
Its configuration:
 
<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 an KV. KEY="damconnection" VALUE=" connectionstrings "."damconnection" is the name of KEY read by DAM by default, please do not change the name of KEY. The value of VALUE can be changed according to the database connection you are connecting to by default.
2. If you want to use multiple database connections to access different databases, you can add another KV entry to appsettings. You can use Config or Factroy in DAM to create different database connections in your code to access different or different types of databases.
connectionstrings
1. The value of connectionString in item does not need to be changed
2. The value of providerName in item is divided into three parts

The full class name of the connection; The assembly name of the connection class
(namespace + class name) (";" in the middle Separate) (take care not to add.dll)

Test code:
DAL layer test code
 
/// <summary> 
///  Data access layer  
///  through Dam Access database  
/// </summary> 
public class DALTest 
{ 
/// <summary> 
///  The simplest insertion operation  
/// Execute Will help you automatically create the corresponding database connection, perform operations, release Command , close the connection, release the link, etc  
/// </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 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 parameter database operation . 
/// </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 a parameter and get the 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 parameter database operation . 
///  You get the return value when the execution is complete  
/// </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> 
///  The insert operation with a parameter, and can get the parameter return value from the BLL Layer was  
/// Same as above ... 
/// </summary> 
/// <returns></returns> 
public int Insert_Paras(DbParameter[] paras) 
{ 
return Execute.SExecuteNonQuery("sp_name", paras); 
// So you can do that BLL Layer through  paras[1] To get the return parameter  
} 
/// <summary> 
///  Performs transactions for multiple operations on the same connection  
///  Used here 1 Two parameterized stored procedures and 1 One without parameters sql Statements operation  
///  Use is to call methods statically  
/// </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) 
{ 
// You need to close the connection manually!!  
connection.Close(); 
connection.Dispose(); 
} 
} 
return resultBool; 
} 
/// <summary> 
///  Perform multi-task operations on the same connection (without using transactions)  
///  Here we use object methods. Multitasking in this way may be more 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 call   The method with the transaction (but we set the transaction to NULL , transaction invalid)  
// So the connection will not automatically close the release, we need to manually release  
connection.Close(); 
connection.Dispose(); 
} 
} 
} 
} 

Dam: Dam.rar
If you're interested, check out the :) help documentation.

Related articles: