code_junkie

Connect to remote MySQL database with Visual C#

Nov 13th, 2011
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MySql.Data.MySqlClient;
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9.  
  10. {
  11.  
  12.  
  13.  
  14.  
  15. static void Main(string[] args)
  16. {
  17. Console.WriteLine("Welcome ...!");
  18. String conString = "SERVER = localhost; DATABASE = l2emu; User ID = root; PASSWORD = password;";
  19.  
  20. MySqlConnection connection = new MySqlConnection(conString);
  21.  
  22. String command = "SELECT * FROM characters";
  23. MySqlCommand cmd = new MySqlCommand(command,connection);
  24. MySqlDataReader reader;
  25. try
  26. {
  27. connection.Open();
  28. cmd.ExecuteNonQuery();
  29. reader = cmd.ExecuteReader();
  30. cmd.CommandType = System.Data.CommandType.Text;
  31. while (reader.Read() != false)
  32. {
  33.  
  34. Console.WriteLine(reader["char_name"]);
  35. Console.WriteLine(reader["level"]);
  36.  
  37. }
  38.  
  39. Console.ReadLine();
  40.  
  41. }
  42. catch (MySqlException MySqlError)
  43. {
  44. Console.WriteLine(MySqlError.Message);
  45. }
  46.  
  47. }
  48. }
  49. }
Add Comment
Please, Sign In to add comment