SqlManager.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. namespace WebAPIBase.NetCore.BusinessCore
  8. {
  9. public sealed class SqlManager
  10. {
  11. private SqlManager()
  12. {
  13. }
  14. public static SqlStruct GetSqlStruct(string className, string sqlStringName)
  15. {
  16. return GetSqlStruct(className, className, sqlStringName);
  17. }
  18. public static SqlStruct GetSqlStruct(string className, string tableName, string sqlStringName)
  19. {
  20. try
  21. {
  22. XmlDocument xmlDoc = XmlDefineFileManage.GetXmlDoc(className);
  23. SqlStruct sqlStruct = new SqlStruct();
  24. XmlNode documentElement = xmlDoc.DocumentElement;
  25. XmlNode xmlNode = documentElement.SelectSingleNode("Table[Name='" + tableName + "']");
  26. if (xmlNode == null)
  27. {
  28. throw new ApplicationException("没有找到表: " + tableName);
  29. }
  30. XmlNode xmlNode2 = xmlNode.SelectSingleNode("Sql[Name='" + sqlStringName + "']");
  31. if (xmlNode2 == null)
  32. {
  33. throw new ApplicationException("没有找到定义的字符串: " + tableName + "---" + sqlStringName);
  34. }
  35. sqlStruct.SqlString = xmlNode2.SelectSingleNode("String").InnerText;
  36. sqlStruct.CommandType = xmlNode2.SelectSingleNode("CommandType").InnerText;
  37. XmlNodeList xmlNodeList = xmlNode2.SelectNodes("Param");
  38. int count = xmlNodeList.Count;
  39. sqlStruct.ParamsList = new string[count];
  40. sqlStruct.ColumnsList = new string[count];
  41. sqlStruct.SqlDbTypeList = new string[count];
  42. if (count != 0)
  43. {
  44. for (int i = 0; i < count; i++)
  45. {
  46. sqlStruct.ParamsList[i] = xmlNodeList[i].SelectSingleNode("Name").InnerText;
  47. sqlStruct.ColumnsList[i] = xmlNodeList[i].SelectSingleNode("Column").InnerText;
  48. sqlStruct.SqlDbTypeList[i] = xmlNodeList[i].SelectSingleNode("SqlDbType").InnerText;
  49. }
  50. }
  51. XmlNodeList xmlNodeList2 = xmlNode2.SelectNodes("Order");
  52. int count2 = xmlNodeList2.Count;
  53. sqlStruct.OrderNameList = new string[count2];
  54. sqlStruct.OrderSortList = new string[count2];
  55. for (int j = 0; j < count2; j++)
  56. {
  57. sqlStruct.OrderNameList[j] = xmlNodeList2[j].SelectSingleNode("Name").InnerText;
  58. sqlStruct.OrderSortList[j] = xmlNodeList2[j].SelectSingleNode("Sort").InnerText;
  59. }
  60. return sqlStruct;
  61. }
  62. catch (Exception ex)
  63. {
  64. throw ex;
  65. }
  66. }
  67. }
  68. }