0

i have build my program with C# and the problem is that i use "Microsoft SQL Server Database File (SqlClient)" for my db and i wanted to make it portable because if i take all the project(sln) and give it to my friend to run it the database path is wrong of course..

So i have the database.mdf inside my directory of project C# so my question is, do you know a way to do it.

i have try to the connection string some patterns,

Data Source=.\SQLEXPRESS;AttachDbFilename="D:\blabla\blablaa\blablaaa\blablaaaa\WindowsFormsApplication2\WindowsFormsApplication2\WindowsFormsApplication2\WindowsFormsApplication2\Database1.mdf";Integrated Security=True;User Instance=True

so i did it like this

Data Source=.\SQLEXPRESS;AttachDbFilename="..\WindowsFormsApplication2\WindowsFormsApplication2\WindowsFormsApplication2\WindowsFormsApplication2\Database1.mdf";Integrated Security=True;User Instance=True

or

Data Source=.\SQLEXPRESS;AttachDbFilename="..\Database1.mdf";Integrated Security=True;User Instance=True

Any suggestions.

dbexec
  • 274
  • 9
  • 19
  • Perhaps `|DataDirectory|` ? http://stackoverflow.com/questions/1409358/ado-net-datadirectory-where-is-this-documented – Marc Gravell Jun 17 '13 at 11:31

1 Answers1

0

If you are wanting to run it from a memory stick for example, and your application is running on the memory stick, then you would do well to get the current location of the application:

Assembly.GetExecutingAssembly().Location

You could then use this to find the location of the database file if this will be fixed also.

Edmund Covington
  • 521
  • 6
  • 17