Guest User

Untitled

a guest
May 10th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MySql.Data.MySqlClient;
  7.  
  8. namespace databaseTest
  9. {
  10. class Program
  11. {
  12. private MySqlConnection connection; //connection property
  13. private string server;
  14. private string database;
  15. private string uid;
  16. private string password;
  17.  
  18. static void Main(string[] args)
  19. {
  20.  
  21. while (true)
  22. {
  23. Console.Clear();
  24. Program myProg = new Program();
  25. myProg.Initialize();
  26. Console.WriteLine("Choose item;");
  27. Console.WriteLine("<A> Add Record");
  28. Console.WriteLine("<B> Delete Record");
  29. Console.WriteLine("<C> Update Record");
  30. Console.WriteLine("<D> Show Record");
  31. Console.WriteLine("<E> Count Records");
  32. Console.WriteLine("<F> Search Records");
  33. Console.WriteLine("<X> Exit");
  34. ConsoleKeyInfo myKey = Console.ReadKey();
  35. if (myKey.Key == ConsoleKey.A)
  36. {
  37. myProg.Insert();
  38. }
  39. }
  40. }
  41.  
  42. private void Initialize()
  43. {
  44. server = "localhost"; //local host (WAMP)
  45. database = "my_db"; //database name
  46. uid = "root"; //database username
  47. password = ""; //database password
  48. string connectionString;
  49. connectionString = "SERVER=" + server + ";" + "DATABASE=" +
  50. database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
  51. connection = new MySqlConnection(connectionString);
  52. }
  53.  
  54. private bool OpenConnection()
  55. {
  56. try
  57. {
  58. connection.Open();
  59. return true;
  60. }
  61. catch (MySqlException ex)
  62. {
  63. //When handling errors, you can your application's response based
  64. //on the error number.
  65. //The two most common error numbers when connecting are as follows:
  66. //0: Cannot connect to server.
  67. //1045: Invalid user name and/or password.
  68. switch (ex.Number)
  69. {
  70. case 0:
  71. Console.WriteLine("Cannot connect to server. Contact administrator");
  72. break;
  73.  
  74. case 1045:
  75. Console.WriteLine("Invalid username/password, please try again");
  76. break;
  77. }
  78. return false;
  79. }
  80. }
  81.  
  82. private bool CloseConnection()
  83. {
  84. try
  85. {
  86. connection.Close();
  87. return true;
  88. }
  89. catch (MySqlException ex)
  90. {
  91. Console.WriteLine(ex.Message);
  92. return false;
  93. }
  94. }
  95.  
  96. public void Insert()
  97. {
  98. List<databaseOperations.User> user = new List<databaseOperations.User>();
  99. databaseOperations.User x = new databaseOperations.User();
  100. Console.Clear();
  101. Console.WriteLine("---> Insert Recordn");
  102. Console.WriteLine("Enter numeric ID: (***)");
  103. int ID = int.Parse(Console.ReadLine());
  104. Console.WriteLine("Enter first name:");
  105. String firstName = Console.ReadLine();
  106. Console.WriteLine("Enter last name:");
  107. String lastName = Console.ReadLine();
  108. Console.WriteLine("Enter telephone:");
  109. String telephone = Console.ReadLine();
  110. x.ID = ID;
  111. x.firstName = firstName;
  112. x.lastName = lastName;
  113. x.telephone = telephone;
  114. user.Add(x);
  115. databaseOperations.InsertToDatabase(x);
  116. }
  117. }
  118. }
  119.  
  120. using System;
  121. using System.Collections.Generic;
  122. using System.Linq;
  123. using System.Text;
  124. using System.Threading.Tasks;
  125. using MySql.Data.MySqlClient;
  126.  
  127. namespace databaseTest
  128. {
  129. class databaseOperations
  130. {
  131. private MySqlConnection connection; //connection property
  132. private string server;
  133. private string database;
  134. private string uid;
  135. private string password;
  136.  
  137. public class User
  138. {
  139. public int ID { get; set; }
  140.  
  141. public string firstName { get; set; }
  142.  
  143. public string lastName { get; set; }
  144.  
  145. public string telephone { get; set; }
  146. }
  147.  
  148. private void Initialize()
  149. {
  150. server = "localhost"; //local host (WAMP)
  151. database = "my_db"; //database name
  152. uid = "root"; //database username
  153. password = ""; //database password
  154. string connectionString;
  155. connectionString = "SERVER=" + server + ";" + "DATABASE=" +
  156. database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
  157. connection = new MySqlConnection(connectionString);
  158. }
  159.  
  160. private bool OpenConnection()
  161. {
  162. try
  163. {
  164. connection.Open();
  165. return true;
  166. }
  167. catch (MySqlException ex)
  168. {
  169. //When handling errors, you can your application's response based
  170. //on the error number.
  171. //The two most common error numbers when connecting are as follows:
  172. //0: Cannot connect to server.
  173. //1045: Invalid user name and/or password.
  174. switch (ex.Number)
  175. {
  176. case 0:
  177. Console.WriteLine("Cannot connect to server. Contact administrator");
  178. break;
  179.  
  180. case 1045:
  181. Console.WriteLine("Invalid username/password, please try again");
  182. break;
  183. }
  184. return false;
  185. }
  186. }
  187.  
  188. private bool CloseConnection()
  189. {
  190. try
  191. {
  192. connection.Close();
  193. return true;
  194. }
  195. catch (MySqlException ex)
  196. {
  197. Console.WriteLine(ex.Message);
  198. return false;
  199. }
  200. }
  201. public void InsertToDatabase(int ID, string firstName, string lastName, string telephone)
  202. {
  203. string query = "INSERT INTO tbl_user (ID,First_Name,Last_Name,Telephone) VALUES('" + ID + "', '" + firstName + "','" + lastName + "','" + telephone + "')";
  204. if (this.OpenConnection() == true)
  205. {
  206. MySqlCommand cmd = new MySqlCommand(query, connection);
  207. cmd.ExecuteNonQuery();
  208. this.CloseConnection();
  209. Console.WriteLine("n -->Record Added - Press enter to continue...");
  210. Console.ReadLine();
  211. }
  212. }
  213. }
  214. }
  215.  
  216. databaseOperations.InsertToDatabase(ID, firstName, lastName, telephone);
  217.  
  218. DatabaseOperations databaseOperations = new DatabaseOperations();
  219. databaseOperations.InsertToDatabase(x.ID, x.firstName, x.lastName, x.telephone);
  220.  
  221. class DatabaseOperations
  222. {
  223. public DatabaseOperations()
  224. {
  225. Initialize();
  226. }
  227.  
  228. // etc.
  229. }
Add Comment
Please, Sign In to add comment