示例#1
0
        /// <summary>
        /// We should try to create a designer
        /// </summary>
        /// /// <param name="dict"></param
        /// <param name="designername"></param
        /// <returns>user contributed to the database!</returns>
        internal static DataBaseResults TryUpdatingTables(Dictionary <int, string> dict, string table, string column, string value)
        {
            DataBaseResults newItemTracker = new DataBaseResults();

            if (!String.IsNullOrEmpty(column) &&
                !String.IsNullOrEmpty(value))
            {
                Boolean valueExists = false;
                //Lets make sure it isn't formatting
                foreach (KeyValuePair <int, string> dictItem in dict)
                {
                    if (dictItem.Value.ToLower().Equals(value.ToLower()))
                    {
                        valueExists       = true;
                        newItemTracker.ID = dictItem.Key;
                    }
                }

                //try adding the new value if it isn't a repeat
                if (!valueExists)
                {
                    string[] columnArray = { column };
                    string[] valueArray  = { value };
                    newItemTracker = CreateNewRow(table, columnArray, valueArray);
                }
            }

            return(newItemTracker);
        }
示例#2
0
        /// <summary>
        /// Insert a new value into a new row of a specified table
        /// </summary>
        /// <param name="table"></param>
        /// <param name="column"></param>
        /// <param name="value"></param>
        /// <returns>id of the new row</returns>
        internal static DataBaseResults CreateNewRow(string table, string[] columns, string[] values)
        {
            DataBaseResults returnValue = new DataBaseResults();

            if (columns.Length == values.Length)
            {
                string command = "INSERT INTO " + table + " (";
                foreach (string col in columns)
                {
                    command += col + ",";
                }
                //Remove the last comma
                command  = command.Remove(command.Length - 1, 1);
                command += ") VALUES(";
                foreach (string val in values)
                {
                    command += "\'" + val + "\',";
                }
                //Remove the last comma
                command  = command.Remove(command.Length - 1, 1);
                command += ")";

                using (MySqlCommand insert = GetMySqlCommand(command))
                {
                    Open();

                    if (insert.ExecuteNonQuery() > 0)
                    {
                        string readCommand            = "SELECT * FROM `" + table + "` WHERE " + columns[0] + " = \"" + values[0] + "\"";
                        Dictionary <int, string> dict = ReadFromTable(table, columns[0], readCommand);

                        //TODO should we ever be seeing this sort of error?
                        if (dict.Count > 1)
                        {
                            throw new Exception("0x0000, When reading for the colum there was more than 1 id with the filtered value, " + dict.ToString());
                        }

                        int id = dict.Keys.First();
                        if (id > 0)
                        {
                            returnValue.NewItemAdded = true;
                            returnValue.ID           = id;
                        }
                    }

                    Close();
                }
            }

            return(returnValue);
        }