這邊會用到 System.Data.SqlServerCe
需支援 Visual Studio 2010 SP1
// path = 欲建立檔案的路徑 // 附註 這邊的路徑中 有用到 反斜線 \ 都得用兩個 反斜,否則會出現 "無法辨認的逸出序列" 的錯誤 // 關於這個錯誤可以參考 http://msdn.microsoft.com/zh-tw/library/44ezxxy3 string path = "D:\\Documents and Settings\\Bisger\\My Documents\\WindowsCE My Documents\\test.sdf"; string constrpda = "DataSource=" + path; if (!File.Exists(path)) // 如果路徑path的檔案不存在則執行 { SqlCeEngine engine = new SqlCeEngine(constrpda); engine.CreateDatabase(); } // 這邊也補上刪除檔案的指令 File.Delete(path);
如果出現 命名空間 'System.Data' 中沒有型別或命名空間名稱 'SqlServerCe' (您是否遺漏了組件參考?)
該組件存在 system.data.sqlserverce.dll,其路徑為
『C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Desktop\System.Data.SqlServerCe.dll』,視版本,4.0 存在
『C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Desktop\System.Data.SqlServerCe.dll』,只要將這個 DLL 加入參考就可以正常使用了。
32位元版則在『C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v3.5\Desktop\System.Data.SqlServerCe.dll』與
『C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v4.0\Desktop\System.Data.SqlServerCe.dll』
若找不到以上所述路徑,代表你的電腦沒有裝 SQLCE 啦!
請至 Microsoft Download Center 下載。3.5版、4.0版
文章參考來源
沒有留言:
張貼留言