コード例 #1
0
ファイル: SQLController.cs プロジェクト: Guillemsc/Fast
        public void ExecutePostQuery(SQLQuery sql_query, Action on_success = null, Action <SQLError> on_fail = null)
        {
            if (connection != null)
            {
                if (connected && sql_query != null)
                {
                    MySqlCommand adapter = new MySqlCommand(sql_query.Query, connection);

                    foreach (KeyValuePair <string, object> entry in sql_query.Parameters)
                    {
                        adapter.Parameters.AddWithValue(entry.Key, entry.Value);
                    }

                    adapter.ExecuteNonQueryAsync().ContinueWith(delegate(Task task)
                    {
                        string error_msg    = "";
                        Exception exception = null;

                        bool has_errors = task.HasErrors(out error_msg, out exception);

                        if (!has_errors)
                        {
                            if (on_success != null)
                            {
                                on_success.Invoke();
                            }
                        }
                        else
                        {
                            SQLError ret       = new SQLError();
                            ret.ErrorMessage   = error_msg;
                            ret.ErrorException = exception;

                            if (on_fail != null)
                            {
                                on_fail.Invoke(ret);
                            }
                        }
                    });
                }
            }
        }
コード例 #2
0
ファイル: SQLController.cs プロジェクト: Guillemsc/Fast
        public void ExecuteGetQuery(SQLQuery sql_query, Action <Data.GridData> on_success = null, Action <SQLError> on_fail = null)
        {
            if (connection != null && sql_query != null)
            {
                if (connected)
                {
                    MySqlCommand adapter = new MySqlCommand(sql_query.Query, connection);

                    foreach (KeyValuePair <string, object> entry in sql_query.Parameters)
                    {
                        adapter.Parameters.AddWithValue(entry.Key, entry.Value);
                    }

                    adapter.ExecuteReaderAsync().ContinueWith(delegate(Task <DbDataReader> task)
                    {
                        string error_msg    = "";
                        Exception exception = null;

                        bool has_errors = task.HasErrors(out error_msg, out exception);

                        if (!has_errors)
                        {
                            List <List <object> > query_data = new List <List <object> >();

                            if (task.Result.HasRows)
                            {
                                while (task.Result.Read())
                                {
                                    List <object> row = new List <object>(task.Result.FieldCount);

                                    int fields = task.Result.FieldCount;

                                    for (int i = 0; i < fields; ++i)
                                    {
                                        object curr_field = task.Result.GetValue(i);

                                        row.Add(curr_field);
                                    }

                                    query_data.Add(row);
                                }
                            }

                            task.Result.Close();

                            Data.GridData ret = new Data.GridData(query_data);

                            if (on_success != null)
                            {
                                on_success.Invoke(ret);
                            }
                        }
                        else
                        {
                            SQLError ret       = new SQLError();
                            ret.ErrorMessage   = error_msg;
                            ret.ErrorException = exception;

                            if (on_fail != null)
                            {
                                on_fail.Invoke(ret);
                            }
                        }
                    });
                }
            }
        }