private static void GetConnectionInfo(ref DBConnectStruct dbc) { Console.Write("Post [localhost]: "); if (String.IsNullOrWhiteSpace((dbc.sHost = Console.ReadLine()))) { dbc.sHost = "localhost"; } Console.Write("Port [3306]:"); if (Int32.TryParse(Console.ReadLine(), out dbc.nPort) == false) { dbc.nPort = 3306; } Console.Write("Database [perforce_web]: "); if (String.IsNullOrWhiteSpace((dbc.sDB = Console.ReadLine()))) { dbc.sDB = "perforce_web"; } Console.Write("User [perforce]: "); if (String.IsNullOrWhiteSpace((dbc.sUser = Console.ReadLine()))) { dbc.sUser = "******"; } Console.Write("Password [perforce]: "); if (String.IsNullOrWhiteSpace((dbc.sPasswd = ReadPassword()))) { dbc.sPasswd = "perforce"; } }
private static MySqlConnection TryDBConnect(DBConnectStruct dbc, out string sErreur) { sErreur = ""; MySqlConnection _conn = new MySqlConnection("User Id=" + dbc.sUser + ";Password="******";Host=" + dbc.sHost + ";Database=" + dbc.sDB + ";Port=" + dbc.nPort); try { _conn.Open(); } catch (MySqlException mEX) { sErreur = mEX.Message; _conn = null; } //Console.WriteLine("SUCCESS"); return(_conn); }
private static void Main(string[] args) { DBConnectStruct dbConnection = new DBConnectStruct(); P4ConnectStruct p4Connection = new P4ConnectStruct(); MySqlConnection _conn; string sSelect, sP4PwdFile, sConfigFile, sErreur, p4ClientExecutable; sSelect = sP4PwdFile = sConfigFile = p4ClientExecutable = ""; ArrayList alResultats; if (ParseCommandLine(args, ref sSelect, ref sConfigFile, ref sP4PwdFile) == false) { ShowHelp(); Environment.Exit(0); } dbConnection.nPort = 3306; //default value, in case the parameter is not in the config file #region MySQL connection config if (ReadConfigFile(sConfigFile, ref p4ClientExecutable, ref dbConnection, ref p4Connection) == false) { ConsoleKeyInfo cki; Console.WriteLine("{0} cannot continue as it needs a valid MySQL connection config file", Assembly.GetExecutingAssembly().GetName().Name); Console.Write("Press (1) to manually input the pertinent info, (2) to abort ? [1/2]"); do { cki = Console.ReadKey(true); } while (cki.Key != ConsoleKey.D1 && cki.Key != ConsoleKey.D2); if (cki.Key == ConsoleKey.D1) { GetConnectionInfo(ref dbConnection); } else { Environment.Exit(0); } } #endregion MySQL connection config if (String.IsNullOrWhiteSpace(sP4PwdFile) == false && File.Exists(sP4PwdFile)) { ReadPasswordFile(sP4PwdFile, ref p4Connection); } _conn = new MySqlConnection("User Id=" + dbConnection.sUser + ";Password="******";Host=" + dbConnection.sHost + ";Database=" + dbConnection.sDB + ";Port=" + dbConnection.nPort); if ((_conn = TryDBConnect(dbConnection, out sErreur)) == null) { Console.WriteLine("Connection Error : {0}", sErreur); Environment.Exit(0); } if (GoFetchData(_conn, sSelect, ref p4Connection, out sErreur, out alResultats) == false) { Console.WriteLine("Error fetching data : {0}", sErreur); Environment.Exit(0); } if (alResultats.Count == 0) { Console.WriteLine("No instance match the given criterias. Exiting."); Environment.Exit(0); } WriteP4Config(p4ClientExecutable, SelectCorrectInstance(_conn, alResultats)); Console.WriteLine("Config file created / config applied."); _conn.Close(); }
private static bool ReadConfigFile(string cfgFile, ref string p4ClientExecutable, ref DBConnectStruct dbc, ref P4ConnectStruct p4c) { string s, ligne; if (File.Exists(cfgFile) == false) { return(false); } var sr = new StreamReader(cfgFile); dbc.nPort = 3306; while ((ligne = sr.ReadLine()) != null) { switch (ligne.ToLower()) { case "[p4user]": if ((p4c.P4USER = sr.ReadLine()) == null) { return(false); } break; case "[p4clientexec]": if ((p4ClientExecutable = sr.ReadLine()) == null) { return(false); } break; case "[serveur]": case "[server]": case "[host]": if ((dbc.sHost = sr.ReadLine()) == null) { return(false); } break; case "[db]": case "[bd]": case "[database]": if ((dbc.sDB = sr.ReadLine()) == null) { return(false); } break; case "[port]": if ((s = sr.ReadLine()) == null) { return(false); } if (Int32.TryParse(s, out dbc.nPort) == false) { dbc.nPort = 3306; } break; case "[user]": if ((dbc.sUser = sr.ReadLine()) == null) { return(false); } break; case "[passwd]": case "[password]": if ((dbc.sPasswd = sr.ReadLine()) == null) { return(false); } break; } } if (sr != null) { sr.Close(); } return(!String.IsNullOrWhiteSpace(dbc.sHost) && !String.IsNullOrWhiteSpace(dbc.sDB) && !String.IsNullOrWhiteSpace(dbc.sUser) && !String.IsNullOrWhiteSpace(dbc.sPasswd)); }