Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 3.36 KB  |  hits: 31  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. MySQL Connection with Visual C# Express 2008
  2. String connString = "SERVER = localhost; DATABASE = request; User ID = root; ID =; UserName =; Date =; Type =; Rules =;";
  3.         MySqlConnection mcon = new MySqlConnection(connString);
  4.         String command = "SELECT * FROM requesttcw";
  5.         MySqlCommand cmd = new MySqlCommand(command, mcon);
  6.         MySqlDataReader reader;
  7.  
  8.         try
  9.         {
  10.             mcon.Open();
  11.             cmd.ExecuteNonQuery();
  12.             reader = cmd.ExecuteReader();
  13.             cmd.CommandType = System.Data.CommandType.Text;
  14.             while (reader.Read() != false)
  15.             {
  16.                 Console.WriteLine(reader["ID"]);
  17.                 Console.WriteLine(reader["ClanName"]);
  18.                 Console.WriteLine(reader["Date"]);
  19.                 Console.WriteLine(reader["Type"]);
  20.                 Console.WriteLine(reader["Rules"]);
  21.  
  22.             }
  23.  
  24.             Console.ReadLine();
  25.  
  26.         }
  27.         catch (Exception)
  28.         {
  29.             MessageBox.Show("ERROR: There was an error trying to connect to the DB!");
  30.             return;
  31.         }
  32.         cmd.CommandText = "INSERT INTO requesttcw (ClanName, Date, Type, Rules) VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "', '" + textBox3.Text + "', '" + richTextBox1.Text + "' LIMIT 1)";
  33.  
  34.         try
  35.         {
  36.             cmd.ExecuteNonQuery();
  37.             MessageBox.Show("You're Request Has Been Posted!");
  38.         }
  39.         catch (Exception ex)
  40.         {
  41.             string message = ("ERROR: There was an error submitting your form!" + ex + "");
  42.             DialogResult result = MessageBox.Show(message, "ERROR", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
  43.  
  44.             switch (result)
  45.             {
  46.                 case DialogResult.Retry:
  47.                     Application.Restart();
  48.                     break;
  49.                 case DialogResult.Cancel:
  50.                     this.Close();
  51.                     break;
  52.             }
  53.         }
  54.        
  55. string connString = "Server=localhost;Database=request;Uid=root;Pwd=;";
  56.        
  57. string connString = "Server=localhost;Database=request;Uid=root;Pwd=;";
  58.  
  59. using (MySqlConnection mcon = new MySqlConnection(connString))
  60. using (MySqlCommand cmd = mcon.CreateCommand())
  61. {
  62.     mcon.Open();
  63.     cmd.CommandText = "SELECT * FROM requesttcw";
  64.     using (MySqlDataReader reader = cmd.ExecuteReader())
  65.     {
  66.         while (reader.Read())
  67.         {
  68.             Console.WriteLine(reader["ID"]);
  69.             Console.WriteLine(reader["ClanName"]);
  70.             Console.WriteLine(reader["Date"]);
  71.             Console.WriteLine(reader["Type"]);
  72.             Console.WriteLine(reader["Rules"]);
  73.         }
  74.     }
  75. }
  76.  
  77. using (MySqlConnection mcon = new MySqlConnection(connString))
  78. using (MySqlCommand cmd = mcon.CreateCommand())
  79. {
  80.     mcon.Open();
  81.     cmd.CommandText = "INSERT INTO requesttcw (ClanName, Date, Type, Rules) VALUES (@ClanName, @Date, @Type, @Rules)";
  82.     cmd.Parameters.AddWithValue("@ClanName", textBox1.Text);
  83.  
  84.     // Warning if the Date column in your database is of type DateTime
  85.     // you need to parse the value first, like this:
  86.     // cmd.Parameters.AddWithValue("@Date", Date.Parse(textBox2.Text));
  87.     cmd.Parameters.AddWithValue("@Date", textBox2.Text);
  88.     cmd.Parameters.AddWithValue("@Type", textBox3.Text);
  89.     cmd.Parameters.AddWithValue("@Rules", richTextBox1.Text);
  90.  
  91.     cmd.ExecuteNonQuery();
  92. }
  93.        
  94. Server=localhost;Database=request;Uid=myUsername;Pwd=myPassword;