public static void SaveObj(SpareEntities db, string type, string name, object obj) //将对象存入系统表 { //将对象流化到流 IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); formatter.Serialize(stream, obj); stream.Position = 0; // Read into a byte array byte[] blobdata = new Byte[stream.Length]; stream.Read(blobdata, 0, (int)stream.Length); stream.Close(); var config = db.TS_CONFIG.FirstOrDefault(p => p.GRPID == type.Trim() && p.KEYID == name.Trim()); if (config == null) { config = new TS_CONFIG() { GRPID = type.Trim(), KEYID = name.Trim(), DATA = blobdata }; } else { config.DATA = blobdata; } db.TS_CONFIG.AddOrUpdate(config); }
public static void SaveFile2DB(SpareEntities db, string type, string name, FileStream fs) { fs.Position = 0; byte[] blobdata = new Byte[fs.Length]; fs.Read(blobdata, 0, (int)fs.Length); fs.Close(); var obj = db.TS_CONFIG.FirstOrDefault(p => p.GRPID == type.Trim() && p.KEYID == name.Trim()); if (obj == null) { obj = new TS_CONFIG() { GRPID = type.Trim(), KEYID = name.Trim(), DATA = blobdata }; } db.TS_CONFIG.AddOrUpdate(obj); }