Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am working on a project in visual studio...my project database is in sql server. i want to put my project database in my app data folder. i have a connectionstring which i used to connect to the database

C#
public static string connectionString = "Data Source=FAROOQPC\\SQLEXPRESS; Initial Catalog=shop; Integrated Security=True";


how to change that connection String after adding my project database in appdata folder of my project..
Posted

try this

C#
AppDomain.CurrentDomain.SetData("DataDirectory", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));

<connectionstrings>
  <add name="DATABASENAME">
       connectionString="Data source=DataDirectory|DATABASEFILENAME;"
       providerName="YOURPROVIDERNAME"/>
</add></connectionstrings>


Hope this will help you
 
Share this answer
 
Comments
Dean Oliver 25-Feb-12 4:06am    
first part is irrelevant.
C#
"Data Source=FAROOQPC\\SQLEXPRESS; Integrated Security=True;"AttachDbFilename=|DataDirectory|\shopDbFileName.mdf;Initial Catalog=shop"
 
Share this answer
 
v2
Comments
codegeekalpha 25-Feb-12 3:40am    
its now working...
Dean Oliver 25-Feb-12 4:05am    
If this solved your problem please mark as the solution. thanks.
itsmahadi007 23-Feb-21 6:57am    
this is correct answer but not for this question. This links keep the data in project folder which leads it the the program file. final exe file require the database file in appdata folder other wise the database mdf file won't be writeable.
is this problem is solved then refer me..........
 
Share this answer
 
Hi there..

Just try this..

C#
SqlConnection con=new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True");
 
Share this answer
 
gh <connectionstrings>
<add name="DataContext">
connectionString="Data source=Database.sdf;"
providerName="System.Data.SqlServerCe.4.0"/>
 
Share this answer
 
v2

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