标签:
1 public class STSDBHelper 2 { 3 4 private string dataName; 5 private static object syncRoot = new object(); 6 /// <summary> 7 /// 数据库名称 8 /// </summary> 9 /// <param name="DataName"></param> 10 public STSDBHelper(string DataName) 11 { 12 dataName = DataName+".db"; 13 } 14 /// <summary> 15 /// 添加数据 16 /// </summary> 17 /// <typeparam name="K"></typeparam> 18 /// <param name="Category"></param> 19 /// <param name="Key"></param> 20 /// <param name="Data"></param> 21 public void Add<K>(string Category, K Key, object Data) 22 { 23 Add<K, object>(Category, new List<KeyValuePair<K, object>> { new KeyValuePair<K, object>(Key, Data) }); 24 } 25 /// <summary> 26 /// 批量添加数据 27 /// </summary> 28 /// <typeparam name="K"></typeparam> 29 /// <typeparam name="V"></typeparam> 30 /// <param name="Category"></param> 31 /// <param name="Items"></param> 32 /// <param name="ExpirationDate"></param> 33 public void Add<K, V>(string Category, IEnumerable<KeyValuePair<K, V>> Items) 34 { 35 lock (syncRoot) 36 { 37 using (IStorageEngine engine = STSdb.FromFile(dataName)) 38 { 39 var table = engine.OpenXTable<K, string>(Category); 40 Items.ForEach(i => 41 { 42 var key = i.Key; 43 var data = i.Value; 44 var result = typeof(V) == typeof(string) ? data as string : JSON.Instance.ToJSON(data); 45 table[key] = result; 46 }); 47 engine.Commit(); 48 } 49 } 50 } 51 52 53 54 /// <summary> 55 /// 获取数据 56 /// </summary> 57 /// <typeparam name="K"></typeparam> 58 /// <typeparam name="V"></typeparam> 59 /// <param name="Category"></param> 60 /// <param name="Keys"></param> 61 /// <returns></returns> 62 public List<KeyValuePair<K, V>> Get<K, V>(string Category, IEnumerable<K> Keys) 63 { 64 var result = new List<KeyValuePair<K, V>>(); 65 lock (syncRoot) 66 { 67 using (IStorageEngine engine = STSdb.FromFile(dataName)) 68 { 69 var table = engine.OpenXTable<K, string>(Category); 70 Keys.ForEach(key => 71 { 72 string buffer; 73 V value; 74 if (table.TryGet(key, out buffer)) 75 value = typeof(V) == typeof(string) ? (V)(object)buffer : JSON.Instance.ToObject<V>(buffer); 76 else 77 value = default(V); 78 79 result.Add(new KeyValuePair<K, V>(key, value)); 80 }); 81 } 82 } 83 return result; 84 } 85 /// <summary> 86 /// 获取数据 87 /// </summary> 88 /// <typeparam name="K"></typeparam> 89 /// <typeparam name="V"></typeparam> 90 /// <param name="Category"></param> 91 /// <param name="Key"></param> 92 /// <returns></returns> 93 public V Get<K, V>(string Category, K Key) 94 { 95 var buffer = Get<K, V>(Category, new K[] { Key }); 96 var result = buffer.FirstOrDefault(); 97 return result.Value; 98 } 99 public string Find<K>(string Category, K Key) 100 { 101 102 lock (syncRoot) 103 { 104 using (IStorageEngine engine = STSdb.FromFile(dataName)) 105 { 106 var table = engine.OpenXTable<K, string>(Category); 107 108 return table.Find(Key); 109 } 110 } 111 112 } 113 114 /// <summary> 115 /// 以升序遍历表的每一行 116 /// </summary> 117 /// <typeparam name="K"></typeparam> 118 /// <typeparam name="V"></typeparam> 119 /// <param name="Category"></param> 120 /// <param name="Key"></param> 121 /// <returns></returns> 122 public IEnumerable<KeyValuePair<K, V>> Forward<K, V>(string Category, K Key) 123 { 124 lock (syncRoot) 125 { 126 using (IStorageEngine engine = STSdb.FromFile(dataName)) 127 { 128 var table = engine.OpenXTable<K, V>(Category); 129 return table.Forward(Key, true, Key, true); 130 131 } 132 } 133 134 } 135 /// <summary> 136 /// 以降序遍历表的每一行 137 /// </summary> 138 /// <typeparam name="K"></typeparam> 139 /// <typeparam name="V"></typeparam> 140 /// <param name="Category"></param> 141 /// <param name="Key"></param> 142 /// <returns></returns> 143 public IEnumerable<KeyValuePair<K, V>> Backward<K, V>(string Category, K Key) 144 { 145 lock (syncRoot) 146 { 147 using (IStorageEngine engine = STSdb.FromFile(dataName)) 148 { 149 var table = engine.OpenXTable<K, V>(Category); 150 return table.Backward(Key, true, Key, true); 151 152 } 153 } 154 155 } 156 /// <summary> 157 /// 返回最小Key的行 158 /// </summary> 159 /// <typeparam name="K"></typeparam> 160 /// <typeparam name="V"></typeparam> 161 /// <param name="Category"></param> 162 /// <param name="Key"></param> 163 /// <returns></returns> 164 public KeyValuePair<K, V> FirstRow<K, V>(string Category, K Key) 165 { 166 lock (syncRoot) 167 { 168 using (IStorageEngine engine = STSdb.FromFile(dataName)) 169 { 170 var table = engine.OpenXTable<K, V>(Category); 171 return table.FirstRow; 172 173 } 174 } 175 176 } 177 /// <summary> 178 ///最大Key行 179 /// </summary> 180 /// <typeparam name="K"></typeparam> 181 /// <typeparam name="V"></typeparam> 182 /// <param name="Category"></param> 183 /// <param name="Key"></param> 184 /// <returns></returns> 185 public KeyValuePair<K, V> LastRow<K, V>(string Category, K Key) 186 { 187 lock (syncRoot) 188 { 189 using (IStorageEngine engine = STSdb.FromFile(dataName)) 190 { 191 var table = engine.OpenXTable<K, V>(Category); 192 return table.LastRow; 193 194 } 195 } 196 197 } 198 /// <summary> 199 /// 返回大于等于指定Key的第一行 200 /// </summary> 201 /// <typeparam name="K"></typeparam> 202 /// <typeparam name="V"></typeparam> 203 /// <param name="Category"></param> 204 /// <param name="Key"></param> 205 /// <returns></returns> 206 public KeyValuePair<K, V>? FindNext<K, V>(string Category, K Key) 207 { 208 lock (syncRoot) 209 { 210 using (IStorageEngine engine = STSdb.FromFile(dataName)) 211 { 212 var table = engine.OpenXTable<K, V>(Category); 213 return table.FindNext(Key); 214 } 215 } 216 } 217 /// <summary> 218 /// 返回大于指定Key的第一行 219 /// </summary> 220 /// <typeparam name="K"></typeparam> 221 /// <typeparam name="V"></typeparam> 222 /// <param name="Category"></param> 223 /// <param name="Key"></param> 224 /// <returns></returns> 225 public KeyValuePair<K, V>? FindAfter<K, V>(string Category, K Key) 226 { 227 lock (syncRoot) 228 { 229 using (IStorageEngine engine = STSdb.FromFile(dataName)) 230 { 231 var table = engine.OpenXTable<K, V>(Category); 232 return table.FindNext(Key); 233 234 } 235 } 236 237 } 238 /// <summary> 239 /// 返回小于指定Key的第一行 240 /// </summary> 241 /// <typeparam name="K"></typeparam> 242 /// <typeparam name="V"></typeparam> 243 /// <param name="Category"></param> 244 /// <param name="Key"></param> 245 /// <returns></returns> 246 public KeyValuePair<K, V>? FindPrev<K, V>(string Category, K Key) 247 { 248 lock (syncRoot) 249 { 250 using (IStorageEngine engine = STSdb.FromFile(dataName)) 251 { 252 var table = engine.OpenXTable<K, V>(Category); 253 return table.FindBefore(Key); 254 255 } 256 } 257 258 } 259 260 /// <summary> 261 /// 分页加载数据 262 /// </summary> 263 /// <typeparam name="K"></typeparam> 264 /// <typeparam name="V"></typeparam> 265 /// <param name="Category"></param> 266 /// <param name="Key"></param> 267 /// <param name="pageIndex"></param> 268 /// <param name="pageSize"></param> 269 /// <returns></returns> 270 public List<KeyValuePair<K, V>> PageRecord<K, V>(string Category, K Key, int pageIndex, int pageSize, ref long Count) 271 { 272 273 var result = new List<KeyValuePair<K, V>>(); 274 using (IStorageEngine engine = STSdb.FromFile(dataName)) 275 { 276 var table = engine.OpenXTable<K, V>(Category).Skip(pageSize * (pageIndex - 1)).Take(pageSize); 277 Count=table.Count(); 278 foreach (var item in table) 279 result.Add(new KeyValuePair<K, V>(item.Key, item.Value)); 280 } 281 return result; 282 } 283 284 285 /// <summary> 286 /// 删除数据 287 /// </summary> 288 /// <typeparam name="K">对象</typeparam> 289 /// <param name="Category">表名</param> 290 /// <param name="Key">key</param> 291 public void Delete<K>(string Category, K Key) 292 { 293 lock (syncRoot) 294 { 295 using (IStorageEngine engine = STSdb.FromFile(dataName)) 296 { 297 var table = engine.OpenXTable<K, string>(Category); 298 if (table != null) 299 { 300 table.Delete(Key); 301 engine.Commit(); 302 } 303 } 304 } 305 } 306 307 /// <summary> 308 /// 表是否存在 309 /// </summary> 310 /// <param name="Category">表名</param> 311 /// <returns></returns> 312 public bool ExistsTable(string Category) 313 { 314 using (IStorageEngine engine = STSdb.FromFile(dataName)) 315 { 316 return engine.Exists(Category); 317 } 318 } 319 /// <summary> 320 /// Key是否存在 321 /// </summary> 322 /// <typeparam name="K"></typeparam> 323 /// <param name="Category"></param> 324 /// <param name="Key"></param> 325 /// <returns></returns> 326 public bool ExistsKey<K>(string Category, K Key) 327 { 328 using (IStorageEngine engine = STSdb.FromFile(dataName)) 329 { 330 var table = engine.OpenXTable<K, string>(Category); 331 return table.Exists(Key); 332 333 } 334 335 } 336 /// <summary> 337 /// 查看数据库中表的数量 338 /// </summary> 339 /// <returns></returns> 340 public int GetDBTableCount() 341 { 342 using (IStorageEngine engine = STSdb.FromFile(dataName)) 343 { 344 return engine.Count; 345 346 347 } 348 } 349 /// <summary> 350 /// 查看当前数据库信息 351 /// </summary> 352 /// <param name="Category"></param> 353 /// <returns></returns> 354 public IDescriptor DatabaseSchemeInfo(string Category) 355 { 356 using (IStorageEngine engine = STSdb.FromFile(dataName)) 357 { 358 IDescriptor descriptor = engine[Category]; 359 360 return descriptor; 361 362 } 363 } 364 /// <summary> 365 /// 返回数据库中表中的记录数 366 /// </summary> 367 /// <typeparam name="K"></typeparam> 368 /// <typeparam name="V"></typeparam> 369 /// <param name="TableName"></param> 370 /// <returns></returns> 371 public long GetTableCount<K>(string TableName,K Key) 372 { 373 using (IStorageEngine engine = STSdb.FromFile(dataName)) 374 { 375 376 //表记录数 377 var table = engine.OpenXTable<K, string>(TableName); 378 return table.Count(); 379 } 380 } 381 /// <summary> 382 /// 删除表 383 /// </summary> 384 public void DeleteTable(string TableName) 385 { 386 using (IStorageEngine engine = STSdb.FromFile(dataName)) 387 { 388 engine.Delete(TableName); 389 engine.Commit(); 390 } 391 } 392 393 }
public ActionResult Index() { var w = new Stopwatch(); w.Start(); string Str = ""; var engine = new STSDBHelper(System.Web.HttpContext.Current.Server.MapPath("~/DB/OrderDB")); List<Model.Orders> list = new List<Model.Orders>();//可以自建DTO //修改直接覆盖某一个key就OK了 for (int i = 0; i < 1000000; i++) { var order = new Model.Orders { event_id = 24, AddressID = 1, Uid = 1, order_id = 100000, order_sn =DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), order_status = 0, shipping_status = 1, pay_status = 1, order_time = DateTime.Now, shipping_amount = 100, pay_amount = 100, weixin = "1233122323_" + i }; list.Add(order); } //插入一个集合 engine.Add("Orders", "qwert", list); w.Stop(); Str = Str + "插入所用时间:" + w.Elapsed; w = new Stopwatch(); w.Start(); //读取数据 var o = engine.Get<string, List<Model.Orders>>("Orders", "qwert"); foreach (var item in o) { // Console.WriteLine(item.weixin); } w.Stop(); Str = Str + "<br/>读取并循环所用时间:" + w.Elapsed; Str = Str + "<br/>---------------------分页数据-----------------------------"; long count=0; int pageIndex = 0; int pageSize = 10; //分页获取某个表中某个key的值 var page = o.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList(); foreach (var item in page) { Str = Str + "<br/>" + item.weixin + "--订单编号:" + item.order_sn; } Str = Str + "<br/>---------------------查看当前数据库中的表信息-----------------------------"; var info = engine.DatabaseSchemeInfo("Orders"); Str = Str + "<br/>AccessTime:" + info.AccessTime; Str = Str + "<br/>CreateTime:" + info.CreateTime; Str = Str + "<br/>ID:" + info.ID; Str = Str + "<br/>ModifiedTime:" + info.ModifiedTime; Str = Str + "<br/>Name:" + info.Name; Str = Str + "<br/>StructureType:" + info.StructureType; Str = Str + "<br/>--------------------------------------------------"; string c=engine.Find("Orders", "qwert"); Str = Str + "<br/>删除前数量:" + engine.GetTableCount("Orders", "qwert"); Str = Str + "<br/>表是否存在:" + (engine.ExistsTable("Orders") ? "存在" : "不存在"); Str = Str + "<br/>Key是否存在:" + (engine.ExistsKey("Orders", "qwert") ? "存在" : "不存在"); //删除一个key engine.Delete("Orders", "qwert"); engine.DeleteTable("Orders");//删除一个表 Str = Str + "<br/>删除Key之后数量:" + engine.GetTableCount("Orders", "qwert"); Str = Str + "<br/>删除之后表是否存在:" + (engine.ExistsTable("Orders") ? "存在" : "不存在"); Str = Str + "<br/>删除之后Key是否存在:" + (engine.ExistsKey("Orders", "1234") ? "存在" : "不存在"); return Content(Str); }
标签:
原文地址:http://www.cnblogs.com/zuihaola/p/stsdb.html