예제 #1
0
 /// <summary>
 /// Returns the column value as a nullable byte.
 /// </summary>
 /// <param name="reader">The data reader.</param>
 /// <param name="ordinal">The zero-based column position.</param>
 /// <returns>The nullable column value.</returns>
 public static byte?GetNullableByte(this NpgsqlDataReader reader, int ordinal)
 {
     if (reader[ordinal] == DBNull.Value)
     {
         return(null);
     }
     else
     {
         return(reader.GetByte(ordinal));
     }
 }
 /// <summary>
 /// Get the value of a column as a byte, given its zero-based ordinal.
 /// </summary>
 /// <param name="i"></param>
 /// <returns></returns>
 public byte GetByte(int i)
 {
     return(_rdr.GetByte(i));
 }
예제 #3
0
 public override byte GetByte(int ordinal)
 {
     return(source.GetByte(ordinal));
 }
 /// <summary>
 /// Get the value of a column as a byte, given its zero-based ordinal.
 /// </summary>
 public byte GetByte(int i) => _rdr.GetByte(i);
예제 #5
0
        public static IEnumerable <T> SimpleQuery <T>(this IDbConnection p_connection, string p_sql = "")
        {
            if (string.IsNullOrEmpty(p_sql))
            {
                p_sql = $@"SELECT * FROM ""{typeof(T).Name}""";
            }

            p_connection.Open();

            using (NpgsqlCommand command = new NpgsqlCommand(p_sql, (NpgsqlConnection)p_connection))
            {
                using (NpgsqlDataReader sqlDataReader = command.ExecuteReader())
                {
                    Type           type          = typeof(T);
                    PropertyInfo[] propertyInfos = type.GetProperties();
                    IList <T>      result        = new List <T>();

                    while (sqlDataReader.Read())
                    {
                        object entity = Activator.CreateInstance(type);
                        propertyInfos.Each(p_info =>
                        {
                            switch (p_info.PropertyType.Name.ToLower())
                            {
                            case "char":
                                p_info.SetValue(entity, sqlDataReader.GetChar(p_info.Name));
                                break;

                            case "boolean":
                                p_info.SetValue(entity, sqlDataReader.GetBoolean(p_info.Name));
                                break;

                            case "string":
                                p_info.SetValue(entity, sqlDataReader.GetString(p_info.Name));
                                break;

                            case "byte":
                                p_info.SetValue(entity, sqlDataReader.GetByte(p_info.Name));
                                break;

                            case "decimal":
                                p_info.SetValue(entity, sqlDataReader.GetDecimal(p_info.Name));
                                break;

                            case "float":
                                p_info.SetValue(entity, sqlDataReader.GetFloat(p_info.Name));
                                break;

                            case "double":
                                p_info.SetValue(entity, sqlDataReader.GetDouble(p_info.Name));
                                break;

                            case "int16":
                                p_info.SetValue(entity, sqlDataReader.GetInt16(p_info.Name));
                                break;

                            case "int32":
                                p_info.SetValue(entity, sqlDataReader.GetInt32(p_info.Name));
                                break;

                            case "int64":
                                p_info.SetValue(entity, sqlDataReader.GetInt64(p_info.Name));
                                break;

                            default:
                                p_info.SetValue(entity, sqlDataReader.GetValue(p_info.Name));
                                break;
                            }
                        });

                        result.Add((T)entity);
                    }

                    return(result);
                }
            }
        }
예제 #6
0
        public static T SimpleQueryFirstOrDefault <T>(this IDbConnection p_connection, string p_sql)
        {
            Type   type   = typeof(T);
            object entity = Activator.CreateInstance(type);

            p_connection.Open();

            using (NpgsqlCommand command = new NpgsqlCommand(p_sql, (NpgsqlConnection)p_connection))
            {
                using (NpgsqlDataReader sqlDataReader = command.ExecuteReader())
                {
                    PropertyInfo[] propertyInfos = type.GetProperties();

                    while (sqlDataReader.Read())
                    {
                        propertyInfos.Each(p_info =>
                        {
                            switch (p_info.PropertyType.Name.ToLower())
                            {
                            case "char":
                                p_info.SetValue(entity, sqlDataReader.GetChar(p_info.Name));
                                break;

                            case "boolean":
                                p_info.SetValue(entity, sqlDataReader.GetBoolean(p_info.Name));
                                break;

                            case "string":
                                p_info.SetValue(entity, sqlDataReader.GetString(p_info.Name));
                                break;

                            case "byte":
                                p_info.SetValue(entity, sqlDataReader.GetByte(p_info.Name));
                                break;

                            case "decimal":
                                p_info.SetValue(entity, sqlDataReader.GetDecimal(p_info.Name));
                                break;

                            case "float":
                                p_info.SetValue(entity, sqlDataReader.GetFloat(p_info.Name));
                                break;

                            case "double":
                                p_info.SetValue(entity, sqlDataReader.GetDouble(p_info.Name));
                                break;

                            case "int16":
                                p_info.SetValue(entity, sqlDataReader.GetInt16(p_info.Name));
                                break;

                            case "int32":
                                p_info.SetValue(entity, sqlDataReader.GetInt32(p_info.Name));
                                break;

                            case "int64":
                                p_info.SetValue(entity, sqlDataReader.GetInt64(p_info.Name));
                                break;

                            default:
                                p_info.SetValue(entity, sqlDataReader.GetValue(p_info.Name));
                                break;
                            }
                        });
                        return((T)entity);
                    }
                }
            }

            return((T)entity);
        }
        public ParameterSchema[] GetCommandParameters(string connectionString, CommandSchema commandSchema)
        {
            string arg = commandSchema.ExtendedProperties["CS_Name"].Value as string;
            List <ParameterSchema> list = new List <ParameterSchema>();

            using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
            {
                npgsqlConnection.Open();
                string text = string.Format("select * from information_schema.parameters where specific_schema='public' and specific_name = '{0}' order by ordinal_position", arg);
                using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand(text, npgsqlConnection))
                {
                    using (NpgsqlDataReader npgsqlDataReader = npgsqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (npgsqlDataReader.Read())
                        {
                            string name      = npgsqlDataReader.IsDBNull(7) ? string.Empty : npgsqlDataReader.GetString(7);
                            int    size      = npgsqlDataReader.IsDBNull(9) ? 0 : npgsqlDataReader.GetInt32(9);
                            int    scale     = npgsqlDataReader.IsDBNull(19) ? 0 : npgsqlDataReader.GetInt32(19);
                            byte   precision = npgsqlDataReader.IsDBNull(17) ? (byte)0 : npgsqlDataReader.GetByte(17);
                            string @string   = npgsqlDataReader.GetString(8);
                            list.Add(new ParameterSchema(commandSchema, name, PostgreSQLSchemaProvider.GetParameterDirection(npgsqlDataReader.GetString(4)), PostgreSQLSchemaProvider.GetDbType(npgsqlDataReader.GetString(8)), @string, size, precision, scale, false, new ExtendedProperty[]
                            {
                                new ExtendedProperty("NpgsqlDbType", PostgreSQLSchemaProvider.GetNativeDbType(@string), DbType.String)
                            }));
                        }
                        if (!npgsqlDataReader.IsClosed)
                        {
                            npgsqlDataReader.Close();
                        }
                    }
                }
                if (npgsqlConnection.State != ConnectionState.Closed)
                {
                    npgsqlConnection.Close();
                }
            }
            return(list.ToArray());
        }