Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6919915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:04:56+00:00 2026-05-27T10:04:56+00:00

I realise that the title may be very confusing, so to explain what I

  • 0

I realise that the title may be very confusing, so to explain what I would like to do, I will illustrate how I can easily solve the same problem in PHP.

E.g. I give the user of my application a choice database types (i.e. MySQL, MSSQL, etc).

In my code I would like to have only one function for each operation rather than a function for each database as I explain below.

NOT

main.php

<?php
function mysql_get_users() {
  //Foo
}

function mssql_get_users() {
  //Foo
}

if($dbtype == "mysql") {
  echo mysql_get_users();
}

if($dbtype == "mssql") {
  echo mssql_get_users();
}

?>

BUT

main.php

<?php
if($dbtype == "mysql") {
  include("mysql.php");
}

if($dbtype == "mssql") {
  include("mssql.php");
}
echo get_users();
?>

mysql.php

<?php
function get_users() {
  //Foo
}
?>

mssql.php

<?php
function get_users() {
  //Foo
}
?>

Now I have the same problem, but in C# using Visual Studio 2010. Since I cannot do this:

 if(dbtype=="mysql") {
    public static MySqlConnection connect(String connectionString)
    {
        MySqlConnection connection = new MySqlConnection(connectionString);
        connection.Open();
        return connection;
    }
}

The big problem with the above is particularly that I cannot put an if-clause around a method(function), then I have to face the problems of the types which are unique to each database type amongst the fact that I have to include the returned connection variable in each method related to database function.

So, any thoughts on how to fix this and make it work like the PHP example above? I am neither an expert nor a newbie on C#, but I learn quite quickly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:04:57+00:00Added an answer on May 27, 2026 at 10:04 am

    Personally, I think you need to approach the problem differently. This is where object-oriented programming is your friend. Basically, you would abstract out the database-specific stuff so the calling code knows nothing about which database it is using. If you were doing it from scratch, here is a very over-simplified example:

    public interface IDatabase {
       void Connect(string ...);
       string Query(string ...);
    }
    
    public class MySQLDB : IDatabase {
       void Connect(string ...){
           MySqlConnection connection = new MySqlConnection(connectionString);
           ...
       }
    
        // same for query, etc
    }
    
    public class OracleDB : IDatabase {
       void Connect(string ...){
           // connect using Oracle library
           ...
       }
    
        // same for query, etc
    }
    

    So then, in your client code, you choose which type of database to instantiate, and then call the same functions regardless of what kind it is:

    IDatabase db;
    if (dbtype == "mysql")
       db = new MySqlDB();
    else if (dbtype == "oracle")
       db = new OracleDB();
    
    db.Connect(...);
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realise that the title of this question may be inaccurate but I was
I realize that the title may be somewhat confusing, so I apologize. Basically, this
I realise that you can open the google maps app ready for directions by
The title of this question is not very clear but I cannot explain what
I realise the title to this question may be vague but I am not
I realize that the title is a long one, but allow me to explain.
(I realize that my title is poor. If after reading the question you have
I realise that similar questions have been asked before however none of the solutions
I realise there might be similar questions but I couldn't find one that was
I realize that it can depend on certain things (and obviously how efficient the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.