private void ScanCardInfoWatcher_Changed(object sender, FileSystemEventArgs e) { addrow("select * from scan_card_info where Card_Record >" + ScanCardInfoIndex, ((DataGridView)(((TabPage)(DatabaseTabControl.TabPages[2])).Controls[0]))); using (SQLClass CurrentCount = new SQLClass("select count(*) from scan_card_info", MySQLConnectionString)) { CurrentCount.Reader.Read(); ScanCardInfoIndex = CurrentCount.Reader.GetInt32(0); if (Card_timer.Enabled) { Card_timer.Stop(); } Card_timer.Start(); label_card.Text = ((DataGridView)(((TabPage)(DatabaseTabControl.TabPages[2])).Controls[0])).Rows[ScanCardInfoIndex - 1].Cells[1].Value.ToString(); } }
private void addrow(string DbCommand, DataGridView data) //DbCommand: Db的指令 //data: 在哪一個頁面加入row { using (SQLClass rowData = new SQLClass(DbCommand, MySQLConnectionString)) { while (rowData.Reader.Read()) { DataGridViewRow row = new DataGridViewRow(); row.CreateCells(data); for (int index = 0; index < rowData.Reader.FieldCount; index++) { row.Cells[index].Value = rowData.Reader.GetString(index); } data.Rows.Add(row); } } }
private void DefaultButton_Click(object sender, EventArgs e)//重置 { foreach (Control con in DatabaseTabControl.Controls) { ((DataGridView)con.Controls[0]).Rows.Clear(); string tbName = ((TabPage)con).Text; addrow("select * from " + tbName, (DataGridView)con.Controls[0]); } #region 初始化FileSystemWatcher供更新畫面用 using (SQLClass ScanFaceInfoCount = new SQLClass("select count(*) from scan_face_info", MySQLConnectionString)) { ScanFaceInfoCount.Reader.Read(); ScanFaceInfoIndex = ScanFaceInfoCount.Reader.GetInt32(0); } using (SQLClass ScanCardInfoCount = new SQLClass("select count(*) from scan_card_info", MySQLConnectionString)) { ScanCardInfoCount.Reader.Read(); ScanCardInfoIndex = ScanCardInfoCount.Reader.GetInt32(0); } ScanFaceInfoWatcher.EnableRaisingEvents = true; ScanCardInfoWatcher.EnableRaisingEvents = true; }
int ScanFaceInfoIndex, ScanCardInfoIndex;//供更新畫面使用 private void MainForm_Load(object sender, EventArgs e) { #region 檢查mysql有沒有打開 MySqlConnection CheckDB = new MySqlConnection(MySQLConnectionString); try { CheckDB.Open(); CheckDB.Close(); MySqlConnection.ClearPool(CheckDB); } catch (MySqlException sqlEx) { MessageBox.Show(sqlEx.Message, "SQL錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Close(); Environment.Exit(Environment.ExitCode); } catch (Exception ex) { MessageBox.Show(ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Close(); Environment.Exit(Environment.ExitCode); } #endregion #region 檢查有無Picture資料夾 Console.WriteLine(Directory.GetCurrentDirectory()); if (!Directory.Exists("Picture")) { Directory.CreateDirectory("Picture"); } #endregion using (SQLClass StudentID = new SQLClass("select * from student_info", MySQLConnectionString)) { while (StudentID.Reader.Read()) { StudentIDListComboBox.Items.Add(StudentID.Reader.GetString(0)); } } ColumnListBox.Width = tableLayoutPanel1.Controls[2].Width - 3; SearchTextBox.Width = tableLayoutPanel1.Controls[2].Width - 3; SearchButton.Width = tableLayoutPanel1.Controls[2].Width - 3; DefaultButton.Width = tableLayoutPanel1.Controls[2].Width - 3; using (SQLClass initialization = new SQLClass("show tables", MySQLConnectionString)) { while (initialization.Reader.Read()) { TabPage page = new TabPage(); page.Text = initialization.Reader.GetString(0);//page name DatabaseTabControl.TabPages.Add(page); page.Location = new System.Drawing.Point(4, 38); page.Padding = new System.Windows.Forms.Padding(3); page.Size = new System.Drawing.Size(782, 798); page.UseVisualStyleBackColor = true; DataGridView data = new DataGridView(); page.Controls.Add(data); data.Location = new System.Drawing.Point(3, 3); data.RowTemplate.Height = 27; data.Size = new System.Drawing.Size(776, 792); data.Dock = System.Windows.Forms.DockStyle.Fill; data.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; data.AllowUserToAddRows = false; //添加顯示圖片功能 if (String.Compare(page.Text, "Scan_Card_Info", true) == 0) { data.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.DoubleClickShowPictrue); } using (SQLClass title = new SQLClass("select column_name from INFORMATION_SCHEMA.COLUMNS where table_name='" + initialization.Reader.GetString(0) + "'", MySQLConnectionString)) { while (title.Reader.Read()) { DataGridViewTextBoxColumn Column = new System.Windows.Forms.DataGridViewTextBoxColumn(); Column.HeaderText = title.Reader.GetString(0); Column.Name = title.Reader.GetString(0); Column.ReadOnly = true; Column.Resizable = System.Windows.Forms.DataGridViewTriState.False; Column.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; data.Columns.AddRange(Column); } } addrow("select * from " + initialization.Reader.GetString(0), data);//讀入資料 } } searchListbox(DatabaseTabControl.SelectedIndex);//初始化搜尋欄位 receiver_backgroundWorker.RunWorkerAsync(); #region 初始化FileSystemWatcher供更新畫面用 using (SQLClass ScanFaceInfoCount = new SQLClass("select count(*) from scan_face_info", MySQLConnectionString)) { ScanFaceInfoCount.Reader.Read(); ScanFaceInfoIndex = ScanFaceInfoCount.Reader.GetInt32(0); } using (SQLClass ScanCardInfoCount = new SQLClass("select count(*) from scan_card_info", MySQLConnectionString)) { ScanCardInfoCount.Reader.Read(); ScanCardInfoIndex = ScanCardInfoCount.Reader.GetInt32(0); } this.ScanFaceInfoWatcher.Changed += new System.IO.FileSystemEventHandler(this.ScanFaceInfoWatcher_Changed); this.ScanCardInfoWatcher.Changed += new System.IO.FileSystemEventHandler(this.ScanCardInfoWatcher_Changed); this.ScanFaceInfoWatcher.EnableRaisingEvents = true; this.ScanCardInfoWatcher.EnableRaisingEvents = true; #endregion }