示例#1
0
        /// <summary>
        /// Execute an UPDATE query.
        /// </summary>
        /// <param name="tableName">The table in which you wish to UPDATE.</param>
        /// <param name="keyValuePairs">The key-value pairs for the data you wish to UPDATE.</param>
        /// <param name="filter">The expression containing the UPDATE filter (i.e. WHERE clause data).</param>
        public void Update(string tableName, Dictionary <string, object> keyValuePairs, Expression filter)
        {
            if (String.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            if (keyValuePairs == null || keyValuePairs.Count < 1)
            {
                throw new ArgumentNullException(nameof(keyValuePairs));
            }

            #region Build-Key-Value-Clause

            string keyValueClause = "";
            int    added          = 0;
            foreach (KeyValuePair <string, object> curr in keyValuePairs)
            {
                if (String.IsNullOrEmpty(curr.Key))
                {
                    continue;
                }

                if (added == 0)
                {
                    if (curr.Value != null)
                    {
                        if (curr.Value is DateTime || curr.Value is DateTime?)
                        {
                            keyValueClause += MysqlHelper.PreparedFieldname(curr.Key) + "='" + DbTimestamp((DateTime)curr.Value) + "'";
                        }
                        else if (curr.Value is int || curr.Value is long || curr.Value is decimal)
                        {
                            keyValueClause += MysqlHelper.PreparedFieldname(curr.Key) + "=" + curr.Value.ToString();
                        }
                        else
                        {
                            if (Helper.IsExtendedCharacters(curr.Value.ToString()))
                            {
                                keyValueClause += MysqlHelper.PreparedFieldname(curr.Key) + "=" + MysqlHelper.PreparedUnicodeValue(curr.Value.ToString());
                            }
                            else
                            {
                                keyValueClause += MysqlHelper.PreparedFieldname(curr.Key) + "=" + MysqlHelper.PreparedStringValue(curr.Value.ToString());
                            }
                        }
                    }
                    else
                    {
                        keyValueClause += MysqlHelper.PreparedFieldname(curr.Key) + "= null";
                    }
                }
                else
                {
                    if (curr.Value != null)
                    {
                        if (curr.Value is DateTime || curr.Value is DateTime?)
                        {
                            keyValueClause += "," + MysqlHelper.PreparedFieldname(curr.Key) + "='" + DbTimestamp((DateTime)curr.Value) + "'";
                        }
                        else if (curr.Value is int || curr.Value is long || curr.Value is decimal)
                        {
                            keyValueClause += "," + MysqlHelper.PreparedFieldname(curr.Key) + "=" + curr.Value.ToString();
                        }
                        else
                        {
                            if (Helper.IsExtendedCharacters(curr.Value.ToString()))
                            {
                                keyValueClause += "," + MysqlHelper.PreparedFieldname(curr.Key) + "=" + MysqlHelper.PreparedUnicodeValue(curr.Value.ToString());
                            }
                            else
                            {
                                keyValueClause += "," + MysqlHelper.PreparedFieldname(curr.Key) + "=" + MysqlHelper.PreparedStringValue(curr.Value.ToString());
                            }
                        }
                    }
                    else
                    {
                        keyValueClause += "," + MysqlHelper.PreparedFieldname(curr.Key) + "= null";
                    }
                }
                added++;
            }

            #endregion

            #region Build-UPDATE-Query-and-Submit

            Query(MysqlHelper.UpdateQuery(tableName, keyValueClause, filter));

            #endregion
        }
示例#2
0
        /// <summary>
        /// Execute an INSERT query.
        /// </summary>
        /// <param name="tableName">The table in which you wish to INSERT.</param>
        /// <param name="keyValuePairs">The key-value pairs for the row you wish to INSERT.</param>
        /// <returns>A DataTable containing the results.</returns>
        public DataTable Insert(string tableName, Dictionary <string, object> keyValuePairs)
        {
            if (String.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            if (keyValuePairs == null || keyValuePairs.Count < 1)
            {
                throw new ArgumentNullException(nameof(keyValuePairs));
            }

            #region Variables

            string keys           = "";
            string values         = "";
            int    insertedId     = 0;
            string retrievalQuery = "";

            #endregion

            #region Build-Key-Value-Pairs

            int added = 0;
            foreach (KeyValuePair <string, object> curr in keyValuePairs)
            {
                if (String.IsNullOrEmpty(curr.Key))
                {
                    continue;
                }

                if (added == 0)
                {
                    #region First

                    keys += MysqlHelper.PreparedFieldname(curr.Key);
                    if (curr.Value != null)
                    {
                        if (curr.Value is DateTime || curr.Value is DateTime?)
                        {
                            values += "'" + DbTimestamp((DateTime)curr.Value) + "'";
                        }
                        else if (curr.Value is int || curr.Value is long || curr.Value is decimal)
                        {
                            values += curr.Value.ToString();
                        }
                        else
                        {
                            if (Helper.IsExtendedCharacters(curr.Value.ToString()))
                            {
                                values += MysqlHelper.PreparedUnicodeValue(curr.Value.ToString());
                            }
                            else
                            {
                                values += MysqlHelper.PreparedStringValue(curr.Value.ToString());
                            }
                        }
                    }
                    else
                    {
                        values += "null";
                    }

                    #endregion
                }
                else
                {
                    #region Subsequent

                    keys += "," + MysqlHelper.PreparedFieldname(curr.Key);
                    if (curr.Value != null)
                    {
                        if (curr.Value is DateTime || curr.Value is DateTime?)
                        {
                            values += ",'" + DbTimestamp((DateTime)curr.Value) + "'";
                        }
                        else if (curr.Value is int || curr.Value is long || curr.Value is decimal)
                        {
                            values += "," + curr.Value.ToString();
                        }
                        else
                        {
                            if (Helper.IsExtendedCharacters(curr.Value.ToString()))
                            {
                                values += "," + MysqlHelper.PreparedUnicodeValue(curr.Value.ToString());
                            }
                            else
                            {
                                values += "," + MysqlHelper.PreparedStringValue(curr.Value.ToString());
                            }
                        }
                    }
                    else
                    {
                        values += ",null";
                    }

                    #endregion
                }

                added++;
            }

            #endregion

            #region Build-INSERT-Query-and-Submit

            DataTable result = Query(MysqlHelper.InsertQuery(tableName, keys, values));

            #endregion

            #region Post-Retrieval

            if (!Helper.DataTableIsNullOrEmpty(result))
            {
                bool idFound = false;

                string primaryKeyColumn = GetPrimaryKeyColumn(tableName);

                foreach (DataRow curr in result.Rows)
                {
                    if (Int32.TryParse(curr["id"].ToString(), out insertedId))
                    {
                        idFound = true;
                        break;
                    }
                }

                if (!idFound)
                {
                    result = null;
                }
                else
                {
                    retrievalQuery = "SELECT * FROM `" + tableName + "` WHERE " + primaryKeyColumn + "=" + insertedId;
                    result         = Query(retrievalQuery);
                }
            }

            #endregion

            return(result);
        }