/// <summary> /// 把当前的值赋给指定的目标 /// </summary> /// <param name="target"></param> /// <param name="copyNullValue">是否拷贝null值</param> /// <param name="copyPkValue">是否拷贝主键值</param> public void CopyValueTo(DataItem target, bool copyNullValue, bool copyPkValue = true) { var targetType = target.GetType(); string targetKeyName = null; if (!copyPkValue) { targetKeyName = SchemaManager.GetSchemaTable(targetType).KeyColumn.PropertyName; } var properties = this.TableType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(m => m.GetCustomAttribute <System.ComponentModel.DataAnnotations.Schema.ColumnAttribute>() != null); foreach (var pro in properties) { try { if (copyPkValue == false && targetKeyName == pro.Name) { continue; } var myval = pro.GetValue(this); if (!copyNullValue && myval == null) { continue; } if (targetType == this.TableType) { pro.SetValue(target, myval); } else { var targetPro = targetType.GetProperty(pro.Name); if (targetPro != null) { var att = targetPro.GetCustomAttribute <DatabaseGeneratedAttribute>(); if (att != null && att.DatabaseGeneratedOption != DatabaseGeneratedOption.None) { continue; } target.SetValue(pro.Name, myval); } } } catch { } } }
public virtual void Insert(DataItem dataitem) { bool needToClose = false; if (this.Connection.State != System.Data.ConnectionState.Open) { needToClose = true; this.Connection.Open(); } var tableSchema = SchemaManager.GetSchemaTable(dataitem.GetType()); string pkid = null; if (tableSchema != null) { pkid = tableSchema.KeyColumn.PropertyName; } var fieldValues = dataitem.GetFieldValues(true); if (fieldValues.Count == 0) { return; } if (tableSchema != null && tableSchema.AutoSetPropertyNameOnInsert != null) { if (dataitem.ChangedProperties.Any(m => m.Key == tableSchema.AutoSetPropertyNameOnInsert) == false) { var val = tableSchema.AutoSetPropertyValueOnInsert; if (!SupportEnum && val != null && val.GetType().IsEnum) { val = Convert.ToInt32(val);//PostgreSql不支持枚举 } var fieldname = tableSchema.AutoSetPropertyNameOnInsert.ToLower(); var fv = fieldValues.FirstOrDefault(m => m.FieldName == fieldname); if (fv != null) { fv.Value = val; } else { fieldValues.Add(new FieldValue() { FieldName = tableSchema.AutoSetPropertyNameOnInsert.ToLower(), Value = val }); } } } StringBuilder str_fields = new StringBuilder(); StringBuilder str_values = new StringBuilder(); try { using (var command = CreateCommand(null)) { int parameterIndex = 1; foreach (var field in fieldValues) { if (str_fields.Length > 0) { str_fields.Append(','); } str_fields.Append(FormatObjectName(field.FieldName)); var parameter = command.CreateParameter(); parameter.ParameterName = "@p" + (parameterIndex++); parameter.Value = field.Value; if (!SupportEnum && field.Value != null && field.Value.GetType().IsEnum) { parameter.Value = Convert.ToInt32(field.Value); } command.Parameters.Add(parameter); if (str_values.Length > 0) { str_values.Append(','); } str_values.Append(parameter.ParameterName); } string sql; if (GetInsertIDValueSqlStringInOneSql()) { sql = string.Format("insert into {0} ({1}) values ({2}) {3}", FormatObjectName(dataitem.TableName), str_fields, str_values, this.GetInsertIDValueSqlString(pkid)); command.CommandText = sql; object id = command.ExecuteScalar(); command.Parameters.Clear(); if (id != null && id != DBNull.Value && !string.IsNullOrEmpty(pkid)) { dataitem.SetValue(pkid, id); } } else { sql = string.Format("insert into {0} ({1}) values ({2})", FormatObjectName(dataitem.TableName), str_fields, str_values); command.CommandText = sql; command.ExecuteNonQuery(); command.Parameters.Clear(); sql = this.GetInsertIDValueSqlString(pkid); if (sql != null) { command.CommandText = sql; object id = command.ExecuteScalar(); if (id != null && id != DBNull.Value && !string.IsNullOrEmpty(pkid)) { dataitem.SetValue(pkid, id); } } } } dataitem.ChangedProperties.Clear(); } catch (Exception ex) { this.ThrowSqlException(dataitem.TableType, ex); } finally { if (needToClose) { this.Connection.Close(); } } }