zoukankan      html  css  js  c++  java
  • BIN文件对象数据库,直接存储对象做数据库,小型项目用它准没错

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.IO;
     6 using System.Runtime.Serialization.Formatters.Binary;
     7 
     8 namespace WpfApplication1
     9 {
    10    public  static class FileDateDo
    11     {
    12 
    13 
    14         public static T getFileDate<T>(string filePath) where T : new()
    15         {
    16             if (!File.Exists(filePath))
    17             {
    18                 return default(T);
    19             }
    20             T t = new T(); 
    21             FileStream fs = new FileStream(filePath, FileMode.Open); 
    22             if (fs.Length < 1) { return t; }
    23             BinaryFormatter bf = new BinaryFormatter();
    24             t = (T)(bf.Deserialize(fs));
    25             fs.Close();
    26             return t;
    27 
    28 
    29         } 
    30         public static void Save<T>(string filePath, List<T> t)
    31         {
    32              
    33             try
    34             {
    35                 using (FileStream fss = new FileStream(filePath, FileMode.Create))
    36                 {
    37                     BinaryFormatter bs = new BinaryFormatter();
    38                     bs.Serialize(fss, t);
    39                 }
    40             }
    41             catch (Exception)
    42             {
    43 
    44                 throw;
    45             }
    46         }
    47  
    48 
    49       
    50          
    51     }
    52 }
    View Code

    缺点

    1.如果有10w条记录,修改一条你就需要重写这个BIN文件,开销相当大,另外如果只是查询一条记录你也需要重新将这个BIN所有内容加载到内存建立对应的对象。


    2.Access, SQL Server可以简单理解做了2件事情,第一他提供了统一界面,任何语言任何进程通过sql都可以与之通讯,获取数据。

    第二件事情他提供了索引机制,通过索引不需要加载所有数据到内存就能根据sql定位查询结果。

    3.修改对象结构会造成数据无法读取,需要保留原对象与新对象,将原数据读取至原对象,然后通过程序转换到新对象,将新对象重新保存在一个新文件(BIN)里面,最后原对象文件和原数据文件(BIN)删除。

    4.实时保存,不建议实时保存,等所有处理完毕延迟保存数据最好。

    但是如果你的应用很小,内存足够hold住数据,且不需要实时保存,那么此方法是可行的,而且可以说比用access都好,呵呵。

    具体问题具体分析,合适就好。

  • 相关阅读:
    bzoj2438
    bzoj3040
    [AHOI2009]维护序列
    [JSOI2008]最大数
    洛谷3378堆模板
    洛谷1439
    洛谷2756
    bzoj1016
    洛谷1875
    [模板] 强连通分量
  • 原文地址:https://www.cnblogs.com/jacd/p/4499062.html
Copyright © 2011-2022 走看看