0

updated question

How do I specify the data source to my local MSSQL server?

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(localhost)\SQLEXPRESS;Initial Catalog=bladb;Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings>

I keep getting a "data source not found" exception no matter what I try.

1
  • I don't think it becomes much easier than this. I know much more complex ways of defining datacontexts though.... Commented Dec 28, 2012 at 20:39

4 Answers 4

2

Yep. Your application needs to know the location of the database, the username, and password to authenticate with.

You call this a magic string but I don't know what other magic method you are thinking of where your application could automatically know the IP Address of your database, the Username, and the Password.

Most of the time when you setup your database they create this connection string for you so you can just copy and paste it in. It is just a one time thing so it shouldn't be too big of a deal.

Sign up to request clarification or add additional context in comments.

3 Comments

mkay i'll back down then :).
whoops, pressed enter by accident. I tried my best to fill in the details but it keeps throwing this exception: Connection string "Data Source=(LocalDb)\v11.0;Initial Catalog=Users;Integrated Security=True;" was not found. I also tried "localhost" as data source but that doesn't work either. Could you give me a few pointers on what I should fill in there? Many thx!
Data Source=(local)\SQLEXPRESS, Data Source=(localhost)\SQLEXPRESS, Data Source=(ws050129)\SQLEXPRESS they all fail with "connection string not found". Any advise on figuring out how the hell to specify the data source?
1

You don't put parenthesis around localhost; it's just for (local) or (LocalDb)\v11.0. For localhost, it would be: DataSource=localhost for the default, unnamed local database (if one exists, which is not necessarily) or DataSource=localhost\SQLExpress for the default named installation of SQL-Server Express.

See http://www.connectionstrings.com/ for more info.

Comments

1

if you define your connection string section in the web.config as

<connectionStrings configSource="connections.config"/>

It will point to a separate config file, just containing the connection strings.

You can then add connection strings in code via the code below. Note the advantage of this is that is does not have to reboot the website. Happy days...

 using (StreamWriter writer = File.CreateText(@"c:\code\AVIVA_site\connections.config"))
            {
                writer.WriteLine("<connectionStrings>");
                for (int i = 0; name.Length > i; i++)
                {
                    writer.WriteLine("<add name=\"{0}\" connectionString=\"{1}\" providerName=\"{2}\" />", name[i], conString[i], provider[i]);
                }
                writer.WriteLine("</connectionStrings>");
                writer.Close();
            }

Comments

0

For the love of god...

if (!WebSecurity.Initialized)
{
    WebSecurity.InitializeDatabaseConnection(
        "DefaultConnection",
        "Users",
        "UserId",
        "UserName",
        true);
}

Instead of adding "DefaultConnection" I added the Configuration.ConnectionStrings["DefaultConnection"] instead.

The problem wasn't in the connection string in the web.config, but in the initializedatabase call...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.