Exemplo n.º 1
0
        public Subdivision(DbDataReader reader)
            : this()
        {
            this.Id = Convert.ToInt32(reader["id"]);

            var columnNames = reader.GetSchemaTable().Rows.OfType<DataRow>().Select(r => (string)r["ColumnName"]);

            if (columnNames.Contains("population") && reader["population"] != DBNull.Value)
            {
                this.Population = Convert.ToInt32(reader["population"]);
            }

            if (columnNames.Contains("beer_volume") && reader["beer_volume"] != DBNull.Value)
            {
                this.Volumes.Beer = Convert.ToInt64(reader["beer_volume"]);
            }

            if (columnNames.Contains("wine_volume") && reader["wine_volume"] != DBNull.Value)
            {
                this.Volumes.Wine = Convert.ToInt64(reader["wine_volume"]);
            }

            if (columnNames.Contains("spirits_volume") && reader["spirits_volume"] != DBNull.Value)
            {
                this.Volumes.Spirits = Convert.ToInt64(reader["spirits_volume"]);
            }

            if (columnNames.Contains("boundary") && reader["boundary"] != DBNull.Value)
            {
                var boundary = reader["boundary"] as string;
                var featureWrapper =
            $@"{{ ""type"": ""FeatureCollection"",
            ""features"": [
              {{ ""type"": ""Feature"",
            ""geometry"": {boundary},
            ""properties"":{{}}
              }}
              ]
            }}";
                var featurecoll = JsonConvert.DeserializeObject(boundary);
                this.GeoJSON = featureWrapper;
            }

            if (columnNames.Contains("name") && reader["name"] != DBNull.Value)
            {
                this.Name = reader["name"] as string;
            }

            if (columnNames.Contains("centre") && reader["centre"] != DBNull.Value)
            {
                this.GeoJsonCentre = reader["centre"] as string;
                var geocentre = JsonConvert.DeserializeObject<Point>(this.GeoJsonCentre);
                this.CentreLatitude = ((GeographicPosition)geocentre.Coordinates).Latitude;
                this.CentreLongitude = ((GeographicPosition)geocentre.Coordinates).Longitude;
            }
        }
Exemplo n.º 2
0
        public LcboStore(DbDataReader reader)
            : this()
        {
            this.Id = Convert.ToInt32(reader["id"]);

            var columnNames = reader.GetSchemaTable().Rows.OfType<DataRow>().Select(r => (string)r["ColumnName"]);

            if (columnNames.Contains("name") && reader["name"] != DBNull.Value)
            {
                this.Name = reader["name"] as string;
            }

            if (columnNames.Contains("beer_volume") && reader["beer_volume"] != DBNull.Value)
            {
                this.Volumes.Beer = Convert.ToInt32(reader["beer_volume"]);
            }

            if (columnNames.Contains("wine_volume") && reader["wine_volume"] != DBNull.Value)
            {
                this.Volumes.Wine = Convert.ToInt32(reader["wine_volume"]);
            }

            if (columnNames.Contains("spirits_volume") && reader["spirits_volume"] != DBNull.Value)
            {
                this.Volumes.Spirits = Convert.ToInt32(reader["spirits_volume"]);
            }

            if (columnNames.Contains("city") && reader["city"] != DBNull.Value)
            {
                this.City = reader["city"] as string;
            }

            if (columnNames.Contains("subdivision_id") && reader["subdivision_id"] != DBNull.Value)
            {
                this.SubdivisionId = Convert.ToInt32(reader["subdivision_id"]);
            }

            if (columnNames.Contains("location") && reader["location"] != DBNull.Value)
            {
                var location = reader["location"] as string;
                var geojsonlocation = JsonConvert.DeserializeObject<Point>(location);
                var properties = new Dictionary<string, object>
                {
                    { "name", this.Name },
                    { "city", this.City },
                    { "beerVolume", this.Volumes.Beer },
                    { "wineVolume", this.Volumes.Wine },
                    { "spiritsVolume", this.Volumes.Spirits },
                    { "totalVolume", this.Volumes.Total },
                };

                var feature = new Feature(geojsonlocation, properties);
                this.GeoJSON = feature;
            }
        }
