Create an object using the ADO. NET universal interface
Another advantage of using the ADO. NET universal interface is that it can be inherited for creating a strongly Typed DataSet. Benefits of a strong DataSet include design-time check and Visual Studio. NET statement filling of a strong DataSet.
For more common use of ADO. NET universal interfaces, I used ADO. NET DbProviderFactories and expanded its compatibility with mysql. Let's take a look at it now. ADO. NET2.0 introduces a provider factory model and a general base class for various ADO. NET classes.
Restrictions of ADO. NET universal interfaces: The interfaces are not easy to expand, ADO. NET1.1 cannot create some class instances, and ADO. NET1.1 cannot judge available. NET data providers. Provides the factory model to address the above restrictions and extends ADO and NET models through abstract accumulation.
Use the ADO. NET DbProviderFactories class to create objects. Restrictions on the provider factory model, many query structures are unique to the database. When you set CommandText for parameterized queries, you may need to provide code specific to the program. to specify the parameter data type, you may need to provide code specific to the program. To make the developed code generic.
It is not limited to specific databases. In this development, DbProviderFactory + standard SQL is used to develop an encapsulation suitable for mysql and sqlserver. However, DbProviderFactories does not provide DbProviderFactory support for mysql, so we need to expand ADO. NET universal interface is compatible with mysql, and in ADO.net 2.0, the ParameterMarkerFormat of mysql and sqlserver has bugs, ADO. so the extension class must solve this bug.
Public static class DbProviderFactoriesEx
{
Public static DbProviderFactory GetFactory (string providerName)
{
If (providerName = null) throw new ArgumentNullException ("providerName ");
DbProviderFactory dbFactory; switch (providerName)
{
Case "MySql. Data. MySqlClient": return new MySqlClientFactory (); default: return DbProviderFactories. GetFactory (providerName );
}
}
Public static string GetParameterMarkerFormat (DbConnection connect)
{
If (connect = null)
Throw new ArgumentNullException ("connect ");
Type type = connect. GetType ();
If (type = typeof (MySql. Data. MySqlClient. MySqlConnection ))
Return "? {0} "; // mysql bug
If (type = typeof (System. Data. SqlClient. SqlConnection ))
Return "@ {0}"; // MS bug connect. Open ();
String result = connect. GetSchema ("performanceinformation"). Rows [0] ["ParameterMarkerFormat"]. ToString ();
Connect. Close (); return result;
}
}