PixelPaul PixelPaul - 22 days ago 6
C# Question

Dapper stored procedure call to correct database

I have the following single database structure:

Server
|
Database
+ Tables
+ Programmability(stored procedures)


In which I am using the following method to make a stored procedure call using Dapper:

public List<Events> GetEvents()
{
using (var connection = new SqlConnection(SQLSettings.GetConnectionString()))
{
return connection.Query<Events>("GetEvents", commandType: CommandType.StoredProcedure).ToList();
}

}


But now I'm changing the back end to a multiple database structure, like:

Server
|
Database1
+ Tables
+ Programmability(stored procedures)
|
Database2
+ Tables
+ Programmability(stored procedures)


My question is, how do I need to modify my method to ensure so that it is hitting the correct database where the stored procedure resides?

Answer

You are ensuring it with proper connection string.

using (var connection = new SqlConnection("Connection string of the first db"))
{
    return connection.Query<Events>("GetEvents", commandType: CommandType.StoredProcedure).ToList();
}

using (var connection = new SqlConnection("Connection string of the second db"))
{
    return connection.Query<Events>("GetEvents", commandType: CommandType.StoredProcedure).ToList();
}