public static CSVData Parse(string[] s, bool hasHeaders) { List <CSVItem> all = new List <CSVItem>(); int line = 0; string[] headers = null; foreach (string i in s) { CSVItem item = ParseArrayItem(i); if (hasHeaders && line == 0) { headers = item.ToArray(); } else { all.Add(item); } line = line + 1; } CSVData csv = new CSVData(); csv.Headings = headers; csv.Items = all.ToArray(); return(csv); }
public static CSVData Parse(System.IO.TextReader reader, bool hasHeaders) { int line = 0; string[] headers = null; List <CSVItem> all = new List <CSVItem>(); string linedata = reader.ReadLine(); while (null != linedata) { CSVItem item = ParseItem(linedata); if (null != item) { if (hasHeaders && line == 0) { headers = item.ToArray(); } else { all.Add(item); } } line++; linedata = reader.ReadLine(); } CSVData csv = new CSVData(); csv.Headings = headers; csv.Items = all.ToArray(); return(csv); }
public List <DataError> ExecuteNonQuery(string[] statement, string conn) { base.InitDbs(conn); List <DataError> ers = new List <DataError>(); prepareInsertStatement(statement, DataSchema.Table); using (DbTransaction trans = base.Database.BeginTransaction()) { int sum = 0; // base.Database.CreateConnection().Open(); for (int i = 0; i < all.Length; i++) { item = all[i]; try { sum += base.Database.ExecuteNonQuery(trans, insert); ers.Add(new DataError() { Type = DataError.ErrorType.NOERROR, Message = "Import done" }); } catch (Exception ex) { ers.Add(new DataError() { Type = DataError.ErrorType.ERROR, Message = "Error:" + ex.Message + " and values (" + string.Join(",", item.ToArray()) + ")" }); } finally { this.AfterProcessed(null, null); } } trans.Commit(); } return(ers); }
public List <DataError> UpdateScript(string path, string conn) { base.InitDbs(conn); DataError ers = new DataError(); CreateTempTable(); string contentcsv = System.IO.File.ReadAllText(path); prepareInsertStatement(contentcsv, tempTableName); List <DataError> erss = new List <DataError>(); using (DbTransaction trans = base.Database.BeginTransaction()) { int sum = 0; // base.Database.CreateConnection().Open(); for (int i = 0; i < all.Length; i++) { item = all[i]; try { sum += base.Database.ExecuteNonQuery(trans, insert); erss.Add(new DataError() { Type = DataError.ErrorType.NOERROR, Message = "Import done" }); } catch (Exception ex) { erss.Add(new DataError() { Type = DataError.ErrorType.ERROR, Message = "Error:" + ex.Message + " and values (" + string.Join(",", item.ToArray()) + ")" }); } finally { this.AfterProcessed(null, null); } } trans.Commit(); } try { string s = PrepareUpdateStatement(); int count = base.Database.ExecuteNonQuery(s); if (count == 0) { foreach (DataError dr in erss) { dr.Type = DataError.ErrorType.NOERROR; dr.Message = "Import done"; } } base.Database.ExecuteNonQuery("Drop table " + tempTableName + ";"); } catch (Exception ex) { foreach (DataError dr in erss) { dr.Type = DataError.ErrorType.ERROR; dr.Message = ex.Message; } } finally { } return(erss); }