單字程式的最終的實作結果執行檔、今天的Playground範例程式碼以上傳至Github
https://github.com/ted59438/EnglishVocabulary_MySQL
2.進入「單字管理」,將所有分類帶入上方的下拉選單
請參考Github當中的DBHelper
private void queryStudentBtn_Click(object sender, EventArgs e)
{
string sql = @"SELECT RealName AS 真實姓名,
Username AS 測驗帳號,
CAST(Birthdate AS DATE) AS 出生日期
FROM Student";
queryResultGrid.DataSource = queryDT(sql);
}
/// <summary>
/// 基本查詢:起
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private DataTable queryDT(string sql)
{
// Step 1. 建立連線物件 (SqlConnection)
MySqlConnection connection = new MySqlConnection();
connection.ConnectionString = getConnectString();
// Step 2. 建立指令物件 (SqlCommand)
MySqlCommand command = new MySqlCommand();
command.Connection = connection;
command.CommandText = sql;
// Step 3. 建立撈取資料的物件 (Adapter)
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = command;
// Step 4. 開啟連線
connection.Open();
// Step 5. 執行SELECT查詢,取得資料後存放到DataTable
DataTable queryResultDT = new DataTable();
adapter.Fill(queryResultDT);
// Step 6. 關閉連線
connection.Close();
return queryResultDT;
}
MySqlConnection connection = new MySqlConnection();
connection.ConnectionString = getConnectString();
MySqlCommand command = new MySqlCommand();
command.Connection = connection;
command.CommandText = sql;
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = command;
connection.Open();
DataTable queryResultDT = new DataTable();
adapter.Fill(queryResultDT);
connection.Close();