简体   繁体   English

Dapper存储过程调用以更正数据库

[英]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: 在其中,我使用以下方法使用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: 假设您知道在哪个DB(1或2)中找到SP,请使用以下任一方法:

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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