public void Write(DbConnect DbConnect) { string FieldString = ""; string ParameterString = ""; foreach (Protocol.StructValue CurrentValue in ChosenBlock.ValueList) { // сверяем значение флага if (!CurrentValue.CheckFlag(Data, Size)) { continue; } if (CurrentValue.Field.Length == 0) { // неизвестное поле throw new Exception("Unknown field: " + CurrentValue.Name); } else if (CurrentValue.Parameter.Length > 0) { // поле из префикса if (!DbConnect.CheckParameter(CurrentValue.Parameter)) { throw new Exception("Unknown parameter: " + CurrentValue.Name); } FieldString += (FieldString.Length > 0 ? "," : "") + "`" + CurrentValue.Field + "`"; ParameterString += (ParameterString.Length > 0 ? "," : "") + "@" + CurrentValue.Parameter; } else if (CurrentValue.Type.Length > 0 && CurrentValue.Size > 0) { // поле из блока, распознаем тип и длину if (CurrentValue.Offset + CurrentValue.Size >= Size) { throw new Exception("Value is out of block limits: " + CurrentValue.Name); } if (CurrentValue.Type == "uint" && CurrentValue.Size == 1) { DbConnect.AddParameter("_" + CurrentValue.Field, Data[CurrentValue.Offset]); } else if (CurrentValue.Type == "uint" && CurrentValue.Size == 2) { DbConnect.AddParameter("_" + CurrentValue.Field, System.BitConverter.ToUInt16(Data, CurrentValue.Offset)); } else if (CurrentValue.Type == "uint" && CurrentValue.Size == 4) { DbConnect.AddParameter("_" + CurrentValue.Field, System.BitConverter.ToUInt32(Data, CurrentValue.Offset)); } else if (CurrentValue.Type == "uint" && CurrentValue.Size == 8) { DbConnect.AddParameter("_" + CurrentValue.Field, System.BitConverter.ToUInt64(Data, CurrentValue.Offset)); } else if (CurrentValue.Type == "float" && CurrentValue.Size == 4) { if (BitConverter.ToString(Data, CurrentValue.Offset, 4) == "00-00-C0-7F") { continue; } DbConnect.AddParameter("_" + CurrentValue.Field, System.BitConverter.ToSingle(Data, CurrentValue.Offset)); } else if (CurrentValue.Type == "datetime" && CurrentValue.Size == 4) { DbConnect.AddParameter("_" + CurrentValue.Field, Converter.ToDateTime32(Data, CurrentValue.Offset)); } else if (CurrentValue.Type == "char" && CurrentValue.Size > 0) { DbConnect.AddParameter("_" + CurrentValue.Field, System.BitConverter.ToString(Data, CurrentValue.Offset, CurrentValue.Size).Replace("-", string.Empty)); } else if (CurrentValue.Type == "byte" && CurrentValue.Size > 0) { byte[] arr = new byte[CurrentValue.Size]; Array.Copy(Data, CurrentValue.Offset, arr, 0, CurrentValue.Size); DbConnect.AddParameter("_" + CurrentValue.Field, arr); } else { throw new Exception("Unknown type: " + CurrentValue.Type + " of " + CurrentValue.Size.ToString() + "bytes"); } FieldString += (FieldString.Length > 0 ? "," : "") + "`" + CurrentValue.Field + "`"; ParameterString += (ParameterString.Length > 0 ? "," : "") + "@_" + CurrentValue.Field; } else { // неизвестное поле throw new Exception("Unknown value: " + CurrentValue.Name); } } if (FieldString.Length > 0) { DbConnect.Query = "INSERT INTO " + ChosenBlock.Table + " (" + FieldString + ") VALUES (" + ParameterString + ")"; try { DbConnect.ExecuteNonQuery(); } catch (Exception e) { throw new Exception((Properties.Settings.Default.DebugInfo ? e.Message : "Database write error")); } } }