LINQ to SQL中的ConnectionString [英] LINQ to SQL connectionstring

查看:153
本文介绍了LINQ to SQL中的ConnectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我希望能够配置连接字符串我的LINQ to SQL的应用程序。我试过很多不同的方式,但似乎无法得到它的工作。我想在当应用程序被运行的代码动态地做到这一点,这样做的原因是,用户可以更改连接设置。

I have an application that I want to be able to configure the connection string for my LINQ to SQL. I've tried so many different ways but cannot seem to get it working. I want to do this dynamically in the code when the app is run, the reason for this is that the user can change the connection settings.

如果我删除的connectionString出App.config中仍然工作正常(连通)应用程序,它使得我不知道,我应该改变连接字符串?

If I delete the connectionString out of app.config the application still works OK (communicating) which makes me wonder where I should be changing the connection string?

推荐答案

我认为要做到这一点的最好办法是阿尔滨的和RUP的答案的组合。您可以在配置文件中的值,然后在运行时读取它,它养活上下文构造函数,像这样:

I think that the best way to do it is a combination of Albin's and Rup's answers. Have a value in the config file, and then read it at run time and feed it to the context constructor, something like this:

WEB.CONFIG:

<appSettings>
<add key="ConString" Value="The connection string" />



代码:

//read value from config
var DBConnString = System.Configuration.ConfigurationManager.AppSettings("ConString");

//open connection
var dataContext= new MyDataContext(sDBConnString)

这样,你甚至可以在运行时更改连接字符串,它会工作,改变在运行的程序。

this way you can change the connection string even at runtime and it will work and change on the running program.

这篇关于LINQ to SQL中的ConnectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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