Reputation: 27
We have two database and single method. In our case queries for MySQL and Ms SQL db are different. We need to define when use query to needed db. How can I code in C# to find which db is using now?
Upvotes: 1
Views: 114
Reputation: 138
I think you can use something like this: make a new class for both methods you are using, in the mysql you put something like this, or whatever you have in your code
public void GetHighscore(int CurrScore)
{
MySqlConnection conn = new MySqlConnection("Server=localhost;Database=basket;Uid=root;pwd=;");
conn.Open();
MySqlCommand command = conn.CreateCommand();
command.CommandText = "select * from tblhighscore where id = 0";
command.Parameters.AddWithValue("@CurrScore", CurrScore);
MySqlDataReader reader = command.ExecuteReader();
conn.Close();
}
and for the ms sql you make another class with your own queries and stuff, i think the only difference should be the name of the sql, for example: MySql would be just sql.
You can call upon the mysql one with the following code, the dbConnect.GetHighscore is a reference to a piece of code in the mysql class, it will execute all the code in that piece of code.:
dbConnect clDb = new dbConnect();
dbConnect.GetHighscore;
Upvotes: 0
Reputation: 433
I would do the following:
Upvotes: 0
Reputation: 13959
You can use naive method to check version which will give sql server etc., This you can use both in sql server as well as mysql
SELECT @@version
Upvotes: 1
Reputation: 2626
If it needs to be in one method you could place using statements within one method, e.g.,
using(SqlConnection conn1 = new SqlConnection("FirstConnectionString"))
{
// your Query
}
using(SqlConnection conn2 = new SqlConnection("SecondConnectionString"))
{
// your Query
}
Where in First and Second Connections strings you define which database you refer to.
Upvotes: 0