CNAS取数仪器端升级
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

4 miesięcy temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using CnasSynchrousModel;
  7. namespace CnasSynchronusClient
  8. {
  9. public class CnasDataCheck
  10. {
  11. public DataTable DtReadyInsert { get; set; }
  12. public DataBaseInfo TargetDataBase { get; set; }
  13. public string StrTableName { get; set; }
  14. public List<IllegalCnasData> LstIllegalMsg { get; set; }
  15. public void CheckData()
  16. {
  17. //1.获取数据库的字段类型和长度
  18. DataTable dtTableType = CnasDataOperationFact.CnasDataOperation().GetCNASTableTypeLenth(StrTableName, TargetDataBase);
  19. LstIllegalMsg = new List<IllegalCnasData>();
  20. //2.根据字段类型和长度获取检查条件
  21. if (dtTableType != null && dtTableType.Rows.Count > 0)
  22. {
  23. foreach (DataColumn dc in DtReadyInsert.Columns)
  24. {
  25. string strColumnName = dc.ColumnName;
  26. bool bIfNullable = true;
  27. DataRow[] rowsType = dtTableType.Select("ColumnName='"+strColumnName+"'");
  28. if (rowsType.Length == 1)
  29. {
  30. string strColumnType = rowsType[0]["DataType"].ToString();
  31. switch (strColumnType.ToLower())
  32. {
  33. case "varchar":
  34. int Maxlenth = Convert.ToInt32(rowsType[0]["CharMaxLenth"]);
  35. int MaxByteLenth= Convert.ToInt32(rowsType[0]["CharOcterLenth"]);
  36. bIfNullable = rowsType[0]["IsNullable"].ToString().ToLower()=="yes"?true:false;
  37. foreach (DataRow dr in DtReadyInsert.Rows)
  38. {
  39. if (!CheckVarcharData(dr[strColumnName].ToString(), bIfNullable, Maxlenth, MaxByteLenth))
  40. LstIllegalMsg.Add(new IllegalCnasData() { ColumnName = strColumnName, ColumnValue = dr[strColumnName].ToString() });
  41. }
  42. break;
  43. case "bigint":
  44. //bIfNullable = Convert.ToBoolean(rowsType[0]["IsNullable"]);
  45. //int NumericPrecision = Convert.ToInt32(rowsType[0]["NumericPrecision"]);
  46. //int NumericScale = Convert.ToInt32(rowsType[0]["NumericScale"]);
  47. break;
  48. case "decimal":
  49. bIfNullable = rowsType[0]["IsNullable"].ToString().ToLower() == "yes" ? true : false;
  50. int NumericPrecision = Convert.ToInt32(rowsType[0]["NumericPrecision"]);
  51. int NumericScale = Convert.ToInt32(rowsType[0]["NumericScale"]);
  52. foreach (DataRow dr in DtReadyInsert.Rows)
  53. {
  54. if (!CheckDecimalData(dr[strColumnName].ToString(), bIfNullable, NumericPrecision, NumericScale))
  55. LstIllegalMsg.Add(new IllegalCnasData() { ColumnName = strColumnName, ColumnValue = dr[strColumnName].ToString() });
  56. }
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. }
  63. private bool CheckVarcharData(string strValue, bool IfNullable, int MaxLenth,int MaxByteLenth)
  64. {
  65. if (strValue == null || strValue == "")
  66. if (!IfNullable) //如果数据库不允许为空,此处需要检查是否准备插入的数据是空值
  67. return false;
  68. else
  69. return true;
  70. if (Encoding.Default.GetByteCount(strValue) > MaxByteLenth)
  71. return false;
  72. return true;
  73. }
  74. private bool CheckDecimalData(string strValue, bool IfNullable, int NumericPrecision, int NumericScale)
  75. {
  76. if (strValue == null || strValue == "")
  77. if (!IfNullable) //如果数据库不允许为空,此处需要检查是否准备插入的数据是空值
  78. return false;
  79. else
  80. return true;
  81. double result=0;
  82. if (!double.TryParse(strValue, out result))
  83. return false;
  84. long ValueInt = (long)(result);
  85. int ValueDigit = (int)(result % 1);
  86. if (ValueInt.ToString().Length > NumericPrecision- NumericScale || ValueDigit.ToString().Length > NumericScale)
  87. return false;
  88. return true;
  89. }
  90. }
  91. public class IllegalCnasData
  92. {
  93. public string ColumnName { get; set; }
  94. public object ColumnValue { get; set; }
  95. }
  96. public class DataFieldAndLenth
  97. {
  98. public string FieldName { get; set; }
  99. public Type FieldType { get; set; }
  100. public int FieldLenth { get; set; }
  101. public int FieldDigits{ get; set; }
  102. }
  103. }