-1

First of all I'd like to apologize because I feel like bothering you will all my stupid questions but I really have a problem.

I'd like to know if I can change the path where C# installs the database (as you all know you have to click on add in solution explorer and then go to serviced database and so a database will be automatically created into the project file and I want to change this automatic location) Is there any way that I can do that?

Secondly, if I can't change the path of an automatically adding a database I would like to change the way that |DataDirectory| acts. I'd like to change it's option of running the database in project/bin/Debug to running the program into the project file, automatically.

jarlh
  • 42,561
  • 8
  • 45
  • 63
Dabuleanu Catalin
  • 59
  • 1
  • 1
  • 15
  • You could change the connection string to point to one which is located in the project file instead of `DataDirectory`. – Salah Akbari May 11 '16 at 07:33
  • 1
    You should put the SQL Server database **on the server** where it belongs and then just use it by defining a server and (logical) database name in the connection string - and don't fiddle around with `.mdf` files in the file system - let SQL Server handle that for you .... – marc_s May 11 '16 at 07:37
  • I unerstand what you want to say @marc_s but the point of all of this "messing" around is that I will not work on my PC,and i won't have the possibility in installing anything else but Default settings/programs on that PC. And i can't be 100% sure that that PC will have SQL Server Express installed. That's why I am really nervous about this thing – Dabuleanu Catalin May 11 '16 at 07:53
  • If you want to use a `.mdf` file , then you *must have* SQL Server Express installed..... and since it's already installed anyway - why not use it **properly**? – marc_s May 11 '16 at 08:06
  • Because if im using this connection string: 'SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=Database1.mdf;Integrated Security=True;User Instance=True");' i get this error: An attempt to attach an auto-named database for file Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. – Dabuleanu Catalin May 11 '16 at 08:10

1 Answers1

1

Usually it is better to leave things at their default. So your connection string is stored in the app.config file (or web.config one for web apps). In this way it is easy to adjust the configuration when you deploy your app to your customer.

If that path is stored in the web.config or app.config and your app reads it at runtime then you could edit (manually or using a setup tool) the path at your customer location to match its constraints and the program will use the new configuration . The DataDirectory substitution string is a compromise to alleviate the setup reconfiguration problems; if you plan to change it via code then you face the same problem, because you still have to read the customer config somewhere.

Anyway, changing the location resolved by |DataDirectory| is easy enough from code.

AppDomain.CurrentDomain.SetData("DataDirectory", your_path_to_a_data_folder);

By the way, the advice from marc_s is a very good one. If you have Sql Server Express installed (and you need it also on the customer machine) then you add your MDF file to the database handled by Sql Server Express using its administrative tool (Sql Server Management Studio). This gives you a logical name for your db (not a file name with its changing path) and in your connection string you use that logical name INSTEAD OF the AttachDbFileName key

DataSource=.\SQLEXPRESS;Database=myDatabaseName; ....;

As an alternative, if you don't need to have a shared access to your database from many PC on the customer LAN then you could look at the LocalDB option for Sql Server Express. This option allows you to not install Sql Server Express to your customer PC but start the database services as a local dll for your own app.

See LocalDB deployment on local PC

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Hmmm.. I understand what you want to say but let's say I will do that on my pc,path being C:\Users\rBcollo\Desktop\Project\ .If i will run it on other PC(that obviously will have another username) I won't be able to run it properly,right? – Dabuleanu Catalin May 11 '16 at 07:49
  • Ok.I kinda managed to finished everything.But now i get this error: "Cannot open database "Database1.mdf" requested by the login. The login failed. Login failed for user 'rBcollo-PC\rBcollo'." And im not using any username or password at connecting the database [link](http://i.imgur.com/j554DMb.png) <- image – Dabuleanu Catalin May 11 '16 at 10:19