Exemple #1
0
        protected async Task <MySqlResult> SelectPreparedStatementAsync <T>(T id, params object[] parameters)
        {
            Debug.Assert(typeof(T) == PreparedStatementType, "Invalid prepared statement type.");

            StoredPreparedStatement preparedStatement;

            if (!preparedStatements.TryGetValue(Convert.ToUInt32(id), out preparedStatement))
            {
                Debug.Assert(preparedStatement != null, "Invalid prepared statement id.");
                return(null);
            }

            try
            {
                using (var connection = new MySqlConnection(connectionString))
                {
                    using (var command = new MySqlCommand(preparedStatement.Query, connection))
                    {
                        for (int i = 0; i < preparedStatement.Types.Count; i++)
                        {
                            command.Parameters.Add("", preparedStatement.Types[i]).Value = parameters[i];
                        }

                        await connection.OpenAsync();

                        return(await Task.Run(() =>
                        {
                            using (var commandReader = command.ExecuteReader(CommandBehavior.Default))
                            {
                                using (var result = new MySqlResult())
                                {
                                    result.Load(commandReader);
                                    result.Count = (uint)result.Rows.Count;
                                    return result;
                                }
                            }
                        }));
                    }
                }
            }
            catch (Exception exception)
            {
                log.Error($"An exception occured while selecting prepared statement {id}!");
                log.Error($"Exception: {exception.Message}");
            }

            return(null);
        }
Exemple #2
0
        protected MySqlResult SelectPreparedStatement <T>(T id, params object[] parameters)
        {
            Debug.Assert(typeof(T) == preparedStatementType);

            StoredPreparedStatement preparedStatement;

            if (!preparedStatements.TryGetValue(Convert.ToUInt32(id), out preparedStatement))
            {
                Debug.Assert(preparedStatement != null);
                return(null);
            }

            try
            {
                using (var connection = new MySqlConnection(connectionString))
                {
                    connection.Open();
                    using (var command = new MySqlCommand(preparedStatement.Query, connection))
                    {
                        for (int i = 0; i < preparedStatement.Types.Count; i++)
                        {
                            command.Parameters.Add("", preparedStatement.Types[i]).Value = parameters[i];
                        }

                        using (var commandReader = command.ExecuteReader(CommandBehavior.Default))
                        {
                            using (var result = new MySqlResult())
                            {
                                result.Load(commandReader);
                                result.Count = (uint)result.Rows.Count;
                                return(result);
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine($"An exception occured while selecting prepared statement {id}!");
                Console.WriteLine($"Exception: {exception.Message}");
            }

            return(null);
        }