When we connect to MSSQLSEVER, it is generally SqlConnection con = new SqlConnection (ConStr); in this format to create a connection string,
This is the time when we seem to feel very convenient. But on that day, your boss gave you an Oracle backup database. Do you still need OracleConnection con = new OracleConnection (conStr)? Then, the boss suddenly wondered why the data volume was not large, in addition, ACCESS2010 is quite trendy. If you want to change it to ACCESS, you can open it directly anywhere. Do you want OleDbConnection conn = new OleDbConnection (conStr ); when the boss said that the customer needed DB2, the child could not bear it. Isn't it a tough task ......
As a result, he thought about the idea of. NET, that is, to provide unified standardization to manage all objects. So let's track the source and solve the problem from the source.
He started to view it like this:
He found that the IDbConnection interface has provided the basic methods we usually need.
Here he thought of using the type conversion (pointing an interface variable to an instance of the class implementing the interface, of course, this can also be written to the first part of the implicit conversion)
Then I need:
OleDbConnection Code
1 // Assumes connectionString is a valid connection string.
2 using (OleDbConnection connection =
3 new OleDbConnection (connectionString ))
4 {
5 connection. Open ();
6 // Do work here.
7}
Or:
OdbcConnection Code
1 // Assumes connectionString is a valid connection string.
2 using (OdbcConnection connection =
3 new OdbcConnection (connectionString ))
4 {
5 connection. Open ();
6 // Do work here.
7}
Or:
OracleConnection Code
1 // Assumes connectionString is a valid connection string.
2
3 using (OracleConnection connection =
4
5 new OracleConnection (connectionString ))
6
7 {
8
9 connection. Open ();
10
11 // Do work here.
12
13}
14
15 OracleConnection nwindConn = new OracleConnection ("Data Source = MyOracleServer; Integrated Security = yes ;");
16
17 nwindConn. Open ();
It turns out that these tedious steps can be omitted.
Now we only need:
IDbConnection conn = new OracleConnection (conStr );
IDbConnection conn = new OleDbConnection (conStr );
IDbConnection conn = new SqlConnection (ConStr );
Is it still complicated? Because it has not been intuitively presented to everyone, now we will go to the Code to show its advantages:
1 using System;
2 using System. Configuration;
3 using System. Data. OleDb;
4 using System. Data;
5 using System. Data. SqlClient;
6 using System. Data. OracleClient;
7
8 namespace IFADO
9
10 {
11 class Program
12 {
13 static void Main (string [] args)
14 {
15 string conStr = ConfigurationManager. ConnectionStrings ["conStr"]. ConnectionString;
16 string privider = ConfigurationManager. ConnectionStrings ["conStr"]. ProviderName;
17 IDbConnection conn;
18 switch (privider)
19 {
20 case "Access": conn = new OleDbConnection (conStr); break;
21
22 case "SQLSever": conn = new SqlConnection (conStr); break;
23
24 case "Oracle": conn = new OracleConnection (conStr); break;
25
26 default: throw new Exception ("no suitable database is found. Please extend it !);
27}
28 using (conn)
29
30 {
31 conn. Open ();
32 using (IDbCommand cmd = conn. CreateCommand ())
33 {
34 cmd. CommandText = "SELECT * FROM Test_TableORDER BY Test_Table.ID DESC ";
35
36 cmd. ExecuteNonQuery ();
37}
38 Console. WriteLine ("query completed! ");
39
40 Console. ReadKey ();
41}
42}
43}
44}
At this time, we only worry about the value of the ProviderName node in the configuration file, but we can extend the database with peace of mind without worrying about whether the node will run normally.
Author: Xie xiaoge