简体   繁体   中英

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?

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();
}

Assuming you know in which DB (1 or 2) to find the SP, use either of these:

using (var connection = new SqlConnection(SQLSettings.GetConnectionString1()))
{ ... }

or

using (var connection = new SqlConnection(SQLSettings.GetConnectionString2()))
{ ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM