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;