public async Task <IEnumerable <User> > GetAllAsync()
        {
            List <User> users = new List <User>();

            //Using EntityConnection, EntityCommand, and Async methods
            using (var db = new EntityConnection("name=AppEntities"))
            {
                await db.OpenAsync();

                EntityCommand command = db.CreateCommand();
                command.CommandText = "SELECT VALUE u FROM AppEntities.USERS AS u";

                using (var entityDataReader =
                           await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess))
                {
                    while (entityDataReader.Read())
                    {
                        users.Add(new User()
                        {
                            Id        = (int)entityDataReader.GetValue(0),
                            FirstName = (string)entityDataReader.GetValue(1),
                            LastName  = (string)entityDataReader.GetValue(2),
                            City      = (string)entityDataReader.GetValue(3),
                            Username  = (string)entityDataReader.GetValue(4)
                        });
                    }
                }
            }

            return(users);
        }
        private async Task <List <Product> > QueryAsync(string connectionString, string commandString)
        {
            var connection = new EntityConnection(connectionString);
            await connection.OpenAsync();

            var command = new EntityCommand(commandString, connection);
            var reader  = command.ExecuteReaderAsync(CommandBehavior.SequentialAccess).Result;
            var list    = new List <Product>();

            do
            {
                while (await reader.ReadAsync())
                {
                    var values = new List <object>();
                    for (var i = 0; i < reader.FieldCount; i++)
                    {
                        values.Add(await reader.GetFieldValueAsync <object>(i));
                    }
                    list.Add(CreateProduct(values));
                }
            }while (await reader.NextResultAsync());

            connection.Close();

            return(list);
        }
示例#3
0
        private static async Task EntityClientSample()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["Formula1Entities"].ConnectionString;
            var    connection       = new EntityConnection(connectionString);
            await connection.OpenAsync();

            EntityCommand command = connection.CreateCommand();

            command.CommandText = "[Formula1Entities].[Racers]";
            DbDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection);

            while (await reader.ReadAsync())
            {
                Console.WriteLine("{0} {1}", reader["FirstName"], reader["LastName"]);
            }
            reader.Close();
        }
示例#4
0
        private static async void EntitySqlDemo2()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["Formula1Entities"].ConnectionString;
            var    connection       = new EntityConnection(connectionString);
            await connection.OpenAsync();

            EntityCommand command = connection.CreateCommand();

            command.CommandText = "SELECT Racers.FirstName, Racers.LastName FROM Formula1Entities.Racers";
            DbDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection);

            while (await reader.ReadAsync())
            {
                Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1));
            }
            reader.Close();
        }
示例#5
0
        private static async void EntitySqlWithParameters()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["Formula1Entities"].ConnectionString;
            var    connection       = new EntityConnection(connectionString);
            await connection.OpenAsync();

            EntityCommand command = connection.CreateCommand();

            command.CommandText = "SELECT VALUE it FROM [Formula1Entities].[Racers] AS it " +
                                  "WHERE it.Nationality = @Country";
            command.Parameters.AddWithValue("Country", "Austria");

            DbDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection);

            while (await reader.ReadAsync())
            {
                Console.WriteLine("{0} {1}", reader["FirstName"], reader["LastName"]);
            }
            reader.Close();
        }