如果數據庫文件像下面那樣存在,請將此代碼嘗試到工作目錄。
d:\ HMProject \數據庫\ HMProject.sdf
string Path = Environment.CurrentDirectory;
string[] appPath = Path.Split(new string[] { "bin" }, StringSplitOptions.None);
AppDomain.CurrentDomain.SetData("DataDirectory", appPath[0]);
連接字符串.sdf文件
<add name="LocalDB" connectionString="metadata=res://*/Client.HMProject.csdl|res://*/Client.HMProject.ssdl|res://*/Client.HMProject.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\Database\HMProjectDB.sdf;Password=HMProject;Persist Security Info=False;"" providerName="System.Data.EntityClient" />
由於
ck.Nitin(丁丁)
你確定你有相對路徑嗎?你輸入的「F:\ My Documents \ Project1 \ bin \ Debug \ Data \ file.sdf」也是一個完整路徑。如果您的應用程序已經從「F:\ My Documents \ Project1 \ bin \」運行,則相對路徑將類似「\ data \ file.sdf」 – Jrud 2009-12-02 15:35:10