Use dbproviderfactories to create project with unknown Database

Source: Internet
Author: User
The new system. Data. Common. dbproviderfactory class provides database developers with applications with unclear data sources. Program . Creating a complex data input application that can seamlessly interwork with all relational database managers (managed data providers are effective) is not easy. For SQL syntax, data types, languages used in Stored Procedures, fault tolerance Processing, and other minor differences in database-dependent functions, there is no doubt that custom processing is required. If you currently use. net Framework oledb managed data provider or ADODB with ole db provider to transmit database interoperability, you may find: Microsoft and third-party ADO. the performance provided by the net hosting provider is enhanced, so it has greater scalability. In addition, the Implementation scope granted by. Net to managed data providers makes it difficult for people to write completely transparent to providers. Code .
The steps for using the dbproviderfactories class are as follows:
1. Configure providername in the configuration file

< Configuration >
< Connectionstrings >
< Add Name = "SQL Server" Providername = "System. Data. sqlclient"  
Connectionstring = "Data Source =. \ sqlexpress; attachdbfilename = | datadirectory | \ databases \ mydata. MDF; Integrated Security = true; user instance = true"   />
</ Connectionstrings >
</ Configuration >


2. Use the following code in the code to createConnectionstringsettings myconnectionsettings = Configurationmanager. connectionstrings [myname];
// Get provider
Dbproviderfactory myprovider = Dbproviderfactories. getfactory (myconnectionsettings. providername );
// Get connection
Dbconnection myconnection = Myprovider. createconnection ();
Myconnection. connectionstring = Myconnectionsettings. connectionstring;
Myconnection. open ();

Dbdataadapter myadapter = Myprovider. createdataadapter ();
Dbcommand mycommand = Myprovider. createcommand ();


Mycommand. Connection = Myconnection;
Mycommand. commandtext = Myquery;

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.