public static List <T> TrasformInList <T>(SqlDataReader reader, Trasform <T> trasform) { List <T> output = new List <T>(); do { T tmp = trasform(reader); if (tmp == null) { break; } output.Add(tmp); } while (true); return(output); }
/// <summary> /// Esegue le Query e salva i dati in un SqlDataReader /// </summary> /// <typeparam name="T">Rappresenta il dato da ritornare</typeparam> /// <param name="trasform">Il delegato che si occupa di trasformare i Dati che ha ricevuto della Query</param> /// <param name="sql">La Query da esequire</param> /// <param name="DBName">Inserire il nome DateBase, Default "master"</param> /// <param name="ServerName">Inserire il nome del Server, Default "(localdb)\MSSQLLocalDB"</param> /// <returns>"T"</returns> public static T ExecQReader <T>(Trasform <T> trasform, string sql, string DBName = "master", string ServerName = @"(localdb)\MSSQLLocalDB") { SqlConnection connection = new SqlConnection(GetConnectionString(DBName, ServerName)); try { connection.Open(); SqlCommand command = new SqlCommand(sql, connection); SqlDataReader data = command.ExecuteReader(); T ris = trasform(data); data.Close(); command.Dispose(); return(ris); } catch (Exception e) { throw e; } finally { connection.Dispose(); } }
public static T ExecQProcedureReader <T>(string procedureName, Trasform <T> trasform, SqlParameter[] sqlParameters = null, string DBName = "master", string ServerName = @"(localdb)\MSSQLLocalDB") { SqlConnection connection = new SqlConnection(GetConnectionString(DBName, ServerName)); try { connection.Open(); SqlCommand command = new SqlCommand(procedureName, connection); command.CommandType = CommandType.StoredProcedure; if (sqlParameters != null) { command.Parameters.AddRange(sqlParameters); } SqlDataReader reader = command.ExecuteReader(); T ris = trasform(reader); reader.Close(); command.Dispose(); return(ris); } catch (Exception e) { throw e; } finally { connection.Dispose(); } }