Пример #1
0
 private void AddDataTableColumns(DataTable dataTable, XapDataReader dr)
 {
     for (int x = 0; x <= dr.FieldCount - 1; x++)
     {
         DataColumn column = new DataColumn(dr.GetName(x), dr.GetFieldType(x));
         dataTable.Columns.Add(column);
     }
 }
Пример #2
0
        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();
            }
        }
Пример #3
0
        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();
            }
        }
Пример #4
0
        //TODO:  Look at injecting XapEnvironment class into DbProvider
        void IXapDataProvider.ExecuteCsv(string filePath)
        {
            StringBuilder sb      = new StringBuilder();
            StreamWriter  _logger = null;
            string        tmp     = string.Empty;

            try {
                _logger = new StreamWriter(XapEnvironment.Instance.MapFolderPath(filePath), false);

                OpenConnection();

                XapDataReader dr = new XapDataReader(_dbCommand.ExecuteReader());

                for (int x = 0; x < dr.FieldCount; x++)
                {
                    if (x + 1 < dr.FieldCount)
                    {
                        _logger.Write("\"" + dr.GetName(x) + "\"" + ",");
                    }
                    else
                    {
                        _logger.Write("\"" + dr.GetName(x) + "\"\n");
                    }
                }

                string obj = string.Empty;
                while (dr.Read())
                {
                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        obj = dr.GetValue(i).ToString();

                        if (obj == null || string.IsNullOrEmpty(obj.ToString()))
                        {
                            if (i + 1 < dr.FieldCount)
                            {
                                _logger.Write("\"" + string.Empty + "\"" + ",");
                            }
                            else
                            {
                                _logger.Write("\"" + string.Empty + "\"\n");
                            }
                        }
                        else
                        {
                            if (i + 1 < dr.FieldCount)
                            {
                                _logger.Write("\"" + dr.GetValue(i).ToString().TrimEnd() + "\"" + ",");
                            }
                            else
                            {
                                _logger.Write("\"" + dr.GetValue(i).ToString().TrimEnd() + "\"\n");
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                throw new XapException($"Error executing csv for {_dbConnectionContext.DbKey}", ex);
            } finally {
                _logger.Close();
                ((IXapDataProvider)this).CloseConnection();
            }
        }