Create an object using the ADO. NET universal interface

Source: Internet
Author: User

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;

}

}

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.