Beispiel #1
0
        /// <summary>
        ///     Executes a query on the database returning an array of rows.
        /// </summary>
        /// <returns>The rows of the database selected.</returns>
        /// <param name="query">The query.</param>
        /// <param name="paramaters">The paramaters to be added to this query.</param>
        public override DatabaseRow[] ExecuteQuery(string query, params QueryParameter[] parameters)
        {
            try
            {
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
                    using (MySqlCommand command = new MySqlCommand(query, connection))
                    {
                        foreach (QueryParameter parameter in parameters)
                        {
                            command.Parameters.AddWithValue(parameter.name, parameter.obj);
                        }

                        connection.Open();
                        using (MySqlDataReader reader = command.ExecuteReader())
                        {
                            int fieldCount          = reader.FieldCount;
                            List <DatabaseRow> rows = new List <DatabaseRow>();

                            while (reader.Read())
                            {
                                //For each row create a DatabaseRow
                                DatabaseRow row = new DatabaseRow();

                                //And add each field to it
                                for (int i = 0; i < fieldCount; i++)
                                {
                                    row.Add(
                                        reader.GetName(i),
                                        reader.GetValue(i)
                                        );
                                }

                                //Add it to the rows
                                rows.Add(row);
                            }

                            return(rows.ToArray());
                        }
                    }
                }
            }
            catch (MySqlException e)
            {
                throw new DatabaseException(e.Message, e);
            }
        }
        public DatabaseRow[] ExecuteQuery(string query, params QueryParameter[] parameters)
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                using (var command = new MySqlCommand(query, connection))
                {
                    foreach (var parameter in parameters)
                    {
                        command.Parameters.Add(parameter.ParameterName, parameter.FieldType, parameter.Size, parameter.Column).Value = parameter.Value;
                    }

                    connection.Open();
                    using (var reader = command.ExecuteReader())
                    {
                        var fieldCount = reader.FieldCount;
                        var rows       = new List <DatabaseRow>();

                        while (reader.Read())
                        {
                            //For each row create a DatabaseRow
                            var row = new DatabaseRow();

                            //And add each field to it
                            for (var i = 0; i < fieldCount; i++)
                            {
                                row.Add(reader.GetName(i), !reader.IsDBNull(i) ? reader.GetValue(i) : null);
                            }

                            //Add it to the rows
                            rows.Add(row);
                        }

                        return(rows.ToArray());
                    }
                }
            }
        }