- MySQL Connection with Visual C# Express 2008
- String connString = "SERVER = localhost; DATABASE = request; User ID = root; ID =; UserName =; Date =; Type =; Rules =;";
- MySqlConnection mcon = new MySqlConnection(connString);
- String command = "SELECT * FROM requesttcw";
- MySqlCommand cmd = new MySqlCommand(command, mcon);
- MySqlDataReader reader;
- try
- {
- mcon.Open();
- cmd.ExecuteNonQuery();
- reader = cmd.ExecuteReader();
- cmd.CommandType = System.Data.CommandType.Text;
- while (reader.Read() != false)
- {
- Console.WriteLine(reader["ID"]);
- Console.WriteLine(reader["ClanName"]);
- Console.WriteLine(reader["Date"]);
- Console.WriteLine(reader["Type"]);
- Console.WriteLine(reader["Rules"]);
- }
- Console.ReadLine();
- }
- catch (Exception)
- {
- MessageBox.Show("ERROR: There was an error trying to connect to the DB!");
- return;
- }
- cmd.CommandText = "INSERT INTO requesttcw (ClanName, Date, Type, Rules) VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "', '" + textBox3.Text + "', '" + richTextBox1.Text + "' LIMIT 1)";
- try
- {
- cmd.ExecuteNonQuery();
- MessageBox.Show("You're Request Has Been Posted!");
- }
- catch (Exception ex)
- {
- string message = ("ERROR: There was an error submitting your form!" + ex + "");
- DialogResult result = MessageBox.Show(message, "ERROR", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
- switch (result)
- {
- case DialogResult.Retry:
- Application.Restart();
- break;
- case DialogResult.Cancel:
- this.Close();
- break;
- }
- }
- string connString = "Server=localhost;Database=request;Uid=root;Pwd=;";
- string connString = "Server=localhost;Database=request;Uid=root;Pwd=;";
- using (MySqlConnection mcon = new MySqlConnection(connString))
- using (MySqlCommand cmd = mcon.CreateCommand())
- {
- mcon.Open();
- cmd.CommandText = "SELECT * FROM requesttcw";
- using (MySqlDataReader reader = cmd.ExecuteReader())
- {
- while (reader.Read())
- {
- Console.WriteLine(reader["ID"]);
- Console.WriteLine(reader["ClanName"]);
- Console.WriteLine(reader["Date"]);
- Console.WriteLine(reader["Type"]);
- Console.WriteLine(reader["Rules"]);
- }
- }
- }
- using (MySqlConnection mcon = new MySqlConnection(connString))
- using (MySqlCommand cmd = mcon.CreateCommand())
- {
- mcon.Open();
- cmd.CommandText = "INSERT INTO requesttcw (ClanName, Date, Type, Rules) VALUES (@ClanName, @Date, @Type, @Rules)";
- cmd.Parameters.AddWithValue("@ClanName", textBox1.Text);
- // Warning if the Date column in your database is of type DateTime
- // you need to parse the value first, like this:
- // cmd.Parameters.AddWithValue("@Date", Date.Parse(textBox2.Text));
- cmd.Parameters.AddWithValue("@Date", textBox2.Text);
- cmd.Parameters.AddWithValue("@Type", textBox3.Text);
- cmd.Parameters.AddWithValue("@Rules", richTextBox1.Text);
- cmd.ExecuteNonQuery();
- }
- Server=localhost;Database=request;Uid=myUsername;Pwd=myPassword;