Exemplo n.º 3
0
 static int GetSchemaTable(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         System.Data.Common.DbDataReader obj = (System.Data.Common.DbDataReader)ToLua.CheckObject(L, 1, typeof(System.Data.Common.DbDataReader));
         System.Data.DataTable           o   = obj.GetSchemaTable();
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Exemplo n.º 4
0
    public static IEnumerable <PSObject> Translate(System.Data.Common.DbDataReader dataReader, Boolean ProviderTypes)
    {
        List <Map> MapList = new List <Map>();
        int        Ord     = 0;

        foreach (var x in dataReader.GetSchemaTable().Select("", "ColumnOrdinal"))
        {
            MapList.Add(new Map(Ord, x["DataType"].ToString(), x["ColumnName"].ToString())); // x("AllowDBNull"),
            Ord += 1;
        }

        PSObject responseObject = new PSObject();

        while (dataReader.Read())
        {
            PSObject psObj = new PSObject();
            foreach (Map m in MapList)
            {
                {
                    var withBlock = psObj.Members;
                    if (dataReader.IsDBNull(m.Ordinal))
                    {
                        withBlock.Add(new PSNoteProperty(m.Name, null), true);
                    }
                    else
                    {
                        try
                        {
                            if (ProviderTypes)
                            {
                                withBlock.Add(new PSNoteProperty(m.Name, dataReader.GetProviderSpecificValue(m.Ordinal)), true);
                            }
                            else
                            {
                                withBlock.Add(new PSNoteProperty(m.Name, dataReader.GetValue(m.Ordinal)), true);
                            }
                        }
                        catch (Exception ex)
                        {
                            string msg = string.Format("Failed to translate, ColumnName = {0} | ColumnOrdinal = {1} | ColumnType = {2} | ToStringValue = '{3}' | See InnerException for details", m.Name, m.Ordinal, m.DataType, dataReader.GetValue(m.Ordinal).ToString());
                            throw new Exception(msg, ex);
                        }
                    }
                }
            }
            yield return(psObj);
        }
    }
 public void Build(DbDataReader reader)
 {
     var tableSchema = reader.GetSchemaTable();
     if (tableSchema == null)
     {
         return;
     }
     FetchTableColumnsName(tableSchema);
     //reader.Read();
     BuildHandle((colName, property) =>
         {
             var propertyType = property.PropertyType;
             var exists = CurrentTableSchemaColumnsName.Find(s => String.Equals(s, colName, StringComparison.InvariantCultureIgnoreCase));
             if (String.IsNullOrWhiteSpace(exists))
             {
                 return false;
             }
             var index = reader.GetOrdinal(exists);
             try
             {
                 if (reader.IsDBNull(index))
                 {
                     return false;
                 }
                 var value = ChangeType(reader.GetValue(index), propertyType);
                 property.SetValue(this, value, null);
             }
             catch (Exception ex)
             {
                 Error.ErrorCode = ErrorMapping.BUSINESS_FFFF;
                 Error.SimpleErrorDescription = ex.Message;
                 return false;
             }
             return true;
         });
 }
        /// <summary>
        /// Gets the column names from a data reader.
        /// </summary>
        private static IEnumerable<string> GetColumnNames(DbDataReader reader)
        {
            List<string> columnNames = new List<string>();
            foreach (DataRow r in reader.GetSchemaTable().Rows)
            {
                columnNames.Add(r[SchemaTableColumn.ColumnName].ToString());
            }

            return columnNames;
        }
Exemplo n.º 7
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="header"></param>
        public void Write(DbDataReader reader, string[] header)
        {
            if ( reader == null )
                throw new ArgumentNullException( "reader" );
            ShouldBeDisposed();
            if ( header == null )
                Write( reader );

            DataTable schema = reader.GetSchemaTable();
            /*
             * ヘッダーを書き出します。
             */
            foreach ( string field in header ) {
                this.Write( field );
            }
            this.WriteLine();

            if ( reader.HasRows ) {
                while ( reader.Read() ) {

                    for ( int i = 0; i < reader.FieldCount; ++i ) {
                        Type datatype = schema.Rows[i]["DataType"] as Type;

                        this.Write( reader[i], datatype );
                    }
                    this.WriteLine();
                }
            }
            this.Flush();
        }
Exemplo n.º 8
0
        /// <summary>
        /// データリーダーを受け取り、その内容を CSV ファイルに書き出します。
        /// </summary>
        /// <param name="reader"></param>
        public void Write(DbDataReader reader)
        {
            if ( reader == null )
                throw new ArgumentNullException( "reader" );
            ShouldBeDisposed();

            DataTable schema = reader.GetSchemaTable();
            foreach ( DataRow row in schema.Rows ) {
                this.Write( row["ColumnName"], typeof( string ) );
            }
            this.WriteLine();

            if ( reader.HasRows ) {
                while ( reader.Read() ) {

                    for ( int i = 0; i < reader.FieldCount; ++i ) {
                        Type datatype = schema.Rows[i]["DataType"] as Type;

                        this.Write( reader[i], datatype );
                    }
                    this.WriteLine();
                }
            }
            this.Flush();
        }
 /// <summary>
 /// Check if an DbDataReader contains a field
 /// </summary>
 /// <param name="reader">The reader</param>
 /// <param name="columnName">The column name</param>
 /// <returns></returns>
 public static bool ColumnExists(DbDataReader reader, string columnName)
 {
     reader.GetSchemaTable().DefaultView.RowFilter = "ColumnName= '" + columnName + "'";
     return (reader.GetSchemaTable().DefaultView.Count > 0);
 }
Exemplo n.º 10
0
 protected virtual DataTable GetSchemaTable(DbCommand cmd)
 {
     using (DbDataReader rdr = cmd.ExecuteReader())
         return(rdr.GetSchemaTable());
 }
Exemplo n.º 11
0
        /// <summary>
        /// 由于DataSet得灵活性,提供了一个将DataReader转换为DataSet的工具方法
        /// </summary>
        /// <param name="reader">DbDataReader对象</param>
        /// <returns>DataSet</returns>
        public static DataSet ConvertDataReaderToDataSet(DbDataReader reader)
        {
			ExceptionHelper.TrueThrow<ArgumentNullException>(reader == null, "reader");

            DataSet dataSet = new DataSet();
			dataSet.Locale = System.Globalization.CultureInfo.InvariantCulture;

            do
            {
				DataTable schemaTable = reader.GetSchemaTable();
                DataTable dataTable = new DataTable();

                if (schemaTable != null)
                {
                    // A query returning records was executed
                    for (int i = 0; i < schemaTable.Rows.Count; i++)
                    {
                        DataRow dataRow = schemaTable.Rows[i];
                        // Create a column name that is unique in the data table
                        string columnName = (string)dataRow["ColumnName"]; //+ "<C" + i + "/>";
                        // Add the column definition to the data table
                        DataColumn column = new DataColumn(columnName, (Type)dataRow["DataType"]);
                        dataTable.Columns.Add(column);
                    }
                    dataSet.Tables.Add(dataTable);
                    // Fill the data table we just created
                    //try
                    //{
                        while (reader.Read())
                        {
                            DataRow dataRow = dataTable.NewRow();
                            for (int i = 0; i < reader.FieldCount; i++)
                                dataRow[i] = reader.GetValue(i);
                            dataTable.Rows.Add(dataRow);
                        }
                    //}
                    //catch(Exception exception)
                    //{
                    //    TraceException(exception);
                    //}
                }
                else
                {
                    // No records were returned
                    DataColumn column = new DataColumn("RowsAffected");
                    dataTable.Columns.Add(column);
                    dataSet.Tables.Add(dataTable);
                    DataRow dataRow = dataTable.NewRow();
                    dataRow[0] = reader.RecordsAffected;
                    dataTable.Rows.Add(dataRow);
                }
            }
            while (reader.NextResult());
            return dataSet;
        }