There are two issues to consider when there is a possibility to replace the database type:
One, different types of database namespaces are not the same, the function names used are not the same, although very similar;
Two, some SQL statements are not common between databases that are not the same!
Then how to change the database type, how to do as little as possible because of the above two points caused by the impact?! With the interface, the 1th impact can be minimized! As for the 2nd, the operation of the data can be implemented in the database server side as far as possible, this discussion is not within the scope of this study, there is not much to say! The following is a detailed description of the use of the interface to minimize the impact of the 1th.
First, clear two points:
1, regardless of the type of database, the operations on the database are: Execute SQL , execute with parameter SQL , execute stored procedures, execute stored procedures with parameters (here The operation is divided, according to different developers have different habits, such as some people are accustomed to points: Add, edit, delete, get these kinds, but this will not affect);
2, Unlike database types, there are different namespaces, function names, and these namespaces, function names must also be replaced when database types are replaced.
To reduce the impact, the number of changes to a different namespace, function name is minimized, and other modules are not required to be changed! My approach is to define an interface that declares a number of methods for database operations; Each database type defines a class to implement these interfaces, and then a unified portal for all databases! In this way, to replace the database type, only need to define a new class, re-implement those interfaces! No need to modify the other!
Here is an example.
Interface:
namespacedataevent
{
// <summary>
/// Database Operation interface
// </summary>
Public Interfaceidataaccess
{
// <summary>
/// Execute SQL statement
//</summary>
// / <param name= "sql" >SQL statements </param>
/// <returns> Returns the number of rows affected </returns>
intExcesql (stringSQL);
Access Database Classes
namespacedataevent
{
Public classaccdataaccess:idataaccess
{
public int excesql (string strsql)
{
string t_str = "accesssql:" + strSQL;
return t_str. Length;
}
}
}
SQL Server Database Classes
namespacedataevent
{
// <summary>
/// SQL Server database operation class
/// Idataaccess Interface implementation
// </summary>
Public classsqldataaccess:idataaccess
{
public int excesql (string strsql)
{
string t_str = "Sql: " + strSQL;
return t_str. Length;
}
}
}
Oracle Database
namespaceDataevent
{
// <summary>
/// Oracle Database Operations class
/// Idataaccess Interface implementation
// </summary>
Public classoradataaccess:idataaccess
{
PrivateOracleDataAdapter dbadapter= NULL;
PrivateOracleDataAdapter Sqladapter
{
Get
{
if (dbadapter = = null)
Dbadapter = new oracledataadapter ();
return (dbadapter);
}
}
Public intExcesql (stringstrSQL)
{
string t_str = "Oracle: " + strSQL;
return t_str. Length;
}
}
}
Unified Entrance:
namespacedataevent
{
// <summary>
/// Unified portal for all database operations
// </summary>
Public Static classcommonaccess
{
Private Static ReadOnly stringDataaccesstype=configurationsettings.appsettings["Dataaccesstype"];
Private Staticidataaccess _obj;
Private Staticidataaccess OBJ
{
Get
{
if(_obj== NULL)
{
string m_classname = "dataevent. " + dataaccesstype;
_obj = (idataaccess) assembly.load ("dataevent") . CreateInstance (M_classname);
}
return_obj;
}
}
// <summary>
/// Test
// </summary>
// / <returns> Returns a string </returns>
Public Static intExcesql (stringstrSQL)
{
return obj.excesql (strSQL);
}
}
}
Test, Button event:
Private voidButton1_Click (Objectsender, EventArgs e)
{
MessageBox.Show (DATAEVENT.COMMONACCESS.EXCESQL ("excesql result! " ). ToString ());
}
Access
database, "Accesssql:excesql result!" string has - characters!
<?XML version= "1.0" encoding= "Utf-8"?>
<Configuration>
<appSettings>
<AddKey= "Dataaccesstype"value= "Accdataaccess"/>
</appSettings>
</Configuration>
displaying results: 25
When replacing an Oracle database (when replacing a database, you only need to modify the Value of the configuration string "Dataaccesstype" in the configuration file Value! ),"Oracle:excesql result!" string Only A characters!
<?XML version= "1.0" encoding= "Utf-8"?>
<Configuration>
<appSettings>
<AddKey= "Dataaccesstype"value= "Oradataaccess"/>
</appSettings>
</Configuration>
Show Results: $
in real development, this is not necessarily the case for changing database types frequently, but taking these possibilities into account can improve the flexibility of the system. At the same time, this example is also hoped to play a role in the future. I hope that we can have this idea later , anytime, anywhere to consider the flexibility of the system, reusability, and so on. My qualifications are still shallow, write is what I can feel at present, what is wrong, but also look at the expert pointing point