Advertisement
Guest User

Untitled

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