Advertisement
vlad0

Untitled

Jul 16th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.28 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 _09.MySQLConnect
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             //change the Uid and Pwd with your username and password at your local MySQL server
  15.             string connectionString = "Server = localhost; Port = 3306; Database = world; Uid = root; Pwd = rootroot; pooling = true;";
  16.             MySqlConnection dbConnection = new MySqlConnection(connectionString);
  17.             dbConnection.Open();
  18.  
  19.             using (dbConnection)
  20.             {
  21.                 #region Checking for existing table 'Books' in database 'World'
  22.                 MySqlCommand checkExistingTable = new MySqlCommand(
  23. @"SELECT 1
  24. FROM INFORMATION_SCHEMA.TABLES
  25. WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME='Books'", dbConnection);
  26.  
  27.                 int existing = Convert.ToInt32(checkExistingTable.ExecuteScalar());
  28.                 if (existing != 1)
  29.                 {
  30.                     MySqlCommand createTable = new MySqlCommand(
  31.  @"CREATE TABLE Books
  32. (
  33.     BookID int PRIMARY KEY AUTO_INCREMENT,
  34.     Title nvarchar(100) NOT NULL,
  35.     Author nvarchar(50) NOT NULL,
  36.     PublishDate date NOT NULL,
  37.     ISBN nvarchar(50) NOT NULL
  38.  
  39. )", dbConnection);
  40.                     Console.WriteLine("Created table 'Book' ", createTable.ExecuteNonQuery());
  41.                 }
  42.  
  43.                 #endregion
  44.  
  45.                 AddProduct(dbConnection, "KillingSpree", "Johnson", new DateTime(2013, 7, 12), "123-123-435");
  46.                 AddProduct(dbConnection, "AAAAAAAA", "BBBBBBBBB", new DateTime(2013, 7, 15), "124-12-35");
  47.                 ListAllBooks(dbConnection);
  48.                 FindBookByName(dbConnection, "illi");
  49.             }
  50.         }
  51.  
  52.         private static void FindBookByName(MySqlConnection dbConnection, string input)
  53.         {
  54.             MySqlCommand selectProducts = new MySqlCommand("SELECT * FROM Books WHERE Title LIKE @searchString", dbConnection);
  55.             input = input.Replace("%", "[%]").Replace("'", "[']").Replace("\\", "[\\]").Replace("_", "[_]").Replace("\"", "[\"]");
  56.             selectProducts.Parameters.AddWithValue("@searchString", "%" + input + "%");
  57.  
  58.             MySqlDataReader result = selectProducts.ExecuteReader();
  59.  
  60.             using (result)
  61.             {
  62.                 Console.WriteLine(new String('-', 20));
  63.                 Console.WriteLine("Found Books");
  64.                 Console.WriteLine(new String('-', 20));
  65.                 while (result.Read())
  66.                 {
  67.                     Console.WriteLine("{0} {1} {2} {3} {4}", result["BookId"], result["Title"], result["Author"], result["PublishDate"], result["ISBN"]);
  68.                 }
  69.  
  70.             }
  71.         }
  72.  
  73.         private static void ListAllBooks(MySqlConnection dbConnection)
  74.         {
  75.             Console.WriteLine(new String('-', 20));
  76.             Console.WriteLine("List All Books");
  77.             Console.WriteLine(new String('-', 20));
  78.             MySqlCommand getAllBooks = new MySqlCommand("SELECT * FROM Books", dbConnection);
  79.  
  80.             MySqlDataReader result = getAllBooks.ExecuteReader();
  81.  
  82.             using (result)
  83.             {
  84.                 while (result.Read())
  85.                 {
  86.                     Console.WriteLine("{0} {1} {2} {3} {4}", result["BookId"], result["Title"], result["Author"], result["PublishDate"], result["ISBN"]);
  87.                 }
  88.             }
  89.         }
  90.  
  91.         private static void AddProduct(MySqlConnection dbConnection, string title, string author, DateTime publishDate, string ISBN)
  92.         {
  93.             MySqlCommand insertIntoProducts = new MySqlCommand(
  94.  @"
  95. INSERT INTO Books(Title, Author, PublishDate, ISBN)
  96. VALUES
  97.    (
  98.        @Title,
  99.        @Author,
  100.        @PublishDate,
  101.        @ISBN
  102.    )
  103.  
  104. ", dbConnection);
  105.             insertIntoProducts.Parameters.AddWithValue("@Title", title);
  106.             insertIntoProducts.Parameters.AddWithValue("@Author", author);
  107.             insertIntoProducts.Parameters.AddWithValue("@PublishDate", publishDate.ToString("yyyy-MM-dd"));
  108.             insertIntoProducts.Parameters.AddWithValue("@ISBN", ISBN);
  109.  
  110.  
  111.             Console.WriteLine("({0}) Rows Added", insertIntoProducts.ExecuteNonQuery());
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement