问题描述
我在尝试使用 连接字符串生成器(ADO.NET) 在 LINQ to SQL 中.让我向你们展示我正在尝试做的事情:
<块引用>app.config 文件:
<?xml version="1.0" encoding="utf-8" ?><配置><配置部分></configSections><连接字符串><添加名称="LoremIpsum"connectionString="数据源=SomeServer;初始目录=SomeDB;用户ID=joe;"providerName="System.Data.SqlClient"/></connectionStrings></配置>
<块引用>
以及表单的一个片段:
ConnectionStringSettings 设置 =ConfigurationManager.ConnectionStrings["LoremIpsum"];如果(空!=设置){字符串连接 = settings.ConnectionString;SqlConnectionStringBuilder 构建器 =新的 SqlConnectionStringBuilder(连接);//passwordTextBox 是用户实际所在的控件//输入他的凭据builder.Password = passwordTextBox.Text;}LINQTOSQLDataClassDataContext db = new LINQTOSQLDataClassDataContext();//最后是一些相当有趣的 LINQ 语句:var foo = db.Table.Single(bar => bar.Table == 不管);
<块引用>
另一方面检查即时窗口:
?builder.ConnectionString数据源=SomeServer;初始目录=SomeDB;用户ID=joe;密码=swordfish"
我总是遇到异常:用户joe"登录失败.有任何想法吗?非常感谢.
您似乎正在尝试修改存储在 app.config 文件中的连接字符串.当您对数据上下文使用无参数构造函数时,它会读取在设计时配置的内容.
尝试将修改后的连接字符串注入 DataContext 的构造函数:
ConnectionStringSettings 设置 = ConfigurationManager.ConnectionStrings["LoremIpsum"];SqlConnectionStringBuilder 构建器;LINQTOSQLDataClassDataContext 数据库;如果(空!=设置){字符串连接 = settings.ConnectionString;builder = new SqlConnectionStringBuilder(connection);//passwordTextBox 是用户实际输入凭据的控件builder.Password =passwordTextBox.Text;db = new LINQTOSQLDataClassDataContext(builder.ConnectionString);} }
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:
the app.config file:
<?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.
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.
Try injecting your modified connection string into the constructor of the 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);
} }
这篇关于如何使 LINQ to SQL 使用在运行时修改的连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!