Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MySql.Data.MySqlClient;
- namespace databaseTest
- {
- class Program
- {
- private MySqlConnection connection; //connection property
- private string server;
- private string database;
- private string uid;
- private string password;
- static void Main(string[] args)
- {
- while (true)
- {
- Console.Clear();
- Program myProg = new Program();
- myProg.Initialize();
- Console.WriteLine("Choose item;");
- Console.WriteLine("<A> Add Record");
- Console.WriteLine("<B> Delete Record");
- Console.WriteLine("<C> Update Record");
- Console.WriteLine("<D> Show Record");
- Console.WriteLine("<E> Count Records");
- Console.WriteLine("<F> Search Records");
- Console.WriteLine("<X> Exit");
- ConsoleKeyInfo myKey = Console.ReadKey();
- if (myKey.Key == ConsoleKey.A)
- {
- myProg.Insert();
- }
- }
- }
- private void Initialize()
- {
- server = "localhost"; //local host (WAMP)
- database = "my_db"; //database name
- uid = "root"; //database username
- password = ""; //database password
- string connectionString;
- connectionString = "SERVER=" + server + ";" + "DATABASE=" +
- database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
- connection = new MySqlConnection(connectionString);
- }
- private bool OpenConnection()
- {
- try
- {
- connection.Open();
- return true;
- }
- catch (MySqlException ex)
- {
- //When handling errors, you can your application's response based
- //on the error number.
- //The two most common error numbers when connecting are as follows:
- //0: Cannot connect to server.
- //1045: Invalid user name and/or password.
- switch (ex.Number)
- {
- case 0:
- Console.WriteLine("Cannot connect to server. Contact administrator");
- break;
- case 1045:
- Console.WriteLine("Invalid username/password, please try again");
- break;
- }
- return false;
- }
- }
- private bool CloseConnection()
- {
- try
- {
- connection.Close();
- return true;
- }
- catch (MySqlException ex)
- {
- Console.WriteLine(ex.Message);
- return false;
- }
- }
- public void Insert()
- {
- List<databaseOperations.User> user = new List<databaseOperations.User>();
- databaseOperations.User x = new databaseOperations.User();
- Console.Clear();
- Console.WriteLine("---> Insert Recordn");
- Console.WriteLine("Enter numeric ID: (***)");
- int ID = int.Parse(Console.ReadLine());
- Console.WriteLine("Enter first name:");
- String firstName = Console.ReadLine();
- Console.WriteLine("Enter last name:");
- String lastName = Console.ReadLine();
- Console.WriteLine("Enter telephone:");
- String telephone = Console.ReadLine();
- x.ID = ID;
- x.firstName = firstName;
- x.lastName = lastName;
- x.telephone = telephone;
- user.Add(x);
- databaseOperations.InsertToDatabase(x);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MySql.Data.MySqlClient;
- namespace databaseTest
- {
- class databaseOperations
- {
- private MySqlConnection connection; //connection property
- private string server;
- private string database;
- private string uid;
- private string password;
- public class User
- {
- public int ID { get; set; }
- public string firstName { get; set; }
- public string lastName { get; set; }
- public string telephone { get; set; }
- }
- private void Initialize()
- {
- server = "localhost"; //local host (WAMP)
- database = "my_db"; //database name
- uid = "root"; //database username
- password = ""; //database password
- string connectionString;
- connectionString = "SERVER=" + server + ";" + "DATABASE=" +
- database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
- connection = new MySqlConnection(connectionString);
- }
- private bool OpenConnection()
- {
- try
- {
- connection.Open();
- return true;
- }
- catch (MySqlException ex)
- {
- //When handling errors, you can your application's response based
- //on the error number.
- //The two most common error numbers when connecting are as follows:
- //0: Cannot connect to server.
- //1045: Invalid user name and/or password.
- switch (ex.Number)
- {
- case 0:
- Console.WriteLine("Cannot connect to server. Contact administrator");
- break;
- case 1045:
- Console.WriteLine("Invalid username/password, please try again");
- break;
- }
- return false;
- }
- }
- private bool CloseConnection()
- {
- try
- {
- connection.Close();
- return true;
- }
- catch (MySqlException ex)
- {
- Console.WriteLine(ex.Message);
- return false;
- }
- }
- public void InsertToDatabase(int ID, string firstName, string lastName, string telephone)
- {
- string query = "INSERT INTO tbl_user (ID,First_Name,Last_Name,Telephone) VALUES('" + ID + "', '" + firstName + "','" + lastName + "','" + telephone + "')";
- if (this.OpenConnection() == true)
- {
- MySqlCommand cmd = new MySqlCommand(query, connection);
- cmd.ExecuteNonQuery();
- this.CloseConnection();
- Console.WriteLine("n -->Record Added - Press enter to continue...");
- Console.ReadLine();
- }
- }
- }
- }
- databaseOperations.InsertToDatabase(ID, firstName, lastName, telephone);
- DatabaseOperations databaseOperations = new DatabaseOperations();
- databaseOperations.InsertToDatabase(x.ID, x.firstName, x.lastName, x.telephone);
- class DatabaseOperations
- {
- public DatabaseOperations()
- {
- Initialize();
- }
- // etc.
- }
Add Comment
Please, Sign In to add comment