如何从两个连接字符串选择呢? [英] How to choose from two connection strings?

查看:147
本文介绍了如何从两个连接字符串选择呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,我曾经遇到过一个问题,使用C#和LINQ到实体两个连接字符串来选择。目前我有两个连接字符串分别是:

Good afternoon, I have encountered a problem to choose from two connection strings using C# and Linq to Entities. Currently I have two connection strings which are:

<add name="GameHutDBEntities1" connectionString="metadata=res://*/GameHutModel.csdl|res://*/GameHutModel.ssdl|res://*/GameHutModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=EMMANUEL-PC\SQLEXPRESS;Initial Catalog=GameHutDB;user id=GameHutAdmin; password=123;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
<add name="GameHutDBEntities2" connectionString="metadata=res://*/GameHutModel.csdl|res://*/GameHutModel.ssdl|res://*/GameHutModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=EMMANUEL-PC\SQLEXPRESS;Initial Catalog=GameHutDB;user id=test; password=1234;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

我有两个角色,这是Admin和办事员。我怎样才能让管理员与第一连接字符串登录,店员会与第二连接字符串登录?

I have two roles which are Admin and Clerk. How can I allow the admin to login with the first connection string and the clerk will login with the second connection string?

推荐答案

这个答案:

string connString;

if(IsAdmin(user))
    connString = ConfigurationManager.ConnectionStrings["GameHutDBEntities1"];
else
    connString = ConfigurationManager.ConnectionStrings["GameHutDBEntities2"];

using(EntityConnection con = new EntityConnection(connString))
{
  using (Entities context = new Entities(con))
  {
      // Some code here
  }
}

其中, IsAdmin 是采取用户,如果他是一个管理和虚假如果不返回true的功能。

Where IsAdmin is a function taking a user and returning true if he is an admin and false if not.

这篇关于如何从两个连接字符串选择呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