EventDBWriter.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.RT.DBCore;
  6. using System.Data;
  7. using Aitex.Core.RT.Log;
  8. namespace Aitex.Core.RT.Event
  9. {
  10. class EventDBWriter
  11. {
  12. EventDB _db = new EventDB();
  13. public void Initialize()
  14. {
  15. Dictionary<string, Type> columns = new Dictionary<string,Type>();
  16. columns["ID"] = typeof(int);
  17. columns["EventEnum"] = typeof(string);
  18. columns["Type"] = typeof(string);
  19. columns["Source"] = typeof(string);
  20. columns["Description"] = typeof(string);
  21. columns["Level"] = typeof(string);
  22. columns["OccurTime"] = typeof(DateTime);
  23. DB.CreateTableIfNotExisted("EventManager", columns, true, "PID");
  24. }
  25. public void WriteEvent(EventItem ev)
  26. {
  27. string executeInsert = string.Format(
  28. @"Insert into ""EventManager""(""ID"",""EventEnum"",""Type"",""Source"",""Description"",""Level"",""OccurTime"") values({0},'{1}','{2}','{3}','{4}','{5}','{6}')",
  29. ev.Id, ev.EventEnum, ev.Type.ToString(), ev.Source, ev.Description.Replace("'", "''"), ev.Level.ToString(), ev.OccuringTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
  30. DB.Insert(executeInsert);
  31. }
  32. public List<EventItem> QueryDBEvent(string sql)
  33. {
  34. List<EventItem> result = new List<EventItem>();
  35. DataSet ds = DB.ExecuteDataset(sql);
  36. if (ds == null)
  37. return result;
  38. for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  39. {
  40. EventItem ev = new EventItem();
  41. int id = 0;
  42. if (int.TryParse(ds.Tables[0].Rows[i]["ID"].ToString(), out id ))
  43. ev.Id = id;
  44. EventType type = EventType.EventUI_Notify;
  45. if (Enum.TryParse<EventType>(ds.Tables[0].Rows[i]["Type"].ToString(), out type))
  46. ev.Type = type;
  47. ev.Source = ds.Tables[0].Rows[i]["Source"].ToString();
  48. ev.EventEnum = ds.Tables[0].Rows[i]["EventEnum"].ToString();
  49. ev.Description = ds.Tables[0].Rows[i]["Description"].ToString();
  50. EventLevel level = EventLevel.Information;
  51. if (Enum.TryParse<EventLevel>(ds.Tables[0].Rows[i]["Level"].ToString(), out level))
  52. ev.Level = level;
  53. var row = ds.Tables[0].Rows[i]["OccurTime"];
  54. if (row != null && !row.Equals(DBNull.Value))
  55. ev.OccuringTime = ((DateTime)row);
  56. result.Add(ev);
  57. }
  58. return result;
  59. }
  60. }
  61. }