Part I
In order to connect to on MySQL method, I propose this more flexible solution, thus, it enables us to customize the connection parameters in one hand, moreover, it enables us to choose which model should we use. I mean, ADO connected mode using data reader or disconnected mode using data adapter and data set.
Walkthrough
Remarque: Of course, I suppose that MySQL server is installed in your machine, a database already exists, and all information and permissions to use the given database are ready.
Here is a class that helps you connect and deal with your MySQL database:
  1. using System;
  2. using System.Text;
  3. using System.Data;
  4. using System.Data.Odbc;
  5. namespace MySqlProj {
  6. /* The class implements IDisposable interface
  7. * inorder to close the connection once the class instance
  8. is disposed*/
  9. public class ODBCClass: IDisposable {
  10. //This is the password private field
  11. private string _Password;
  12. //The server name
  13. public string Server { get;
  14. set; }
  15. //The port number
  16. public string Port { get;
  17. set; }
  18. //The data base name
  19. public string DataBaseName { get;
  20. set; }
  21. //The user name
  22. public string UserID { get;
  23. set; }
  24. //The password is only set for security issues
  25. public string Password {
  26. set { _Password = value; }
  27. }
  28. //Set a query
  29. public string Query { get;
  30. set; }
  31. //Define a private connection
  32. private OdbcConnection myConnection;
  33. //Define a command
  34. OdbcCommand myCommand;
  35. /// <summary>
  36. /// This is the constructor
  37. /// </summary>
  38. /// <param name="Server">string: The server name</param>
  39. /// <param name="Port">string: The port number</param>
  40. /// <param name="DataBaseName">string: The data base name</param>
  41. /// <param name="UserID">string: The user name</param>
  42. /// <param name="Password">string: The password</param>
  43. public ODBCClass(string Server, string Port, string DataBaseName, string UserID, string Password, string Query) {
  44. this.Server = Server;
  45. this.Port = Port;
  46. this.DataBaseName = DataBaseName;
  47. this.UserID = UserID;
  48. this.Password = Password;
  49. this.Query = Query;
  50. myConnection = new OdbcConnection();
  51. myConnection.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" + Server + "; PORT=" + Port + ";DATABASE= " + DataBaseName + ";UID= " + UserID + ";PWD=" + Password;
  52. try {
  53. //Open the connection
  54. myConnection.Open();
  55. //Notify the user that the connection is opened
  56. Console.WriteLine("Connected to the data base");
  57. //Create a new command object
  58. myCommand = new OdbcCommand(Query, myConnection);
  59. /* CommandBehavior.CloseConnection option forces the connection to close if
  60. somethig id wrong*/
  61. } catch (OdbcException caught) {
  62. //TO DO Deal with the exception
  63. } catch (InvalidOperationException caught) {
  64. //TO DO Deal with the exception
  65. }
  66. }
  67. /// <summary>
  68. /// OdbcCommand : This method returns a command object
  69. /// </summary>
  70. /// <param name="Query">string: This is the sql query</param>
  71. /// <returns>returns an OdbcCommand</returns>
  72. /// <summary>
  73. /// void: It is used to close the connection if you work within disconnected
  74. /// mode
  75. /// </summary>
  76. public void CloseConnection() {
  77. myConnection.Close();
  78. }
  79. public OdbcCommand GetOdbcCommand() {
  80. //Returns a command object
  81. return myCommand;
  82. }
  83. //When the object is disposed the connection is closed
  84. public void Dispose() {
  85. myConnection.Close();
  86. }
  87. }
  88. }
Now, open a new Project>Console application and name it as you like, create a new empty class and name it ODBCClass, then copy and paste the above class in the code editor.
Once this is done you can choose either to work within a connected mode, if you do so then implement the main method as follows:
  1. using System.Data.Odbc;
  2. namespace MySqlProj {
  3. class Program {
  4. static void Main(string[] args) {
  5. using(ODBCClass o = new ODBCClass("localhost", "3306", "database", "me", "me", "select * from user")) {
  6. OdbcCommand comm = o.GetOdbcCommand("Select * from user");
  7. OdbcDataReader oReader = comm.ExecuteReader();
  8. while (oReader.Read()) { Console.WriteLine(oReader[0] + " " + oReader[1]); }
  9. Console.Read();
  10. }
  11. }
  12. }
  13. }
If you want to do the same thing but in disconnected mode then implement the Main method as follows:
  1. using System;
  2. using System.Text;
  3. using System.Data;
  4. using System.Data.Odbc;
  5. namespace MySqlProj {
  6. class Program {
  7. static void Main(string[] args) {
  8. using(ODBCClass o = new ODBCClass("localhost", "3306", "database", "me", "me")) {
  9. OdbcCommand comm = o.GetOdbcCommand("Select * from user");
  10. OdbcDataAdapter oAdapter = new OdbcDataAdapter(comm);
  11. DataSet Ds = new DataSet();
  12. oAdapter.Fill(Ds);
  13. Console.WriteLine("Data set is filled you can make use of it now");
  14. //TO DO Make use of the populated data set
  15. Console.Read();
  16. }
  17. }
  18. }
  19. }
That's it
God dotneting!!!