我怎样才能让LINQ to SQL的使用已被修改在运行时连接字符串? [英] How can I make LINQ to SQL use a connection string which is being modified at runtime?

查看:157
本文介绍了我怎样才能让LINQ to SQL的使用已被修改在运行时连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一些困难,试图用 连接字符串生成器(ADO.NET)< /在LINQ STRONG> 到SQL。让我告诉你们我想要做的:

  

的app.config 文件:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&lt;结构&GT;
    &LT; configSections&GT;
    &LT; / configSections&GT;
    &LT;的ConnectionStrings&GT;
        &LT;添加名称=LoremIpsum
             的connectionString =数据源= SomeServer;初始目录= SomeDB;用户ID =乔
             的providerName =System.Data.SqlClient的/&GT;
    &LT; /的ConnectionStrings&GT;
&LT; /结构&gt;
 

  

和形式的一个片段:

  ConnectionStringSettings设置=
    ConfigurationManager.ConnectionStrings [LoremIpsum];
如果(NULL!=设置)
{
    字符串连接= settings.ConnectionString;
    SqlConnectionStringBuilder建设者=
         新SqlConnectionStringBuilder(连接);

    //密码框是控制乔用户实际上在哪里
    //进入他的证件
    builder.Password = passwordTextBox.Text;
}

LINQTOSQLDataClassDataContext DB =新LINQTOSQLDataClassDataContext();

//最后一些比较anecdotic LINQ一句话在这里:
无功富= db.Table.Single(巴=&GT; bar.Table ==等等);
 

  

在另一方面检查即时窗口

 ?builder.ConnectionString
数据源= SomeServer;初始目录= SomeDB;用户ID =乔;密码=箭鱼
 

我总是得到一个例外:用户登录失败'乔'。有任何想法吗?感谢很多提前。

解决方案

好像你正试图修改存储在app.config文件中的连接字符串。当您使用一个无参数的构造函数为您的数据背景下,读什么在设计时进行配置。

尝试注入您的修改后的连接串入的DataContext的构造函数:

  ConnectionStringSettings设置= ConfigurationManager.ConnectionStrings [LoremIpsum];
SqlConnectionStringBuilder建设者;
LINQTOSQLDataClassDataContext分贝;

如果(NULL!=设置)
{
    字符串连接= settings.ConnectionString;
    建设者=新SqlConnectionStringBuilder(连接);

   //密码框是乔的地方,用户实际进入了国书控制

    builder.Password = passwordTextBox.Text;
    DB =新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的使用已被修改在运行时连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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