FreakOut.classes.DatabaseConnector.InsertGame C# (CSharp) Method

InsertGame() public method

public InsertGame ( Game GameToInsert ) : int
GameToInsert Game
return int
        public int InsertGame(Game GameToInsert)
        {
            //Add a game to the database
            //Check if everything is correct
            if (GameToInsert.Name == null || GameToInsert.InfoXML == null || GameToInsert.ReleaseDate == null || GameToInsert.scraper_gdb_id == 0)
            {
                LogInstance.WriteToFile(3, "Unable to add game to the database. This is critical!", LogSource);
                LogInstance.WriteToFile(3, "GameToInsert - Contents:\n" + GameToInsert.Name + "\n" + GameToInsert.ReleaseDate.ToString() + "\n" + GameToInsert.scraper_gdb_id + "\n" + GameToInsert.InfoXML, LogSource);
                return 0;
            }

            LogInstance.WriteToFile(1, "Adding game " + GameToInsert.Name + " to the database.", LogSource);

            if (!connectionopen)
            {
                DBConnector.Open();
                connectionopen = true;
            }

            FbTransaction DBTransaction = DBConnector.BeginTransaction();
            string XMLString = GameToInsert.InfoXML.InnerXml.Replace(GameToInsert.InfoXML.InnerXml.Replace("<data>", ""), GameToInsert.InfoXML.InnerXml.Replace("</data>", ""));
            XMLString = XMLString.Replace("'", "''");
            FbCommand DBCommand;

            if (GameToInsert.Platform == null)
                DBCommand = new FbCommand("INSERT INTO games (name_of_game, info_xml, downloaded, snatched, wanted, ReleaseDate, TGDB_ID, Platform) VALUES ('" + GameToInsert.Name + "', '" + XMLString + "', 0, 0, 1, '" + GameToInsert.ReleaseDate.Day + "/" + GameToInsert.ReleaseDate.Month + "/" + GameToInsert.ReleaseDate.Year + "','" + GameToInsert.scraper_gdb_id + "','None')", DBConnector, DBTransaction);
            else
                DBCommand = new FbCommand("INSERT INTO games (name_of_game, info_xml, downloaded, snatched, wanted, ReleaseDate, TGDB_ID, Platform) VALUES ('" + GameToInsert.Name + "', '" + XMLString + "', 0, 0, 1, '" + GameToInsert.ReleaseDate.Day + "/" + GameToInsert.ReleaseDate.Month + "/" + GameToInsert.ReleaseDate.Year + "','" + GameToInsert.scraper_gdb_id + "','" + GameToInsert.Platform + "')", DBConnector, DBTransaction);

            DBCommand.ExecuteNonQuery();
            DBTransaction.Commit();
            DBCommand = new FbCommand("SELECT id FROM games WHERE name_of_game = '" + GameToInsert.Name + "'", DBConnector, DBTransaction);
            FbDataReader DBReader = DBCommand.ExecuteReader();
            DBReader.Read();
            DBConnector.Close();
            connectionopen = false;
            return DBReader.GetInt32(0);
        }

Usage Example

Exemplo n.º 1
0
        public Nintendo64_PostProcess(string DirectoryToProcess, char SpaceChar, bool MetaEnabled, bool RenamingEnabled, bool MovingEnabled, DatabaseConnector DBConnection)
        {
            MetaEnabled = true;
            RenamingEnabled = true;
            MovingEnabled = true;

            string[] FilesToProcess = System.IO.Directory.GetFiles(@DirectoryToProcess);
            Progress ProgressForm = new Progress();
            ProgressForm.Show();
            ProgressForm.Step = ProgressForm.CalculateStep(FilesToProcess.Length);

            foreach (string SingleFile in FilesToProcess)
            {
                ProgressForm.UpdateProcessingName = SingleFile;

                //Get the Directory including the file
                string FileDirectory = System.IO.Directory.GetParent(SingleFile).FullName;

                //Get the filename without the path
                string Filename = SingleFile.Replace(FileDirectory + "\\", "");
                string Searchname = Filename;
                //Strip and Save the extension
                Regex FileExtensionRegex = new Regex("\\.(...)$");
                string Extension = FileExtensionRegex.Match(Searchname).Value;
                Searchname = FileExtensionRegex.Replace(Searchname, "");

                //Replace "SpaceChar" with actual spaces for our search
                Searchname = Searchname.Replace(SpaceChar, " ".ToCharArray()[0]);
                LogFacility.WriteToFile(4, "Title to search: " + Searchname, LogSource);

                //Initiate scraper
                Scraper.TheGamesDB InfoScraper = new Scraper.TheGamesDB(LogFacility);
                List<Game> Results = InfoScraper.Search(Searchname, "Nintendo 64");

                //We will only take the first result, let's hope that it's the best one...
                if (Results != null)
                    DBConnection.InsertGame(InfoScraper.GetGameByID(Results[0].scraper_gdb_id));
                else
                    LogFacility.WriteToFile(1, "Could not find an item for " + Searchname, LogSource);

                if (Results != null)
                {
                    Filename = Results[0].Name.Replace(" ".ToCharArray()[0], SpaceChar);
                    Filename += Extension;

                    LogFacility.WriteToFile(4, "Resulted Filename: " + FileDirectory + "\\" + Filename, LogSource);

                    if (RenamingEnabled && !MovingEnabled)
                        System.IO.File.Move(SingleFile, FileDirectory + "\\" + Filename);

                    if (MovingEnabled)
                        System.IO.File.Move(SingleFile, FileDirectory + "\\" + Filename);
                }

                ProgressForm.UpdateProgressBar();
            }

            ProgressForm.Close();
        }