采用附加mdf文件的方式连接数据库时,将MDF文件放在App_Data文件夹下
然后设置DataDirectory
string dataDir = AppDomain.CurrentDomain.BaseDirectory; if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\")) { dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName; AppDomain.CurrentDomain.SetData("DataDirectory", dataDir); }
连接字符串:
<add name="App1ConnectionString" connectionString="Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\App1.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>