public bool ExportToCsvFile(string filename, DataTable tblSrc, bool append, EnumCharEncode encode) { _error = null; System.IO.StreamWriter sw = null; if (tblSrc == null) { _error = "Source data is null"; } else if (tblSrc.Columns.Count == 0) { _error = "Source data is empty"; } else { try { bool bWriteHeader = HasHeader; if (bWriteHeader && append) { if (System.IO.File.Exists(filename)) { System.IO.FileInfo fi = new System.IO.FileInfo(filename); if (fi.Length > 0) { bWriteHeader = false; } } } System.Text.StringBuilder sb; // sw = new System.IO.StreamWriter(filename, append, EncodeUtility.GetEncoding(encode), 2048); if (bWriteHeader && !append) { sb = new System.Text.StringBuilder(); sb.Append(DataTransferConvert.Encode(_delimiter, tblSrc.Columns[0].ColumnName)); for (int i = 1; i < tblSrc.Columns.Count; i++) { sb.Append(DataTransferConvert.Delimiter(_delimiter)); sb.Append(DataTransferConvert.Encode(_delimiter, tblSrc.Columns[i].ColumnName)); } sw.WriteLine(sb.ToString()); } for (int r = 0; r < tblSrc.Rows.Count; r++) { sb = new System.Text.StringBuilder(); object v = tblSrc.Rows[r][0]; if (v == DBNull.Value || v == null) { v = VPLUtil.GetDefaultValue(tblSrc.Columns[0].DataType); } sb.Append(DataTransferConvert.Encode(_delimiter, StringUtility.ToString(v))); for (int i = 1; i < tblSrc.Columns.Count; i++) { sb.Append(DataTransferConvert.Delimiter(_delimiter)); v = tblSrc.Rows[r][i]; if (v == DBNull.Value || v == null) { v = VPLUtil.GetDefaultValue(tblSrc.Columns[i].DataType); } sb.Append(DataTransferConvert.Encode(_delimiter, StringUtility.ToString(v))); } sw.WriteLine(sb.ToString()); } } catch (Exception er) { _error = ExceptionLimnorDatabase.FormExceptionText(er, "Error saving data to file {0}", filename); } finally { if (sw != null) { sw.Close(); } } } return(string.IsNullOrEmpty(_error)); }
public string ReceiveData(DataTable tblSrc, bool bSilent) { string error = null; System.IO.StreamWriter sw = null; if (tblSrc == null) { return("Source data is null"); } if (tblSrc.Columns.Count == 0) { return("Source data is empty"); } try { string sf = _links.GetValue(FILENAME) as string; bool bWriteHeader = HasHeader; if (bWriteHeader) { if (System.IO.File.Exists(sf)) { System.IO.FileInfo fi = new System.IO.FileInfo(sf); if (fi.Length > 0) { bWriteHeader = false; } } } System.Text.StringBuilder sb; // sw = new System.IO.StreamWriter(sf, _bAppend, EncodeUtility.GetEncoding(EncodingType), BufferSize); if (bWriteHeader) { sb = new System.Text.StringBuilder(); sb.Append(DataTransferConvert.Encode(_delimiter, tblSrc.Columns[0].ColumnName)); for (int i = 1; i < tblSrc.Columns.Count; i++) { sb.Append(DataTransferConvert.Delimiter(_delimiter)); sb.Append(DataTransferConvert.Encode(_delimiter, tblSrc.Columns[i].ColumnName)); } sw.WriteLine(sb.ToString()); } for (int r = 0; r < tblSrc.Rows.Count; r++) { sb = new System.Text.StringBuilder(); sb.Append(DataTransferConvert.Encode(_delimiter, StringUtility.ToString(tblSrc.Rows[r][0]))); for (int i = 1; i < tblSrc.Columns.Count; i++) { sb.Append(DataTransferConvert.Delimiter(_delimiter)); sb.Append(DataTransferConvert.Encode(_delimiter, StringUtility.ToString(tblSrc.Rows[r][i]))); } sw.WriteLine(sb.ToString()); } } catch (Exception er) { error = ExceptionLimnorDatabase.FormExceptionText(er, "Error saving data to file {0}", Filename); TraceLogClass.TraceLog.ShowMessageBox = !bSilent; TraceLogClass.TraceLog.Log(er); } finally { if (sw != null) { sw.Close(); } } return(error); }