Advertisement
Guest User

Untitled

a guest
Dec 26th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.29 KB | None | 0 0
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8.  
  9. namespace MySQL
  10. {
  11.     class Program
  12.     {
  13.         static MySqlConnection conn;
  14.         static MySqlCommand cmd;
  15.         static string dbHost = "127.0.0.1";
  16.         static string dbUser = "";
  17.         static string dbPass = "";
  18.  
  19.         static void Main(string[] args)
  20.         {
  21.             try
  22.             {
  23.                 Console.WriteLine("正在連結 MySQL 資料庫\n\nHost:{0}\nUser:{1}\nPassword:{2}\nDatabase:TOTP\n", dbHost, dbUser, dbPass);
  24.                 if (OpenDatabase())
  25.                 {
  26.                     Console.WriteLine("連結成功!\n");
  27.                     while (true)
  28.                     {
  29.                         Console.WriteLine("1. 建立 question, user 資料表");
  30.                         Console.WriteLine("2. 新增使用者");
  31.                         Console.WriteLine("3. 顯示使用者");
  32.                         Console.WriteLine("4. 新增 OTP 題庫");
  33.                         Console.WriteLine("5. 顯示 OTP 題庫");
  34.                         Console.WriteLine("6. 刪除使用者");
  35.                         Console.Write("input:");
  36.                         switch (Console.ReadLine())
  37.                         {
  38.                             case "1":
  39.                                 CreateTable();
  40.                                 break;
  41.                             case "2":
  42.                                 InsertUser();
  43.                                 break;
  44.                             case "3":
  45.                                 ShowUser();
  46.                                 break;
  47.                             case "4":
  48.                                 InsertQuestion();
  49.                                 break;
  50.                             case "5":
  51.                                 ShowQuestion();
  52.                                 break;
  53.                             case "6":
  54.                                 DeleteUser();
  55.                                 break;
  56.                             default:
  57.                                 break;
  58.                         }
  59.                     }
  60.                 }
  61.             }
  62.             catch (MySqlException ex)
  63.             {
  64.                 Console.WriteLine(ex.Message);
  65.             }
  66.             Console.ReadKey();
  67.         }
  68.  
  69.         static bool OpenDatabase()
  70.         {
  71.             string connStr = "server=" + dbHost + ";uid=" + dbUser + ";pwd=" + dbPass + ";database=TOTP";
  72.             try
  73.             {
  74.                 conn = new MySqlConnection(connStr);
  75.                 cmd = conn.CreateCommand();
  76.                 conn.Open();
  77.                 return true;
  78.             }
  79.             catch (Exception ex)
  80.             {
  81.                 if (ex.Message.Contains("Unknown database"))
  82.                 {
  83.                     Console.WriteLine("建立 TOTP 資料庫");
  84.                     string connStr2 = "server=" + dbHost + ";uid=" + dbUser + ";pwd=" + dbPass;
  85.                     conn = new MySqlConnection(connStr2);
  86.                     cmd = conn.CreateCommand();
  87.                     conn.Open();
  88.                     cmd.CommandText = "CREATE DATABASE TOTP";
  89.                     cmd.ExecuteNonQuery();
  90.                     conn.Close();
  91.                     conn = new MySqlConnection(connStr);
  92.                     cmd = conn.CreateCommand();
  93.                     conn.Open();
  94.                     return true;
  95.                 }
  96.                 else if (ex.Message.Contains("Access denied"))
  97.                 {
  98.                     Console.WriteLine("無法登入");
  99.                 }
  100.             }
  101.             return false;
  102.         }
  103.  
  104.         static void CreateTable()
  105.         {
  106.             try
  107.             {
  108.                 cmd.CommandText = "SELECT `index` FROM `question`";
  109.                 cmd.ExecuteNonQuery();
  110.             }
  111.             catch (MySqlException)
  112.             {
  113.                 cmd.CommandText = "CREATE TABLE `totp`.`question` ( `index` INT(4) NOT NULL , `descript` VARCHAR(80) NULL , `answer` VARCHAR(32) NULL , PRIMARY KEY (`index`)) ENGINE = InnoDB;";
  114.                 cmd.ExecuteNonQuery();
  115.                 Console.WriteLine("新增資料表 question\n");
  116.             }
  117.             try
  118.             {
  119.                 cmd.CommandText = "SELECT `account` FROM `user`";
  120.                 cmd.ExecuteNonQuery();
  121.             }
  122.             catch (MySqlException)
  123.             {
  124.                 cmd.CommandText = "CREATE TABLE `totp`.`user` ( `account` VARCHAR(20) NOT NULL , `password` VARCHAR(32) NOT NULL , PRIMARY KEY (`account`)) ENGINE = InnoDB;";
  125.                 cmd.ExecuteNonQuery();
  126.                 Console.WriteLine("新增資料表 user\n");
  127.             }
  128.         }
  129.  
  130.         static void InsertUser()
  131.         {
  132.             bool flag = true;
  133.             while (flag)
  134.             {
  135.                 Console.Write("\n帳號:");
  136.                 string account = Console.ReadLine();
  137.                 Console.Write("密碼:");
  138.                 string password = MD5Hash(Console.ReadLine());
  139.                 cmd.CommandText = string.Format("INSERT INTO `user` VALUES('{0}','{1}')", account, password);
  140.                 try
  141.                 {
  142.                     cmd.ExecuteNonQuery();
  143.                     Console.Write("新增使用者成功\n");
  144.                 }
  145.                 catch (Exception)
  146.                 {
  147.                     Console.Write("新增使用者失敗\n");
  148.                 }
  149.                 Console.Write("繼續新增使用者(Y/N):");
  150.                 flag = Console.ReadLine().ToLower() == "y";
  151.             }
  152.         }
  153.  
  154.         static string MD5Hash(string data)
  155.         {
  156.             using (MD5 md5 = MD5.Create())
  157.             {
  158.                 return BitConverter.ToString(md5.ComputeHash(Encoding.ASCII.GetBytes(data))).Replace("-", "");
  159.             }
  160.         }
  161.  
  162.         static void ShowUser()
  163.         {
  164.             StreamWriter file = new StreamWriter(File.Create("User.txt"));
  165.             cmd.CommandText = "SELECT * FROM `user`";
  166.             using (var result = cmd.ExecuteReader())
  167.             {
  168.                 try
  169.                 {
  170.                     Console.WriteLine("\nAccount\tPassword");
  171.                     while (result.Read())
  172.                     {
  173.                         string account = result.GetString(0);
  174.                         string password = result.GetString(1);
  175.                         Console.WriteLine("{0}\t{1}", account, password);
  176.                         file.WriteLine(account + ", " + password);
  177.                     }
  178.                     file.Close();
  179.                 }
  180.                 catch (Exception)
  181.                 {
  182.  
  183.                 }
  184.             }
  185.         }
  186.  
  187.         static void InsertQuestion()
  188.         {
  189.             int index = 1;
  190.             cmd.CommandText = "SELECT MAX(`index`) FROM `question`";
  191.             using (var result = cmd.ExecuteReader())
  192.             {
  193.                 try
  194.                 {
  195.                     result.Read();
  196.                     index = result.GetInt32(0) + 1;
  197.                 }
  198.                 catch (Exception)
  199.                 {
  200.  
  201.                 }
  202.             }
  203.             bool flag = true;
  204.             while (flag)
  205.             {
  206.                 Console.Write("請輸入問題:");
  207.                 string descript = Console.ReadLine();
  208.                 Console.Write("請輸入答案:");
  209.                 string answer = Console.ReadLine();
  210.                 cmd.CommandText = "INSERT INTO `question` VALUES(" + index++ + ",'" + descript + "','" + answer + "')";
  211.                 cmd.ExecuteNonQuery();
  212.                 Console.Write("繼續新增問題(Y/N):");
  213.                 flag = Console.ReadLine().ToLower() == "y";
  214.             }
  215.         }
  216.  
  217.         static void ShowQuestion()
  218.         {
  219.             StreamWriter file = new StreamWriter(File.Create("Question.txt"));
  220.             cmd.CommandText = "SELECT * FROM `question`";
  221.             using (var result = cmd.ExecuteReader())
  222.             {
  223.                 try
  224.                 {
  225.                     Console.WriteLine("\nIndex\tDescript\tAnswer");
  226.                     while (result.Read())
  227.                     {
  228.                         int index = result.GetInt32(0);
  229.                         string descript = result.GetString(1);
  230.                         string answer = result.GetString(2);
  231.                         Console.WriteLine("{0}\t{1}\t{2}", index, descript, answer);
  232.                         file.WriteLine(index + ", " + descript + ", " + answer);
  233.                     }
  234.                     file.Close();
  235.                 }
  236.                 catch (Exception)
  237.                 {
  238.  
  239.                 }
  240.             }
  241.         }
  242.  
  243.         static void DeleteUser()
  244.         {
  245.             Console.Write("請輸入使用者名稱:");
  246.             string User = Console.ReadLine();
  247.             cmd.CommandText = "DELETE FROM `user` WHERE `account`='" + User + "'";
  248.             try
  249.             {
  250.                 if (cmd.ExecuteNonQuery() > 0)
  251.                     Console.WriteLine("刪除使用者" + User + "成功\n");
  252.                 else
  253.                     Console.WriteLine("無使用者" + User + "\n");
  254.             }
  255.             catch (Exception e)
  256.             {
  257.                 Console.WriteLine(e.Message);
  258.             }
  259.         }
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement