private void btnClearQUery_Click(object sender, EventArgs e) { LstViewSample.Items.Clear(); LstViewQuery.Items.Clear(); GridViewDbData.ClearSelection(); btnSearchGene.Enabled = false; btnClearSearchTxt.Enabled = false; txtBoxDrugNameInput.Enabled = false; }
private void LoadDbData() { // sql command => take all columns in database string SQLcommand = string.Format(@"SELECT * FROM TablePharmacogene"); // connection string string connectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", Path.Combine(PATH.DIR_DATA_SOURCE, DatabasebName)); // this is for database connect // traditionally, when retrieving data from database, // "open", "read/write", "close" three actions using (OleDbConnection connection = new OleDbConnection(connectionString)) { // Create a command and set its connection OleDbCommand cmd = new OleDbCommand(SQLcommand, connection); // Open the connection and execute the select command. // sometimes errors may occur, so use try ... catch to deal with exception try { // Open connecton connection.Open(); // refer to this // https://dotblogs.com.tw/yc421206/2009/07/12/9355 OleDbDataAdapter a = new OleDbDataAdapter(cmd); // DataTable is a datatype which form is like worksheet in excel. DataTable dt = new DataTable(); a.SelectCommand = cmd; a.Fill(dt); // GridViewDbData corresponds to the component in the windows // which demonstrates data in database. GridViewDbData.DataSource = dt; // auto resize column GridViewDbData.AutoResizeColumns(); } catch (Exception ex) { // when error occurs, display ex.Message Console.WriteLine(ex.Message); } } }
// event function corresponds to "Read DB Data" button private void btnReadDB_Click(object sender, EventArgs e) { GridViewDbData.ClearSelection(); LoadDbData(); }