views:

1220

answers:

3

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.

+5  A: 

You're forgetting to send in the connectionstring to the DataContext constructor.

Example:

LINQTOSQLDataClassDataContext db = new LINQTOSQLDataClassDataContext(builder.ConnectionString);
Marpe
Thanks for your comment Marpe! It's quite possible I'm probably forgetting that. Would you mind giving an example of how to do it?
Nano Taboada
Ah, I just read in the old revisions of your question what your problem was. Your last sentence seems to have been cut out - the one that says: "I'm always getting an exception: Login failed for user 'joe'".The reason for this is because if you don't provide your newly created connection string, the datacontext constructor you're using will go directly to the ConfigurationManager.ConnectionStrings collection and pick out the one specified in the DBML file (and in your case you have no password for Joe stored in the config).
Marpe
Just pass it in to the constructor as: LINQTOSQLDataClassDataContext db = new LINQTOSQLDataClassDataContext(builder.ConnectionString);
Marpe
For some reason IntelliSense is disagreeing with us: The name 'builder' does not exist in the current context
Nano Taboada
Update: adding the constructor within de IF solves that error.
Nano Taboada
True - I gave the example out of context. However - be aware of that in your original code if the if-condition is false (ie. you haven't got the LoremIpsum connection strings in your config) your code would go on to create the data context using the connection string used when designing the DBML. And if that is present you might get surprises. And if it is not present you will get an exception.I would consider reversing the condition in the if clause and add your error handling code there (throw your own exception for example). And then, after the if statement do all initialization code.
Marpe
+5  A: 

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;
SqlConnectionStringBuilder 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); } }

CleverCoder
Thanks much your advice CleverCoder!
Nano Taboada
+2  A: 

You can force a DataContext to use a specific connection string with

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.

Jason