EventDBWriter.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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["event_id"] = typeof(int);
  17. columns["event_enum"] = typeof(string);
  18. columns["type"] = typeof(string);
  19. columns["source"] = typeof(string);
  20. columns["description"] = typeof(string);
  21. columns["level"] = typeof(string);
  22. columns["occur_time"] = typeof(DateTime);
  23. DB.CreateTableIfNotExisted("event_data", columns, true, "gid");
  24. }
  25. public void WriteEvent(EventItem ev)
  26. {
  27. string executeInsert = string.Format(
  28. @"Insert into ""event_data""(""event_id"",""event_enum"",""type"",""source"",""description"",""level"",""occur_time"") 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 int GetCount(string cmdText)
  33. {
  34. return DB.GetCount(cmdText);
  35. }
  36. public List<EventItem> QueryDBEvent(string sql)
  37. {
  38. List<EventItem> result = new List<EventItem>();
  39. DataSet ds = DB.ExecuteDataset(sql);
  40. if (ds == null)
  41. return result;
  42. try
  43. {
  44. for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  45. {
  46. EventItem ev = new EventItem();
  47. int id = 0;
  48. if (int.TryParse(ds.Tables[0].Rows[i]["event_id"].ToString(), out id))
  49. ev.Id = id;
  50. EventType type = EventType.EventUI_Notify;
  51. if (Enum.TryParse<EventType>(ds.Tables[0].Rows[i]["type"].ToString(), out type))
  52. ev.Type = type;
  53. ev.Source = ds.Tables[0].Rows[i]["source"].ToString();
  54. ev.EventEnum = ds.Tables[0].Rows[i]["event_enum"].ToString();
  55. ev.Description = ds.Tables[0].Rows[i]["description"].ToString();
  56. EventLevel level = EventLevel.Information;
  57. if (Enum.TryParse<EventLevel>(ds.Tables[0].Rows[i]["level"].ToString(), out level))
  58. ev.Level = level;
  59. if (!ds.Tables[0].Rows[i]["occur_time"].Equals(DBNull.Value))
  60. ev.OccuringTime = ((DateTime)ds.Tables[0].Rows[i]["occur_time"]) ;
  61. //DateTime dt = (DateTime)ds.Tables[0].Rows[i]["occur_time"];
  62. //if (DateTime.TryParse(ds.Tables[0].Rows[i]["occur_time"].ToString(), out dt ))
  63. //ev. = dt;
  64. result.Add(ev);
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. LOG.Write(ex);
  70. }
  71. return result;
  72. }
  73. }
  74. }