Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. using MySql.Data;
  2. using MySql.Data.MySqlClient;
  3.  
  4. namespace Data
  5. {
  6. public class DBConnection
  7. {
  8. private DBConnection()
  9. {
  10. }
  11.  
  12. private string databaseName = string.Empty;
  13. public string DatabaseName
  14. {
  15. get { return databaseName; }
  16. set { databaseName = value; }
  17. }
  18.  
  19. public string Password { get; set; }
  20. private MySqlConnection connection = null;
  21. public MySqlConnection Connection
  22. {
  23. get { return connection; }
  24. }
  25.  
  26. private static DBConnection _instance = null;
  27. public static DBConnection Instance()
  28. {
  29. if (_instance == null)
  30. _instance = new DBConnection();
  31. return _instance;
  32. }
  33.  
  34. public bool IsConnect()
  35. {
  36. bool result = true;
  37. if (Connection == null)
  38. {
  39. if (String.IsNullOrEmpty(databaseName))
  40. result = false;
  41. string connstring = string.Format("Server=localhost; database={0}; UID=UserName; password=your password", databaseName);
  42. connection = new MySqlConnection(connstring);
  43. connection.Open();
  44. result = true;
  45. }
  46.  
  47. return result;
  48. }
  49.  
  50. public void Close()
  51. {
  52. connection.Close();
  53. }
  54. }
  55. }
  56.  
  57. var dbCon = DBConnection.Instance();
  58. dbCon.DatabaseName = "YourDatabase";
  59. if (dbCon.IsConnect())
  60. {
  61. //suppose col0 and col1 are defined as VARCHAR in the DB
  62. string query = "SELECT col0,col1 FROM YourTable";
  63. var cmd = new MySqlCommand(query, dbCon.Connection);
  64. var reader = cmd.ExecuteReader();
  65. while(reader.Read())
  66. {
  67. string someStringFromColumnZero = reader.GetString(0);
  68. string someStringFromColumnOne = reader.GetString(1);
  69. Console.WriteLine(someStringFromColumnZero + "," + someStringFromColumnOne);
  70. }
  71. }
  72.  
  73. PM> Install-Package MySql.Data
  74.  
  75. private void Initialize()
  76. {
  77. server = "localhost";
  78. database = "connectcsharptomysql";
  79. uid = "username";
  80. password = "password";
  81. string connectionString;
  82. connectionString = "SERVER=" + server + ";" + "DATABASE=" +
  83. database + ";" + "U`enter code here`ID=" + uid + ";" + "PASSWORD=" + password + ";";
  84.  
  85. connection = new MySqlConnection(connectionString);
  86. }
  87.  
  88. protected readonly static string Server = "server";
  89. protected readonly static string UserName = "username";
  90. protected readonly static string Password = "password";
  91. protected readonly static string Database = "database";
  92. protected readonly static string Port = "port";
  93.  
  94.  
  95. public readonly string config = string.Format("Server = {0}; Port = {1}; Database = {2}; Uid = {3}; Pwd = {4}; pooling = true; Allow Zero Datetime = False; Min Pool Size = 0; Max Pool Size = 200; ", Server, Port, Database, UserName, Password);
  96.  
  97. using (var con = new MySqlConnection { ConnectionString = config })
  98. {
  99. con.Open();
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement