I have in a Webapi application where SQL and MYSQL support
The implementation is very simple:
I created an Abstract class with the configuration of the two connections
Follows code:
https://gist.github.com/thiagoloureiro/7d8fab3eed2e5a6f8bb8639a0c65ff54
namespace Data.Dapper.Class
{
public abstract class BaseRepository
{
public string connstringSQL = ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString;
public MySqlConnection GetMySqlConnection(bool open = true,
bool convertZeroDatetime = false, bool allowZeroDatetime = false)
{
string cs = "Server=xxx;Port=41890;Database=yyyy;Uid=zzz;Pwd=db;";
var csb = new MySqlConnectionStringBuilder(cs)
{
AllowZeroDateTime = allowZeroDatetime,
ConvertZeroDateTime = convertZeroDatetime
};
var conn = new MySqlConnection(csb.ConnectionString);
if (open) conn.Open();
return conn;
}
}
}
There in the case of MYSQL:
using (var db = GetMySqlConnection())
{
const string sql = @"insert into User (Login, Password, CreatedOn, LastLogon) values (@Login, @Password, NOW(), NOW())";
db.Execute(sql, new { Login = username, Password = password }, commandType: CommandType.Text);
}
SQL Server:
using (var db = new SqlConnection(connstring))
{
const string sql = @"insert into [User] ([Login], [Password], [CreatedOn], [LastLogon]) values (@Login, @Password, GETDATE(), GETDATE())";
db.Execute(sql, new { Login = username, Password = password }, commandType: CommandType.Text);
}
Then in the command line of using I differentiate which DB I want to use.
On my Github I have some things where I used this code:
https://github.com/thiagoloureiro/WebAPIBase/
abs
if you have no problem with the query user or Dapper
– Marco Souza
Dapper can handle that? in my researches I saw that using Factorymethod, implemented in the Dbprovidersfactories class, can be used any class that implements Idbconnection. Here is the reference https://stackoverflow.com/questions/5898711/data-layer-abstract-factory, very old! but if you have an example of how I do this with daaper it would be very helpful!
– Anderson Souza