0

I need a relative path for my SQL DB but I get some errors. Could somebody help me? The errors are on the bolded lines.

SqlConnection con = new SqlConnection("Data Source=|DataDirectory|\\MFdb.mdf");
string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
string path = System.IO.Path.GetDirectoryName(**executable**);
**AppDomain.CurrentDomain.SetData("DataDirectory", path);**
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • It greatly helps when you share all pertinent information. Error messages, for example. – ProgrammingLlama Apr 19 '22 at 11:26
  • It would help if you tell us what is the error. – McNets Apr 19 '22 at 11:26
  • Invalid token '(' in class, record, struct, or interface member declaration – Sebastian Filipovici Apr 19 '22 at 11:35
  • Invalid token '"DataDirectory"' in class, record, struct, or interface member declaration – Sebastian Filipovici Apr 19 '22 at 11:36
  • The name 'AppDomain.CurrentDomain.SetData' does not exist in the current context. – Sebastian Filipovici Apr 19 '22 at 11:36
  • The name 'path' does not exist in the current context. – Sebastian Filipovici Apr 19 '22 at 11:36
  • Mainly, I have errors on the "path", "executable", "AppDomain.CurrentDomain.SetData("DataDirectory", path)" – Sebastian Filipovici Apr 19 '22 at 11:38
  • As far as your connection string issue goes, I suggest you [use a SqlConnectionStringBuilder](https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder.attachdbfilename?view=dotnet-plat-ext-6.0) to help you get it right. Please also be aware that you can edit your question to include error messages, you shouldn't put them in comments. – Crowcoder Apr 19 '22 at 12:14
  • 2
    All of your error messages are syntax errors. They have nothing to do with the connection string. You need to fix them first. They should be pretty easy. If not, please learn more about the basic syntax of C#. Do note that you cannot have these double stars in your code. – siride Apr 19 '22 at 12:21
  • The connection string you used is only meant for SQL Server Express [User Instance databases](https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/sql-server-express-user-instances) *in web applications*. In these cases `|DataDirectory|` refers to the web app's `App_Data` directory. If the database file is in the same folder as the executable you don't need it. On the other hand, user instance databases aren't very scaleable and were replaced by [LocalDB](https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-express-localdb?view=sql-server-ver15) – Panagiotis Kanavos Apr 19 '22 at 12:26
  • You *don't* need the assembly's path, or call `.CurrentDomain.SetData("DataDirectory", path)`. You need to understand what the connection string you used does. SQL Server is *not* a file-based database like Access or SQLite. Both the old User Instance feature and LocalDB require SQL Server Express, although LocalDB can be installed without installing the entire product – Panagiotis Kanavos Apr 19 '22 at 12:29
  • Which dbms are you using? (There are dozens of different SQL DB's.) – jarlh Apr 19 '22 at 13:01

1 Answers1

0

Replace "Data Source=|DataDirectory|\MFdb.mdf" to @"Data Source=DataDirectory\MFdb.mdf"

Vadym K
  • 1
  • 1
  • Do you know what the [|DataDirectory|](https://stackoverflow.com/q/1409358/7444103) is or [how it's defined](https://stackoverflow.com/q/12187068/7444103)? – Jimi Apr 20 '22 at 18:51