public void Insert <T>(params T[] data) { if (data != null && data.Length > 0) { var t = typeof(T); var attr = MappedObjectAttribute.GetAttribute <MappedClassAttribute>(t); if (attr == null) { throw new Exception(string.Format("Type {0}.{1} could not be automatically inserted.", t.Namespace, t.Name)); } attr.InferProperties(t); var tableData = MakeDataTable(attr.Name, t, data); if (this.Connection.State == ConnectionState.Closed) { this.Connection.Open(); } var bulkCopy = new SqlBulkCopy(this.Connection); foreach (var column in tableData.Columns.Cast <DataColumn>()) { bulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName); } bulkCopy.DestinationTableName = attr.Name; bulkCopy.WriteToServer(tableData); } }
private string MakeBasicSqlTypeString(Type t) { if (t.IsGenericType) { var subTypes = t.GetGenericArguments(); if (subTypes.Length > 1) { throw new Exception("Type is too complex!"); } t = subTypes.First(); } var temp = MappedObjectAttribute.GetAttribute <MappedTypeAttribute>(t); if (temp != null) { return(MakeSqlTypeString(temp)); } else if (reverseTypeMapping.ContainsKey(t)) { return(reverseTypeMapping[t]); } else { return(null); } }
private void MaybeSynchronizeUDTT(Type t) { var attr = MappedObjectAttribute.GetAttribute <SqlServerMappedClassAttribute>(t); if (attr != null && attr.IsUploadable) { SynchronizeComplexUDTT(t, attr); } }
static bool IsUDTT(Type t) { var isUDTT = false; if (t.IsArray) { t = t.GetElementType(); isUDTT = t != typeof(byte) && IsTypePrimitive(t); } var attr = MappedObjectAttribute.GetAttribute <SqlServerMappedClassAttribute>(t); return((attr != null && attr.IsUploadable) || isUDTT); }
public static string MakeUDTTName(Type t) { if (t.IsArray) { t = t.GetElementType(); } var attr = MappedObjectAttribute.GetAttribute <SqlServerMappedClassAttribute>(t); if (attr == null) { attr = new SqlServerMappedClassAttribute(); attr.Name = t.Name; } attr.InferProperties(t); return(attr.Name + "UDTT"); }
public void Insert(Type t, System.Collections.IEnumerable data, bool insertDefaultColumns) { // temporarily close the connection to the file bool wasOpen = false; if (this.Connection.State != ConnectionState.Closed) { wasOpen = true; this.Connection.Close(); } // get the properties of the type we're inserting var attr = MappedObjectAttribute.GetAttribute <MappedClassAttribute>(t); if (attr == null) { throw new Exception(string.Format("Type {0}.{1} could not be automatically inserted.", t.Namespace, t.Name)); } attr.InferProperties(t); var props = attr.Properties.Where(p => p.Include).ToArray(); // setup DAO var engine = new DAO.DBEngine(); var db = engine.OpenDatabase(this.Connection.DataSource); var recordSet = db.OpenRecordset(attr.Name); var fields = new DAO.Field[props.Length]; var fieldNames = recordSet.Fields .OfType <DAO.Field>() .Select(f => f.Name); for (int i = 0; i < fields.Length; ++i) { fields[i] = recordSet.Fields[props[i].Name]; } // copy foreach (var obj in data) { recordSet.AddNew(); for (int j = 0; j < fields.Length; ++j) { if (!props[j].IsIdentity || insertDefaultColumns) { var value = props[j].GetValue(obj); if (props[j].SystemType == typeof(Guid)) { value = string.Format("{{{0}}}", value); } fields[j].Value = value; } } recordSet.Update(); } // clean up recordSet.Clone(); db.Close(); // reopen the connection, if the user is expecting it. if (wasOpen) { this.Connection.Open(); } }