2016-11-17 31 views
0

我有以下的單數據庫結構:小巧玲瓏的存儲過程調用正確的數據庫

Server 
    | 
    Database 
    + Tables 
    + Programmability(stored procedures) 

在裏面我是用下面的方法採用精緻小巧,使存儲過程調用:

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

} 

但現在我正在將後端更改爲多個數據庫結構,如:

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

我的問題我s,我如何修改我的方法以確保它正在存儲過程所在的正確數據庫?

+0

哪個DB它使用是基於連接字符串。 – juharr

+0

我覺得這很簡單。感謝所有人的快速回復。 – PixelPaul

回答

1

您正在通過正確的連接字符串來確保它。

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

假設你知道在哪個DB(1或2)找到SP,請使用以下兩種:

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

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