?
Solved

connection string c#

Posted on 2015-01-24
2
Medium Priority
?
98 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 750 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 750 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

752 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