Solved

connection string c#

Posted on 2015-01-24
2
80 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Query (QBE) Using VBA Function for Criteria 13 57
Problem to file 3 67
Question to Pivot table 1 33
How do ASP.NET and MVC work together? 4 25
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

943 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now