Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace MySQL
  8. {
  9.     class Program
  10.     {
  11.         static MySqlConnection conn;
  12.         static MySqlCommand cmd;
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             string dbHost = "127.0.0.1";
  17.             string dbUser = "";
  18.             string dbPass = "";
  19.             string dbName = "TOTP";
  20.             string connStr = "server=" + dbHost + ";uid=" + dbUser + ";pwd=" + dbPass + ";database=" + dbName;
  21.             conn = new MySqlConnection(connStr);
  22.             cmd = conn.CreateCommand();
  23.             try
  24.             {
  25.                 Console.WriteLine("正在連結 MySQL 資料庫\n\nHost:{0}\nUser:{1}\nPassword:{2}\nDatabase:{3}\n", dbHost, dbUser, dbPass, dbName);
  26.                 conn.Open();
  27.                 Console.WriteLine("成功連結!\n");
  28.                 CreateTable();
  29.                 Console.WriteLine("新增 OTP 題庫\n");
  30.                 Insert();
  31.                 Show();
  32.             }
  33.             catch (MySqlException ex)
  34.             {
  35.                 Console.WriteLine(ex.Message);
  36.             }
  37.             Console.ReadKey();
  38.         }
  39.  
  40.         static void CreateTable()
  41.         {
  42.             try
  43.             {
  44.                 cmd.CommandText = "SELECT `index` FROM `question`";
  45.                 cmd.ExecuteNonQuery();
  46.             }
  47.             catch (MySqlException ex)
  48.             {
  49.                 cmd.CommandText = "CREATE TABLE `totp`.`question` ( `index` INT(4) NOT NULL , `descript` VARCHAR(80) NULL , `answer` VARCHAR(32) NULL , PRIMARY KEY (`index`)) ENGINE = InnoDB;";
  50.                 cmd.ExecuteNonQuery();
  51.                 Console.WriteLine("新增資料表 question\n");
  52.             }
  53.         }
  54.  
  55.         static void Insert()
  56.         {
  57.             int index = 1;
  58.             cmd.CommandText = "SELECT MAX(`index`) FROM `question`";
  59.             using (var result = cmd.ExecuteReader())
  60.             {
  61.                 try
  62.                 {
  63.                     result.Read();
  64.                     index = result.GetInt32(0) + 1;
  65.                 }
  66.                 catch (Exception)
  67.                 {
  68.  
  69.                 }
  70.             }
  71.             Console.Write("新增問題(Y/N):");
  72.             string flag = Console.ReadLine().ToLower();
  73.             while (flag == "y")
  74.             {
  75.                 Console.Write("請輸入問題:");
  76.                 string descript = Console.ReadLine();
  77.                 Console.Write("請輸入答案:");
  78.                 string answer = Console.ReadLine();
  79.                 cmd.CommandText = "INSERT INTO `question` VALUES(" + index + ",'" + descript + "','" + answer + "')";
  80.                 cmd.ExecuteNonQuery();
  81.                 Console.Write("繼續新增問題(Y/N):");
  82.                 flag = Console.ReadLine().ToLower();
  83.             }
  84.         }
  85.  
  86.         static void Show()
  87.         {
  88.             cmd.CommandText = "SELECT * FROM `question`";
  89.             using (var result = cmd.ExecuteReader())
  90.             {
  91.                 try
  92.                 {
  93.                     Console.WriteLine("\nIndex\tDescript\tAnswer");
  94.                     while (result.Read())
  95.                         Console.WriteLine("{0}\t{1}\t{2}", result.GetUInt32(0), result.GetString(1), result.GetString(2));
  96.                 }
  97.                 catch (Exception)
  98.                 {
  99.  
  100.                 }
  101.             }
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement