(轉載請註明來源:cnblogs coder-fang) sql
1. 項目添加package:數據庫
2. 建立好sqlce數據庫並設計好關係表,在項目中添加ado.net 實體數據模型,並鏈接到此數據 庫。app
3. 若是不想在app.config暴露數據庫敏感字段(如密碼)時, 在建立實體模型時,進行以下選擇:ide
4. 修改自動生成的Context.cs:函數
5. 在此文件中,增長一個構造函數 :ui
6.建立sqlcceprovider,填寫鏈接字串:this
public abstract class SqlceProvider { protected const string DBUtilVersion = "1.0"; protected EntityConnectionStringBuilder ecb; protected EntityConnectionStringBuilder ecbReadOnly; public SqlceProvider(string dataSource) { ecb = new EntityConnectionStringBuilder() { Metadata = "res://*/DAO.SQLCEStorage.csdl|res://*/DAO.SQLCEStorage.ssdl|res://*/DAO.SQLCEStorage.msl", Provider = "System.Data.SqlServerCe.4.0", ProviderConnectionString = "Data Source=" + dataSource +";"+ "Default Lock Timeout=2000;Persist Security Info=false;Max Database Size=4091;Password=123456;" }; } protected String FindExceptMsg(Exception e) { return e.InnerException == null ? e.Message : FindExceptMsg(e.InnerException); } }
7. 建立子類,並實現業務數據的增刪改查:spa
public partial class SqlceStorageProvider : SqlceProvider, IStorage, IDisposable { static Object objCreate = new Object(); Entities db; public SqlceStorageProvider(string dataSource = "./Store.db") : base(dataSource) { db = new Entities(ecb.ConnectionString); try { Config v = db.Config.Find("DBVersion"); if (!v.value.Equals(DBUtilVersion)) { throw new Exception("Database version is not compatible"); } } catch (DbEntityValidationException e) { throw new DBException(e.HResult, (e.EntityValidationErrors.Count() > 0) ? e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage : e.Message); } catch (Exception e) { throw new DBException(e.HResult, FindExceptMsg(e)); } } public List<Patient> FindPatient(int page, int size, out int total, out DBException err, Patient search = null) { List<Patient> ret = null; total = 0; err = new DBException(); lock (this) { try { var query = from p in db.Patient select p; if (search != null) { if (search.FamilyName != null) { query = query.Where(p => p.FamilyName.StartsWith(search.FamilyName)); } } total = query.Count(); return query.OrderByDescending(p => p.id).Skip((page - 1) * size).Take(size).ToList(); } catch (DbEntityValidationException e) { err.Code = e.HResult; err.Msg = (e.EntityValidationErrors.Count() > 0) ? e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage : e.Message; } catch (Exception e) { err.Code = e.HResult; err.Msg = FindExceptMsg(e); } return ret; } } public int SaveOrChangePatient(Patient p, out DBException err) { int ret = -1; err = new DBException(); lock (this) { try { if (p.id > 0) { db.Entry(p).State = EntityState.Modified; } else { db.Patient.Add(p); } db.SaveChanges(); return p.id; } catch (DbEntityValidationException e) { err.Code = e.HResult; err.Msg = (e.EntityValidationErrors.Count() > 0) ? e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage : e.Message; } catch (Exception e) { err.Code = e.HResult; err.Msg = FindExceptMsg(e); } return ret; } } public Boolean RemovePatient(Patient p, out DBException err) { bool ret = false; err = new DBException(); lock (this) { try { db.Entry(p).State = EntityState.Deleted; db.SaveChanges(); return true; } catch (DbEntityValidationException e) { err.Code = e.HResult; err.Msg = (e.EntityValidationErrors.Count() > 0) ? e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage : e.Message; } catch (Exception e) { err.Code = e.HResult; err.Msg = FindExceptMsg(e); } return ret; } } }
8. 級聯查詢代碼示例:.net
var query = from s in db.Study.Include("Patient") select s; if (search.Patname != "") { query = query.Where(s => s.Patient.FamilyName.StartsWith(search.Patname)); }
9.經過EF實現SQLCE操做已完成,直接建立DB實例,便可對數據庫進行操做。設計