/// <summary> /// Create sql data record collection for bulk insert. /// It creates meta data from insert procedure mapping customattribute /// </summary> /// <typeparam name="T"> /// Any type which implements Framework.Common.Models.IModel interface. /// </typeparam> /// <param name="modelCollection"> /// A collection of model items to be bulk inserted. /// </param> /// <returns> /// A collection of SqlDataRecord object for bulk insert. /// </returns> public List <SqlDataRecord> CreateSqlDataRecordCollection <T>(List <T> modelCollection) where T : IModel { List <SqlDataRecord> recordCollection = null; ModelParameterMap[] modelInsertParameters = new ModelParameterMap[this.map.Parameters.Count]; SqlMetaData[] metaDataCollection = new SqlMetaData[this.map.Parameters.Count]; foreach (ArrayList parameterMap in this.map.Parameters.Values) { ModelParameterMap[] parameters = (ModelParameterMap[])parameterMap.ToArray(typeof(ModelParameterMap)); if (parameters != null && parameters.Length > 0) { for (int i = 0; i < parameters.Length; i++) { ModelParameterMap mp = parameters[i]; if (mp.ParameterAction == DBProcedureType.INSERT) { // create meta data for insert parameter. SqlMetaData metaData = this.CreateSQLMetaData(mp.ParameterName, mp.DatabaseType); metaDataCollection[mp.ParameterIndex - 1] = metaData; modelInsertParameters[mp.ParameterIndex - 1] = mp; } } } } List <SqlMetaData> metaDataList = new List <SqlMetaData>(); for (int counter = 0; counter < metaDataCollection.Length; counter++) { if (metaDataCollection[counter] != null) { metaDataList.Add(metaDataCollection[counter]); } } // create one sql data record object for each model in the collection // and add that object to list recordCollection = new List <SqlDataRecord>(); foreach (IModel model in modelCollection) { SqlDataRecord dataRecord = new SqlDataRecord(metaDataList.ToArray()); // assign value for each property in the data record. for (int index = 0; index < modelInsertParameters.Length; index++) { if (modelInsertParameters[index] != null) { ModelParameterMap mp = modelInsertParameters[index]; // this.SetDataRecordValue(dataRecord, mp.DatabaseType, mp.Property.GetValue(model, null), index); dataRecord.SetValue(index, mp.Property.GetValue(model, null)); } } recordCollection.Add(dataRecord); } return(recordCollection); }
/// <summary> /// Creates SQL command object with parameters for update operation /// </summary> /// <param name="model"> /// Imodel object /// </param> /// <returns> /// An SqlCommand object with command text and all parameters. /// </returns> public SqlCommand GetUpdateCommand(IModel model) { SqlCommand asaCommand = new SqlCommand(); SortedList parameterSortedList = new SortedList(); string updateProcedureName = ((StoredProcedureMappingAttribute)this.map.Procedures[DBProcedureType.UPDATE]).ProcedureName; if (updateProcedureName != null) { asaCommand.CommandText = updateProcedureName; asaCommand.CommandType = CommandType.StoredProcedure; foreach (ArrayList al in this.map.Parameters.Values) { ModelParameterMap[] parameters = (ModelParameterMap[])al.ToArray( typeof(ModelParameterMap)); if (parameters != null && parameters.Length > 0) { for (int i = 0; i < parameters.Length; i++) { ModelParameterMap mp = (ModelParameterMap)parameters[i]; if (mp.ParameterAction == DBProcedureType.UPDATE) { SqlParameter p = new SqlParameter(); string parameterName = string.Concat("@", mp.ParameterName); p.ParameterName = parameterName; p.DbType = mp.DatabaseType; p.SourceColumn = parameterName; object parameterValue = mp.Property.GetValue(model, null); if (parameterValue == null) { p.Value = DBNull.Value; } else { p.Value = parameterValue; } int index; if (mp.ParameterIndex == 0) { index = parameterSortedList.Count + 1; } else { index = mp.ParameterIndex; } parameterSortedList.Add(index, p); } } } } } if (parameterSortedList.Count > 0) { foreach (DictionaryEntry parameter in parameterSortedList) { int index = (int)parameter.Key; SqlParameter currentParam = (SqlParameter)parameter.Value; asaCommand.Parameters.Add(currentParam); } } return(asaCommand); }