Reputation: 9840
I have created a ASP.NET C# MVC
project. I am trying to connect it to a MSSQL
DB. In the Web.config
file i added the following;
<connectionStrings>
<add name="sdbconnection" providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\o\documents\visual studio 2012\Projects\ppl\ppl\App_Data\ppldb.mdf";Integrated Security=True" />
</connectionStrings>
But, the above code is incorrect ; I want it to be as follows;
<connectionStrings>
<add name="sdbconnection" providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|ppldb.mdf;Integrated Security=True" />
</connectionStrings>
But What is DataDirectory
? Where do i specify it ?
What is name="sdbconnection"
? Can i give any name that i want ?
Upvotes: 0
Views: 822
Reputation: 5552
Name is use to identify and use code your applications Please refer this article http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config Data Directory is usually App_Data folder and in web.config file name is use to use connection string in the application
Ex:String conn=ConfigurationManager.ConnectionStrings["Your name at web.config file"].ConnectionString;
or String conn=ConfigurationManager.ConnectionStrings["Your name at web.config file"].toString();
Upvotes: 0
Reputation: 19830
First question Quoting MSDN:
The |DataDirectory| portion of the connection string specifies that the MDF file is located inthe App_Data directory
Moreover you can change it using following function:
AppDomain.CurrentDomain.SetData("DataDirectory", "D:\database");
Second question name="sdbconnection" allows you to use specified conection string. So in your code you can write:
string connectionString = ConfigurationManager.ConnectionStrings["sdbconnection"].ConnectionString;
to acces this connection string
Upvotes: 2