/// <summary> /// Read and fill sql data reader into T type object /// </summary> /// <typeparam name="T">The type that sq data reader records will be converted to it</typeparam> /// <param name="sqlDataReader">The sqlDataReader object.</param> /// <returns>List of T type</returns> public static List <T> ToViewModel <T>(SqlDataReader sqlDataReader) { var list = new List <T>(); while (sqlDataReader.Read()) { var schemaTable = sqlDataReader.GetSchemaTable(); if (schemaTable != null && schemaTable.Select("ColumnName='ErrorCode'").Length > 0) { var sqlSpException = new ShonizSqlSpException(); sqlSpException.Add((int)sqlDataReader["ErrorCode"], sqlDataReader["ErrorMessage"].ToString()); while (sqlDataReader.Read()) { sqlSpException.Add((int)sqlDataReader["ErrorCode"], sqlDataReader["ErrorMessage"].ToString()); } throw sqlSpException; } var o = (T)Activator.CreateInstance(typeof(T)); foreach (var prop in o.GetType().GetProperties()) { try { prop.SetValue(o, sqlDataReader[prop.Name], null); } catch { } } list.Add(o); } return(list); }
/// <summary> /// Runs the sp. /// </summary> /// <param name="connectionNameEnum">The connection name enum.</param> /// <param name="storeProcureName">Name of the store procure.</param> /// <returns>Scalar data of sp</returns> /// <exception>SqlSPException ex /// <cref>SqlSpException ex</cref> /// </exception> public static object RunSp(ConnectionNameEnum connectionNameEnum, string storeProcureName) { object returnedObject = null; SqlCommand command = SqlGeneralMethods.MakeSqlCommand(connectionNameEnum, storeProcureName, Parameters); SqlDataReader dr = command.ExecuteReader(); if (dr.Read()) { var schemaTable = dr.GetSchemaTable(); if (schemaTable != null && schemaTable.Select("ColumnName='ErrorCode'").Length > 0) { var sqlSpException = new ShonizSqlSpException(); sqlSpException.Add((int)dr["ErrorCode"], dr["ErrorMessage"].ToString()); while (dr.Read()) { sqlSpException.Add((int)dr["ErrorCode"], dr["ErrorMessage"].ToString()); } ConnectionManager.ReleaseConnection(command.Connection); Parameters.Clear(); throw sqlSpException; } returnedObject = dr[0]; } ConnectionManager.ReleaseConnection(command.Connection); Parameters.Clear(); return(returnedObject); }