Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 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 = "sabpprook";
  18.             string dbPass = "s0931222509";
  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.             }
  32.             catch (MySqlException ex)
  33.             {
  34.                 Console.WriteLine(ex.Message);
  35.             }
  36.         }
  37.  
  38.         static void CreateTable()
  39.         {
  40.             try
  41.             {
  42.                 cmd.CommandText = "SELECT `index` FROM `question`";
  43.                 cmd.ExecuteNonQuery();
  44.             }
  45.             catch (MySqlException ex)
  46.             {
  47.                 cmd.CommandText = "CREATE TABLE `totp`.`question` ( `index` INT(4) NOT NULL , `descript` VARCHAR(80) NULL , `answer` VARCHAR(32) NULL , PRIMARY KEY (`index`)) ENGINE = InnoDB;";
  48.                 cmd.ExecuteNonQuery();
  49.                 Console.WriteLine("新增資料表 question\n");
  50.             }
  51.         }
  52.  
  53.         static void Insert()
  54.         {
  55.             int index = 1;
  56.             cmd.CommandText = "SELECT MAX(`index`) FROM `question`";
  57.             using (var result = cmd.ExecuteReader())
  58.             {
  59.                 result.Read();
  60.                 index = result.GetInt32(0) + 1;
  61.             }
  62.             while (true)
  63.             {
  64.                 Console.Write("請輸入問題:");
  65.                 string descript = Console.ReadLine();
  66.                 Console.Write("請輸入答案:");
  67.                 string answer = Console.ReadLine();
  68.                 cmd.CommandText = "INSERT INTO `question` VALUES(" + index + ",'" + descript + "','" + answer + "')";
  69.                 cmd.ExecuteNonQuery();
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement