<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
介面中有一個dataGridview、兩個Button、兩個Label和兩個TextBox。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Windows.Forms; using System.Data; using MySql.Data.MySqlClient; namespace TemSys { public class DBCtrl { private MySqlConnection m_ClientsqlConn; public DBCtrl() // 連線型別 { m_ClientsqlConn = new MySqlConnection(); try { m_ClientsqlConn.Dispose(); m_ClientsqlConn.Close(); m_ClientsqlConn.ConnectionString = "Database=dbName;Data Source=localhost;User Id=root;Password=123;charset=utf8"; m_ClientsqlConn.Open(); } catch (Exception ee) { MessageBox.Show(ee.Message); } } public DBCtrl(string IP, string DBname, string Uname, string Pword) // 建立連線 { m_ClientsqlConn = new MySqlConnection(); try { m_ClientsqlConn.Dispose(); m_ClientsqlConn.Close(); m_ClientsqlConn.ConnectionString = string.Format("Database={0};Data Source={1};User Id={2};Password={3};charset=utf8", DBname, IP, Uname, Pword); m_ClientsqlConn.Open(); } catch (Exception ee) { MessageBox.Show(ee.Message); } } public void DBConn(string connStr) // 過載 建立連線 { try { m_ClientsqlConn.Close(); m_ClientsqlConn.ConnectionString = connStr; m_ClientsqlConn.Open(); } catch (Exception ee) { MessageBox.Show(ee.Message); } } public DataTable GetDataTable(string SQLstr) // 獲取DataTable 一個表 { Console.Write("zcn==獲取資料庫連線,開啟資料庫"); try { if (m_ClientsqlConn.State == ConnectionState.Open) m_ClientsqlConn.Close(); m_ClientsqlConn.Open(); MySqlDataAdapter da = new MySqlDataAdapter(SQLstr, m_ClientsqlConn); DataTable resultDS = new DataTable(); da.Fill(resultDS); return resultDS; } catch (Exception ee) { Console.Write("zcn==獲取資料庫連線,開啟資料庫異常異常"); //MessageBox.Show( ee.Message); m_logclass.WriteLogFilein(ee.Message, "GetDataTable.txt"); return null; } finally { m_ClientsqlConn.Close(); } } public DataTable GetDataTableUsing(string SQLstr) // 獲取DataTable 一個表 { using (MySqlConnection m_ClientsqlConn = new MySqlConnection()) { } try { if (m_ClientsqlConn.State == ConnectionState.Open) m_ClientsqlConn.Close(); m_ClientsqlConn.Open(); MySqlDataAdapter da = new MySqlDataAdapter(SQLstr, m_ClientsqlConn); DataTable resultDS = new DataTable(); da.Fill(resultDS); return resultDS; } catch (Exception ee) { //MessageBox.Show( ee.Message); return null; } } public List<string> GetStringListfor(string lineName,DataTable dt) //根據某一列的名字 獲取某個集合中該列的所有值 { List<string> list = new List<string>(); foreach (DataRow dr in dt.Rows) { list.Add((string)dr[lineName]); } return list; } public List<DataRow> GetDataRowfor(DataTable dt) //根據 datatable 獲取每一行的資料的datarow { List<DataRow> list = new List<DataRow>(); foreach (DataRow dr in dt.Rows) { list.Add(dr); } return list; } /* public DataRow GetDataRowfor(string tablename,string ID) //根據ID號 返回對應行的 DataRow { string ss = "select * from " + tablename + " where ID = '"+ID +"'"; DataRow dr = new DataRow(); DataTable dt = GetDataTable(ss); dr = dt.Rows[0]; return dr; } */ public DataTable GetDataTableOneLine(string SQLstr) // 獲取DataTable 一個表中一行 { MySqlDataAdapter da = new MySqlDataAdapter(SQLstr, m_ClientsqlConn); DataTable resultDS = new DataTable(); da.Fill(resultDS); return resultDS; } public DataTable GetDataSet_to_Table(string SQLstr) // 獲取 dataset 多個表中 table { try { MySqlDataAdapter da = new MySqlDataAdapter(SQLstr, m_ClientsqlConn); DataSet ds = new DataSet(); da.Fill(ds); return ds.Tables[0]; } catch { return null; } } public Boolean InsertDBase(string insString) { try { if (m_ClientsqlConn.State == ConnectionState.Open) m_ClientsqlConn.Close(); m_ClientsqlConn.Open(); MySqlCommand sqlcomd = new MySqlCommand(insString, m_ClientsqlConn); sqlcomd.ExecuteNonQuery(); return true; } catch (Exception ee) { return false; } finally { m_ClientsqlConn.Close(); } } public Boolean deleteRowfor(string tablename,int deleteID) //根據 ID 刪除指定行 { try { string ss ="delete from "+ tablename +" where ID = "+ deleteID; if (m_ClientsqlConn.State == ConnectionState.Open) m_ClientsqlConn.Close(); m_ClientsqlConn.Open(); MySqlCommand sqlcmd = new MySqlCommand(ss, m_ClientsqlConn); sqlcmd.ExecuteNonQuery(); return true; } catch(Exception ee) { return false; } } public Boolean ModifyRowfor(string modifystr,string modifyID) // 根據 { try { string ss = modifystr + " where id = " + modifyID; if (m_ClientsqlConn.State == ConnectionState.Open) m_ClientsqlConn.Close(); m_ClientsqlConn.Open(); MySqlCommand sqlcmd = new MySqlCommand(ss, m_ClientsqlConn); sqlcmd.ExecuteNonQuery(); return true; } catch (Exception ee) { return false; } finally { m_ClientsqlConn.Close(); } } public void CloseDBase() // 引數型別 不同資料庫連線 { m_ClientsqlConn.Close(); } } }
ps:資料庫的設定資訊儲存在Config.ini檔案中,如果僅是測試用的話,可以直接在
m_DataBase = new DBCtrl(ipstr, namestr, usernamestr, passwordstr);
處輸入ip地址、資料庫名、資料庫使用者名稱和密碼即可
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace TemSys { public partial class ModifyDevice : Form { [DllImport("kernel32")] //讀寫ini檔案函數 private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); DataTable dt = new DataTable(); private DBCtrl m_DataBase; public ModifyDevice() { InitializeComponent(); } //將所有的textBox值設為空 private void TextBoxNull() { textBox1.Text = ""; textBox2.Text = ""; } //設定Lab值 private void labelshow() { label1.Text = dataGridView1.Columns[0].HeaderText; label2.Text = dataGridView1.Columns[12].HeaderText; } //初始化介面 private void ModifyDevice_Load(object sender, EventArgs e) { StringBuilder retval = new StringBuilder(); GetPrivateProfileString("DBConfig", "dbip", "", retval, 20, AppDomain.CurrentDomain.BaseDirectory + "Config.ini"); string ipstr = retval.ToString(); GetPrivateProfileString("DBConfig", "dbname", "", retval, 20, AppDomain.CurrentDomain.BaseDirectory + "Config.ini"); string namestr = retval.ToString(); GetPrivateProfileString("DBConfig", "dbusername", "", retval, 20, AppDomain.CurrentDomain.BaseDirectory + "Config.ini"); string usernamestr = retval.ToString(); GetPrivateProfileString("DBConfig", "dbpassword", "", retval, 20, AppDomain.CurrentDomain.BaseDirectory + "Config.ini"); string passwordstr = retval.ToString(); m_DataBase = new DBCtrl(ipstr, namestr, usernamestr, passwordstr); initDataTable(); } private void initDataTable() { string ssp = string.Format("select * from device_info1"); dt = m_DataBase.GetDataTable(ssp); dataGridView1.DataSource = dt; labelshow(); } //雙擊dataGridView響應事件 private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { string index = dataGridView1.CurrentRow.Cells[0].Value.ToString(); if (label1.Text == "id") { string ssp = string.Format("select * from device_info1 where id='" + index + "'"); dt = m_DataBase.GetDataTable(ssp); //DataRow row = dt.Rows[0]; textBox1.Text = dt.Rows[0]["id"].ToString(); textBox2.Text = dt.Rows[0]["number"].ToString(); } } //點選修改按鈕響應事件 private void btnModify_Click(object sender, EventArgs e) { bool flag = false; string ssp = string.Format("update device_info1 set number='" + textBox2.Text + "'"); flag = m_DataBase.ModifyRowfor(ssp, textBox1.Text); if (flag) { MessageBox.Show("修改成功!"); initDataTable(); } else { MessageBox.Show("修改失敗!"); } } private void btnDelete_Click(object sender, EventArgs e) { bool flag = false; int currentIndex = (int)dataGridView1.CurrentRow.Cells[0].Value; Console.WriteLine("輸出當前選中資料行:" + currentIndex); flag = m_DataBase.deleteRowfor("device_info1", currentIndex); if (flag) { MessageBox.Show("刪除成功!"); initDataTable(); } else { MessageBox.Show("刪除失敗!"); } } } }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45