简体   繁体   中英

How to set sqlconnecton to datadirectory like in ASP.Net. i'm using C# form

This is my current sql connection

Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Kenneth\VisualStudioProjects\Emp - Trainee\Emp - Trainee\sample.mdf;Integrated Security=True;User Instance=True

If I change AttachDbFilename=|Datadirectory|sample.mdf it doesn't work because it only works on website ASP.NET

在C#代码中设置DataDirectory

AppDomain.CurrentDomain.SetData("DataDirectory", @"C:\Kenneth\VisualStudioProjects\Emp - Trainee\Emp - Trainee\");

The DataDirectory unless set using:

AppDomain.CurrentDomain.SetData("DataDirectory", "c:\\MyDataDirectory");

would be pointing at the application folder. If debugging the application from Visual Studio, this would be the bin\\Debug folder. Try putting your database in the same place where your compiled exe is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM