I'm trying to make a database browser in a C# console application
.
Now I have a piece of code that checks the login data for the MySql Server
, but it is wrapped in a while loop.
That means the variable basicConn
inside the while loop is inaccessible, so I can't use it outside the loop.
Now I declared that variable before the loop and filled inside the loop, but after the loop I want to use the connection but I get the error: " Use of unassigned local variable `basicConn' ".
Here is my piece of code:
//Check voor het inloggen om uit de whileLoop te komen; bool check1 = false; //Er mogen maximaal 3 foute login acites gemaakt worden; int fails = 0;
MySqlConnection basicConn;
Console.WriteLine ("Log hier in om verbinding te maken met de MySql server.\n");
//Blijf alles wat in deze whileLoop staat herhalen tot het goed is gegaan;
while (check1 == false)
{
//Als er 3 fouten zijn gemaakt met inloggen wordt het programma gesloten;
if (fails < 3 && fails >= 0)
{
Console.WriteLine ("Poging {0}", fails + 1);
//Hostnaam;
string host = "";
Console.Write ("Hostname: ");
host = Console.ReadLine ();
//Gebruikersnaam;
string gebruikersnaam = "";
Console.Write ("Gebruikersnaam: ");
gebruikersnaam = Console.ReadLine ();
//Wachtwoord;
string wachtwoord = "";
Console.Write ("Wachtwoord: ");
wachtwoord = Console.ReadLine ();
//MySql Connectie basis;
string connString = "Server=" + host + ";Uid=" + gebruikersnaam + ";Password=" + wachtwoord + ";";
basicConn = new MySqlConnection (connString);
try
{
//MySQl Connectie maken;
basicConn.Open ();
Console.WriteLine ("Connectie is gemaatk!");
check1 = true;
}
catch (Exception)
{
Console.WriteLine ("Er is iets misgegaan met een connectie maken, probeer het opnieuw.\n\n");
check1 = false;
fails += 1;
}
}
else
{
Console.WriteLine ("Je aantal pogingen zijn mislukt!\nHet programma wordt gesloten.");
//Laat het programma 1.5 seconden wachten tot het verdergaat;
Thread.Sleep (1500);
//Laat het programma sluiten;
Environment.Exit (1);
}
}
// ===============================================================================================================================
MySqlCommand db = basicConn.CreateCommand ();
db.CommandText = "SHOW DATABASES";
MySqlDataReader Read_db;
Read_db = db.ExecuteReader ();
while (Read_db.Read()) {
string row = "";
for (int i = 0; i < Read_db.FieldCount; i++)
row += Read_db.GetValue (i).ToString ();
Console.WriteLine (row);
}
Read_db.Close ();