DataSet IXapDataProvider.ExecuteDataSet() { try { OpenConnection(); XapDataReader dr = new XapDataReader(_dbCommand.ExecuteReader()); DataSet dataSet = new DataSet(); DataTable dataTable = null; do { dataTable = new DataTable(); AddDataTableColumns(dataTable, dr); dataSet.Tables.Add(dataTable); while (dr.Read()) { AddDataRow(dataTable, dr); } } while (dr.NextResult()); return(dataSet); } catch (Exception ex) { throw new XapException($"Error executing dataset for {_dbConnectionContext.DbKey}", ex); } finally { ((IXapDataProvider)this).CloseConnection(); } }
XmlDocument IXapDataProvider.ExecuteXml() { ArrayList columnNames = null; try { OpenConnection(); XapDataReader dr = new XapDataReader(_dbCommand.ExecuteReader()); XmlDocument xDoc = new XmlDocument(); XmlElement rootNode = xDoc.CreateElement("ResultSets"); xDoc.AppendChild(rootNode); do { columnNames = new ArrayList(dr.FieldCount); for (int x = 0; x <= dr.FieldCount - 1; x++) { columnNames.Add(dr.GetName(x)); } string obj = string.Empty; XmlElement resultNode = xDoc.CreateElement("ResultSet"); XmlElement rowNode = null; XmlElement columnNode = null; while (dr.Read()) { rowNode = xDoc.CreateElement("Row"); for (int i = 0; i <= dr.FieldCount - 1; i++) { columnNode = xDoc.CreateElement(columnNames[i].ToString()); obj = dr.GetValue(i).ToString().Trim(); if (obj == null || string.IsNullOrEmpty(obj.ToString())) { obj = string.Empty; } columnNode.InnerText = obj.ToString(); rowNode.AppendChild(columnNode); } resultNode.AppendChild(rowNode); } rootNode.AppendChild(resultNode); } while (dr.NextResult()); return(xDoc); } catch (Exception ex) { throw new XapException($"Error executing xml for {_dbConnectionContext.DbKey}", ex); } finally { ((IXapDataProvider)this).CloseConnection(); } }
string IXapDataProvider.ExecuteJson() { StringBuilder json = new StringBuilder(); string retVal = string.Empty; try { OpenConnection(); XapDataReader dr = new XapDataReader(_dbCommand.ExecuteReader()); json.AppendLine("{\"data\":["); do { string obj = string.Empty; string colName = string.Empty; while (dr.Read()) { json.AppendLine("{"); for (int i = 0; i <= dr.FieldCount - 1; i++) { obj = dr.GetValue(i).ToString().Trim(); colName = dr.GetName(i); if (obj == null || string.IsNullOrEmpty(obj.ToString())) { obj = string.Empty; } json.AppendLine(string.Format("\"{0}\":\"{1}\",", colName, obj.ToString())); } json = json.RemoveLast(","); json.AppendLine("},"); } } while (dr.NextResult()); json = json.RemoveLast(","); json.AppendLine("]}"); retVal = json.ToString(); return(retVal); } catch (Exception ex) { throw new XapException($"Error executing json for {_dbConnectionContext.DbKey}", ex); } finally { ((IXapDataProvider)this).CloseConnection(); } }