private SqlCommand InsertPosition(string tabelName, MSG message) { SqlCommand sqlCommand = new SqlCommand(); StringBuilder tableColumnsNames = new StringBuilder(); StringBuilder commandParametersNames = new StringBuilder(); try { tableColumnsNames.Append("HeaderID"); commandParametersNames.Append("@posHeaderID"); sqlCommand.Parameters.AddWithValue("@posHeaderID", message.HeaderId); PropertyInfo[] posProperties = message.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(MSGPosAttribute))).ToArray(); for (int i = 0; i < posProperties.Length; i++) { if (posProperties[i].GetValue(message) != null) { tableColumnsNames.Append(","); tableColumnsNames.Append(posProperties[i].Name); commandParametersNames.Append(","); string posItem = string.Format("@pos{0}", posProperties[i].Name); commandParametersNames.Append(posItem); sqlCommand.Parameters.AddWithValue(posItem, posProperties[i].GetValue(message)); } } string commandText = string.Format(@"INSERT INTO {0} ({1}) VALUES({2})", tabelName, tableColumnsNames.ToString(), commandParametersNames.ToString()); _loger.Log(string.Format("Insert position command = {0}", commandText)); sqlCommand.CommandText = commandText; } catch (Exception ex) { _loger.Log(ex); } return(sqlCommand); }