Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Pls someone tell that how to access database file from App_data folder i have used this connectionstring in c# file

C#
SqlConnection conn = new SqlConnection("Server=.\\sqlexpress; AttachDbFileName=|DataDirectory|Web2011.mdf; Trusted_Connection=yes");



but this connectionstring is not running, Pls someone tell that what is wrong in this connectionstring.
Posted
Updated 16-Sep-11 6:46am
v3
Comments
Orcun Iyigun 16-Sep-11 12:46pm    
Updated the tags.
nikhil bhatt 07 2-Jul-14 16:49pm    
okk but now i am hosting my website so this connection string is valid for that...??

Keep in mind that App_Data allows you to use a file based SQL Server database :)

You can try this:

Choose "Add New Data Source" from the Data menu.[And follow the connection wizard]
If you don't have a Data menu, do the following:
*Click on Tools >>Connect to Database...
*Select "Microsoft SQL Server Database File", take the default Data provider, and click OK
*On the next screen, browse to your Database file, which will be in your VS Solution folder structure somewhere.
Test the connection. If you want to add the string to the web.config, click the Advanced button, and copy the Data Source line (at the bottom of the dialog box), and paste it into a connection string in the appropriate place in the web.config file. You will have to add the "AttachDbFilename" attribute and value. Example:

The raw text from the Advanced panel:

Data Source=.\SQLEXPRESS;Integrated Security=True;Connect Timeout=30;User Instance=True The actual entry in the web.config:

C#
<add name="SomeDataBase" connectionstring="Data Source=.\SQLEXPRESS;  AttachDbFilename=C:\Development\blahBlah\App_Data\SomeDataFile.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True" /> 


- or -

In your Solution Add New Project (Database Project)
While adding this project, it asks for database connection, provide your databse connection, or you can specify default database later using Set Default Reference.
You can find this facility by right clicking on database project. now your asp.net allow u to run query from visual studio..

And another good place to check is the following link.
VB
http://connectionstrings.com/


Good luck
 
Share this answer
 
v3
http://connectionstrings.com/[^]

this link will be useful for u
 
Share this answer
 
v2
Comments
AditSheth 17-Sep-11 1:39am    
its a best link to find Connection string..
And here you can find Connection string for Sql .mdf file http://www.connectionstrings.com/sql-server-2005
Thanks
rkthiyagarajan 17-Sep-11 4:50am    
Nice Adit... Usefull link
You forgot to add the db name in the connection string i.e.,


"Server=.\\sqlexpress; AttachDbFileName=|DataDirectory|Web2011.mdf; Trusted_Connection=yes;Database =dbname"

Just try with this & Reply me
 
Share this answer
 
v2
You Use connectionString Web.config....(Based on App_Data Folder)

Web.config..

XML
<connectionStrings>
 
   <add name="OsmEmpConString" connectionString="Data Source=.\sqlexpress;Integrated Security=SSPI;AttachDBFilename=C:\Documents and Settings\Administrator\My Documents\Downloads\Rags_Modified\Rags\Rags\App_Data\Reg.mdf;User Instance=true" providerName="System.Data.SqlClient"/>

   <add name="SomeDataBase" connectionString="Data Source=.\SQLEXPRESS; AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\Downloads\Rags_Modified\Rags\Rags\App_Data\Reg.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True" providerName="System.Data.SqlClient"/>

 </connectionStrings>


If you use in Web.config whenever you want connectionString just call this code behind code...

C# Codebehind...

C#
string constr = ConfigurationManager.ConnectionStrings["SomeDataBase"].ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900