EventDBWriter.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 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. try
  39. {
  40. for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  41. {
  42. EventItem ev = new EventItem();
  43. int id = 0;
  44. if (int.TryParse(ds.Tables[0].Rows[i]["event_id"].ToString(), out id))
  45. ev.Id = id;
  46. EventType type = EventType.EventUI_Notify;
  47. if (Enum.TryParse<EventType>(ds.Tables[0].Rows[i]["type"].ToString(), out type))
  48. ev.Type = type;
  49. ev.Source = ds.Tables[0].Rows[i]["source"].ToString();
  50. ev.EventEnum = ds.Tables[0].Rows[i]["event_enum"].ToString();
  51. ev.Description = ds.Tables[0].Rows[i]["description"].ToString();
  52. EventLevel level = EventLevel.Information;
  53. if (Enum.TryParse<EventLevel>(ds.Tables[0].Rows[i]["level"].ToString(), out level))
  54. ev.Level = level;
  55. if (!ds.Tables[0].Rows[i]["occur_time"].Equals(DBNull.Value))
  56. ev.OccuringTime = ((DateTime)ds.Tables[0].Rows[i]["occur_time"]) ;
  57. //DateTime dt = (DateTime)ds.Tables[0].Rows[i]["occur_time"];
  58. //if (DateTime.TryParse(ds.Tables[0].Rows[i]["occur_time"].ToString(), out dt ))
  59. //ev. = dt;
  60. result.Add(ev);
  61. }
  62. }
  63. catch (Exception ex)
  64. {
  65. LOG.WriteExeption(ex);
  66. }
  67. return result;
  68. }
  69. }
  70. }