Solved

connection string c#

Posted on 2015-01-24
2
84 Views
Last Modified: 2015-02-24
How can I achieve below

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

con will be same.
0
Comment
Question by:rnsr
2 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 250 total points
ID: 40568203
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
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
ID: 40569433
Variables are null until you initialize them. So the following is sufficient:

private static MySqlConnection  con;
private static SqlConnection con;
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question