C# 如何让 LINQ to SQL 使用在运行时被修改的连接字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1019434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make LINQ to SQL use a connection string which is being modified at runtime?
提问by Nano Taboada
I'm experimenting some difficulties trying to use Connection String Builders (ADO.NET)within LINQ to SQL. Let me show you guys what I'm trying to do:
我正在尝试在 LINQ to SQL 中使用连接字符串生成器 (ADO.NET) 时遇到一些困难。让我向你们展示我正在尝试做的事情:
the app.configfile:
的的app.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="LoremIpsum"
connectionString="Data Source=SomeServer;Initial Catalog=SomeDB;User ID=joe;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
and a snippet of the form:
和表单的一个片段:
ConnectionStringSettings settings =
ConfigurationManager.ConnectionStrings["LoremIpsum"];
if (null != settings)
{
string connection = settings.ConnectionString;
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder(connection);
// passwordTextBox being the control where joe the user actually
// enters his credentials
builder.Password = passwordTextBox.Text;
}
LINQTOSQLDataClassDataContext db = new LINQTOSQLDataClassDataContext();
// finally some rather anecdotic LINQ sentence here:
var foo = db.Table.Single(bar => bar.Table == whatever);
On the other hand checking the Immediate Window:
另一方面,检查立即窗口:
?builder.ConnectionString
"Data Source=SomeServer;Initial Catalog=SomeDB;User ID=joe;Password=swordfish"
I'm always getting an exception: Login failed for user 'joe'. Any ideas? Thanks much in advance.
我总是遇到一个例外:用户“joe”登录失败。有任何想法吗?非常感谢。
采纳答案by Sean Aitken
It seems like you are trying to modify the connection string that is stored in the app.config file. When you use a no argument constructor for your data context, it reads what was configured at design time.
您似乎正在尝试修改存储在 app.config 文件中的连接字符串。当您为数据上下文使用无参数构造函数时,它会读取在设计时配置的内容。
Try injecting your modified connection string into the constructor of the DataContext:
尝试将修改后的连接字符串注入到 DataContext 的构造函数中:
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["LoremIpsum"];
SqlConnectionStringBuilder builder;
LINQTOSQLDataClassDataContext db;
if (null != settings)
{
string connection = settings.ConnectionString;
builder = new SqlConnectionStringBuilder(connection);
// passwordTextBox being the control where joe the user actually enters his credentials
builder.Password =passwordTextBox.Text;
db = new LINQTOSQLDataClassDataContext(builder.ConnectionString);
} }
回答by Ostemar
You're forgetting to send in the connectionstring to the DataContext constructor.
您忘记将连接字符串发送到 DataContext 构造函数。
Example:
例子:
LINQTOSQLDataClassDataContext db = new LINQTOSQLDataClassDataContext(builder.ConnectionString);
回答by jason
You can force a DataContext
to use a specific connection string with
您可以强制 aDataContext
使用特定的连接字符串
DataContext db = new DataContext(myConnectionString);
The parameterless DataContext
constructor will use a connection string from the App.config
file first, then the connection string set at compile time.
无参数DataContext
构造函数将首先使用App.config
文件中的连接字符串,然后是编译时设置的连接字符串。