CNAS取数仪器端升级
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

119 lines
5.1KB

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