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);