Torna al Thread

class TxtFilesUtilities { /// <summary> /// scrivo su file /// </summary> /// <param name="source">dataset sorgente</param> /// <param name="path">percorso del file di destinazione</param> public static void WriteToFile(DataSet source, string path) { StringBuilder sb = new StringBuilder(); string s = string.Empty; // se esistono tables (datatables) allora ciclo le righe e le scrivo if (source.Tables.Count > 0) { DataTable query = source.Tables[0]; // se esistono righe le ciclo per scriverle if (query.Rows.Count > 0) { foreach (DataRow dr in query.Rows) { sb.AppendFormat("NOME: {0} {1}, CODICE FISCALE: {2}, ETA': {3}", dr["Nome"], dr["Cognome"], dr["CodiceFiscale"], dr["Eta"]); sb.AppendLine(); } s = sb.ToString(); } } if (!string.IsNullOrEmpty(s)) { // scrivo su file e rilascio la risorsa (va sempre a sovrascrivere, se metti true nel secondo parametro, appendi in coda) using (StreamWriter sw = new StreamWriter(path, false)) { sw.Write(s); sw.Close(); } } }
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5