2

我有一个连接到 MYSQL 数据库的应用程序。我使用实体框架来完成所有工作。现在,当我第一次安装时,我设置了实体,并产生了这样的连接字符串:

<connectionStrings>
<add name="networkingEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=lucian;persist security info=True;database=networking"" providerName="System.Data.EntityClient"/>
</connectionStrings>  

现在,我的应用程序有 2 个用户:一个管理员和一个学生。

Form1 => 从数据库和服务器更新信息

登录表单=> 用户认证

MainForm => 所有动作都参与其中。如果管理员已登录,他可以修改数据库中的内容

数据库:有 2 个用户:root 和 lucian。

“lucian”是受限用户...

现在,我的问题是:

如何向现有的连接字符串添加第二个连接字符串,并在运行时选择该连接字符串?我的意思是,当 Form1 运行时,让线程进入睡眠状态,选择第二个连接字符串,然后转到登录表单,以管理员身份登录并使 cnahge 进入数据库?

以及如何从外部文件中的连接字符串中获取登录信息?

4

2 回答 2

0

So this is how I have solved:
1. Modified the App.config like:

 <connectionstrings configsource="DatabaseConnectionDetails.config" />;

and this is DatabaseConnectionDetails.config:

<connectionstrings>
    <add name="networkingEntities" connectionstring="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=lucian;password=lucian;persist security info=True;database=networking&quot;" providername="System.Data.EntityClient" />
    <add name="networkingEntitiesAdmin" connectionstring="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=root;password=lucian;persist security info=True;database=networking&quot;" providername="System.Data.EntityClient" />
  </connectionstrings>

*2.*Added a contructor with string parameter to Model1.Context.tt template:

 public <#=code.Escape(container)#>(string myString)
        : base(myString)
    {
<#
if (!loader.IsLazyLoadingEnabled(container))
{
#>
        this.Configuration.LazyLoadingEnabled = false;
<#
}
#>
    }  

*3.*get the connection string like this:

string  str = ConfigurationManager.ConnectionStrings["networkingEntitiesAdmin"].ConnectionString;

*4.*And whenever I want to use the context , I use(for example):

networkingEntities net=new networkingEntities(str);


public List<utilizator> ListaUtilizatori()
        {

            var query = from u in net.utilizator
                        select u;
            List<utilizator> users = new List<utilizator>();
            try
            {
                users = query.ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return users;
        }

Now, I still have to discover how I encrypt the config files.... Thank you all for help...

于 2013-05-14T21:01:17.553 回答
0

As far as I can see you have used either EF DB first or model first as a result of which the connection string was added to the app.config by Entity Framework. If the second connection string should point to the same database, add a new DataModel to your project and point it to the database of your choice and generate a model from it after which, EF makes the connection string for you.

To target the second connection string.

// by name attribute from the app.config/web.config
ConfigurationManager.ConnectionStrings["networkingEntities"].ConnectionString; connection string.
// or select by index.
var index = 0;
var adminConnectionString = ConfigurationManager.ConntectionString[++index];
var userConnectionString = ConfigurationManager.ConnectionString[index];

Edit to your answer. Hi, because I too am interested in this topic I did a quick google and found this one: http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.protectsection.aspx It seems .NET has a few handy build in mechanism for us available. Tho I am also new to the topic I have no readily answer as to how it is used. GLHF

于 2013-05-14T20:03:39.067 回答