currently i create dynamic database using CreateDatabase() method in Entity Framework. it executes sucessfully and it creates database at following path: "c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA" but i want to create database in my application's directory. how to do this? please help me.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Normally all databases are created on default path configured in SQL Server. Only if you are using SQL Server Express and if you define connection string which specifies database to be created in your data folder it will use your application folder. It will work only with SQL Server Express. Make sure that inner part of connection string looks like:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|\YourDbFile.mdf;Database=YourDbName; Integrated Security=SSPI;
Where |DataDirectory|
placeholder which will be replaced by path to your data directory. For web application it should be App_Data and for standalone applications it should be application's folder. You can also use any path directly in connection string or control DataDirectory resolution from code by using:
AppDomain.CurrentDomain.SetData("DataDirectory", YourPath);