[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 105
  • Last Modified:

connection string c#

How can I achieve below

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

con will be same.
0
rnsr
Asked:
rnsr
2 Solutions
 
Daniel WilsonCommented:
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)Commented:
Variables are null until you initialize them. So the following is sufficient:

private static MySqlConnection  con;
private static SqlConnection con;
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now