connection string c#

How can I achieve below

private static MySqlConnection  con = null;
private static SqlConnection con = null;

con will be same.
rnsrAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Daniel WilsonConnect With a Mentor Commented:
Are you trying to use the same con variable regardless of which type it is?

If they both inherit from the same type, you can declare the variable as the parent type, then instantiate it as a child type.  Checking the inheritance ...
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx
and
http://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-open.html

Obviously the Microsoft version inherits system.Data.Common.DbConnection.  I can't tell from documentation about the MySql one.  If it does also, this would look like
private static DbConnection con = null;

if (ShouldUseMicrosoft) con = new SqlConnection();
if (ShouldUseMySql) con = new MySqlConnection();

Open in new window


Thereafter you could use con for any method calls defined on DbConnection.

Considering how many methods are specific to these DB connection classes, I don't think you'll find it as satisfactory as it theoretically should be.  But ... that at least gets you exploring the possibilities.
0
 
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
Variables are null until you initialize them. So the following is sufficient:

private static MySqlConnection  con;
private static SqlConnection con;
0
All Courses

From novice to tech pro — start learning today.