Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm wondering whether it's possible to use built in ASP.NET application services (aspnet_user, aspnet_role etc table) without specifying a connection string in a web.config.

At the moment I store connection strings externally, but I keep finding hard-coded connection strings all over the web.config xml, various providers etc. It's driving me crazy.

Thank you

share|improve this question
3  
Just a note: the concepts "hard-coded" and "data in a web.config" are mutually exclusive. Hard-Coded means that the information is stored inside the actual code, requiring a recompile to change. Putting data in a config file is the way to avoid "hard coding" information. Just semantics ;) – Chris Lively Jun 17 '10 at 19:31
Thanks for the feedback. I'll rephrase myself. What I was trying to say is that I don't like the application services adding connection strings into various config files "automatically". – CodeWorks Jun 18 '10 at 8:53

1 Answer

up vote 2 down vote accepted

You can write your own provider via overriding already existed, built-in class so it will read it's connection string from somewhere else:

public class MyMembershiProvider : SqlMembershiProvider
{
    public override void Initialize(string name, NameValueCollection config)
    {
        config["connectionString"] = "what ever you want";

        base.Initialize(name, config);
    }
}
share|improve this answer
Hey, thank you for this. This still means that application will look for different configuration files depending whether it's a web.config it's after or app.config. On top of this I have unit tests which seem to be using a conncetion string of their own. LINQ to SQL had connection strings built in some configuration files - I removed those. At the moment my main concern is asp net data providers. – CodeWorks Jun 18 '10 at 8:54
Spasibo za sovet %) – CodeWorks Jun 18 '10 at 8:56
@vikp: So you want to minimize the number of connection strings used in the application? You can specify not connectionString but connectionStringName so it will read it appropriate connection string from Web.config/connectionStrings node. Рад помочь! :) – abatishchev Jun 18 '10 at 9:44
@vikp: Also you can use special class to hold (manage) your connection string(s). I do so. public static class Database { public static SqlConnection GetConnection() { } } which holds your connection string(s) or reads them from Web.config or anywhere else – abatishchev Jun 18 '10 at 10:02
Sorry for the late reply. Was away. Yes I'd like to minimise the number of places where I store the connection strings. I'm not too keen on web.config or app.config. In my opinion too many things are done for you behind the scenes, which I don't like. I will use a helper class which will read the connection strings from encrypted XML config. Hopefully overhead of reading data from encrypted xml config file won't be too big. Thanks a lot! – CodeWorks Jun 24 '10 at 11:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.