C#Winfrom資料庫增刪改查例項--SQL操作版
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.Data.OleDb; using System.Configuration; namespace TestDBOper { public partial class fmMain : Form { static string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; static DBState dbs; enum DBState { sAdd, sMod, sDel, sBro } public fmMain() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.dataGridView1.AutoGenerateColumns = false; //不要自動生成列 btnQuery_Click(sender, e); } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } //查詢 private void btnQuery_Click(object sender, EventArgs e) { int p = 0; if (this.dataGridView1.CurrentCell != null) { p = this.dataGridView1.CurrentCell.RowIndex; } OleDbConnection conn = new OleDbConnection(connstr); OleDbDataAdapter da = new OleDbDataAdapter("select * from users where username like '%" + this.textBox1.Text + "%'", conn); DataTable dt = new DataTable(); conn.Open(); da.Fill(dt); this.dataGridView1.DataSource = dt; if (p < dataGridView1.Rows.Count) { this.dataGridView1.CurrentCell = dataGridView1.Rows[p].Cells[0]; } toolStripStatusLabel1.Text = "共查詢到 " + dt.Rows.Count.ToString() + " 條資料"; dbs = DBState.sBro; SetBtn(); } //增加 private void btnAdd_Click(object sender, EventArgs e) { dbs = DBState.sAdd; SetBtn(); UserTxtClear(); } //修改 private void btnMod_Click(object sender, EventArgs e) { dbs = DBState.sMod; SetBtn(); UserRefresh(); } //刪除 private void btnDel_Click(object sender, EventArgs e) { if (MessageBox.Show("您確定要刪除嗎?", "提示", MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } string sql = "delete from users where id
[email protected]"; OleDbConnection conn = new OleDbConnection(connstr); OleDbCommand cmd = new OleDbCommand(sql, conn); OleDbParameter[] param = { new OleDbParameter("@id", this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells["id"].Value.ToString()) }; cmd.Parameters.AddRange(param); conn.Open(); int n = 0; n = cmd.ExecuteNonQuery(); conn.Close(); if (n != 0) { MessageBox.Show("刪除成功"); } else { MessageBox.Show("刪除失敗"); } btnQuery_Click(sender, e); } //儲存 private void btnSave_Click(object sender, EventArgs e) { if (txtUserName.Text.Trim()=="") { MessageBox.Show("姓名 不能為空"); return; } if (txtUserSex.Text.Trim()=="") { MessageBox.Show("性別 不能為空"); return; } if (txtUserAge.Text.Trim()=="") { MessageBox.Show("年齡不能為空"); return; } if (dbs == DBState.sAdd) { string sql = "insert into Users (username,userage,usersex) values (@username,@userage,@usersex)"; OleDbConnection conn = new OleDbConnection(connstr); OleDbCommand cmd = new OleDbCommand(sql, conn); OleDbParameter[] param ={ new OleDbParameter("@username",txtUserName.Text), new OleDbParameter("@userage",txtUserAge.Text), new OleDbParameter("@usersex",txtUserSex.Text) }; cmd.Parameters.AddRange(param); conn.Open(); int n = 0; n = cmd.ExecuteNonQuery(); conn.Close(); if (n != 0) { MessageBox.Show("增加成功"); } else { MessageBox.Show("增加失敗"); } } else if (dbs == DBState.sMod) { string sql = "update users set[email protected],[email protected],[email protected] where id= @id"; OleDbConnection conn = new OleDbConnection(connstr); OleDbCommand cmd = new OleDbCommand(sql, conn); OleDbParameter[] param ={ new OleDbParameter("@username",txtUserName.Text), new OleDbParameter("@userage",txtUserAge.Text), new OleDbParameter("@usersex",txtUserSex.Text), new OleDbParameter("@id",this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["id"].Value.ToString()) }; cmd.Parameters.AddRange(param); conn.Open(); int n = 0; n = cmd.ExecuteNonQuery(); conn.Close(); if (n != 0) { MessageBox.Show("修改成功"); } else { MessageBox.Show("修改失敗"); } } dbs = DBState.sBro; SetBtn(); btnQuery_Click(sender, e); } //取消 private void btnCancel_Click(object sender, EventArgs e) { dbs = DBState.sBro; SetBtn(); } //---------------------------------------------- 功能函式 ------------------------------------------------ //清空輸入框 private void UserTxtClear() { txtUserName.Text = ""; txtUserSex.Text = ""; txtUserAge.Text = ""; } //重新整理使用者 private void UserRefresh() { txtUserName.Text = this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["username"].Value.ToString(); txtUserSex.Text = this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["usersex"].Value.ToString(); txtUserAge.Text = this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["userage"].Value.ToString(); } //設定按鈕狀態 private void SetBtn() { btnAdd.Enabled = dbs == DBState.sBro; btnMod.Enabled = dbs == DBState.sBro; btnDel.Enabled = dbs == DBState.sBro; btnQuery.Enabled = dbs == DBState.sBro; btnSave.Enabled = dbs != DBState.sBro; btnCancel.Enabled = dbs != DBState.sBro; } private void dataGridView1_DoubleClick(object sender, EventArgs e) { btnMod_Click(sender, e); } } }
相關推薦
C#Winfrom資料庫增刪改查例項--SQL操作版
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq;
C#連線資料庫增刪改查程式碼 SQL SERVER/ACCESS 通用類
using System.Data.OleDb; using System.Data; public static class DataTools { private static OleDbConnection oleCo
thinkphp3.2.3版本的資料庫增刪改查例項
框架thinkphp 版本:3.2.3 內容:資料庫操作 1.多表查詢一條資料 M('a表')->join("b表 on b表.id=a表.id")->where('
IntelliJ Idea SpringBoot 資料庫增刪改查例項
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/
Kotlin整合Spring Boot實現資料庫增刪改查(spring data jpa版)
接上次的kotlin整合spring boot的mybatis版本,這次分享的內容也很精彩,現在spring data jpa也慢慢流行起來了,因此學習kotlin的時候也順帶寫了spring data jpa版本的,下面就直接上程式碼分享給大家了 1 pom加入如下配置
unity網路實戰開發(叢林戰爭)-前期知識準備(011-c#連線資料庫並實現增刪改查以及sql注入問題)
使用工具:VS2015,Mysql使用語言:c#作者:Gemini_xujian參考:siki老師-《叢林戰爭》視訊教程繼上一篇文章內容,這節課講解一下資料庫的前期連線準備以及通過c# 實現資料庫的增刪改擦操作。首先你需要自行安裝Mysql以及它的workbench元件。然後
Python使用pymql操作資料庫的增刪改查例項
Python使用pymql操作資料庫的增刪改查例項 資料庫: 一、新增: import pymysql # 匯入pymysql包 conn=pymysql.connect(host='localhost',user='root',passwd='123',db='db_good
SQL Server資料庫————增刪改查
--增刪改查--增 insert into 表名(列名) value(值列表) --刪 delect from 表名 where 條件 --改 update 表名 set 列名=值1,列名2=值2 where 條件 --查 select 列名1,列名2,...from 表名 where 條件 gr
ASP.NET 連線資料庫 增刪改查簡單例項
本文主要講解連線ASP.NET 連線資料庫最簡單的方法和增刪改查的小例子,因為只涉及到一個頁面,所以沒有使用web.config,以及使用DBHelper,旨在讓讀者們拋開封裝好的東西,瞭解實際程式碼。 因為本例涉及到資料庫,所以在開始之前請先在資料庫裡新建一個名為“te
C#編寫簡單的資料庫增刪改查(二)
一、今天我們繼續資料庫的增刪改查的編寫,下面我們進行刪除操作,我們在Formdelete介面上拖入幾個控制元件,如下圖: (資料庫顯示框參照之前的資料庫增加的方法) 將控制元件改名後,雙擊刪除按鈕進入程式碼編寫介面,首先連線資料庫,在判斷
MySQL - 最經典的 命令列操作資料庫 + 表(增刪改查例項)
MySQL - 最經典的 命令列操作資料庫 + 表(增刪改查例項) 安裝 MySQL 請參考:MySQL 安裝 + 入門大全 + 常用命令合集 增刪改查例項步驟: (1)登入 MySQL,建立新的資料庫,切換資料庫: (2)建立相對複雜的表,查看錶結構:
SQL Server 資料庫增刪改查語句
在使用SQL Server的時候,需要在新建查詢中簡單的處理資料庫,下面介紹一下SQL基礎的增刪改查語句: 介紹下面用到的變數: Table:資料庫表名 Column::欄位 Content:內容 1
winfrom連線資料庫增刪改查寫法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Data; using System.Data.SqlClient; namespa
Flask框架教程彙總(5)---其中有原生sql +資料庫增刪改查+ migrate遷移 等
本節目錄: 1 原生sql資料庫 2 在flask中使用ORM模型 3 設計表模型 4 建立模型 5 資料的增/刪/改操作 6 拆分成mvt 7 自定義刪除的類 8 資料的查詢操作 9 資料的邏輯查詢 10
NX二次開發-NX連線SqlServer資料庫(增刪改查)C#版
版本:NX9+VS2012+SqlServer2008r2 以前我寫過一個NX連線MySQL資料庫(增刪改查)的文章https://www.cnblogs.com/nxopen2018/p/12297590.html 這次寫一下,NX連線SqlServer資料庫(增刪改查)C#版,使用了ADO.NET技術。
c# xml的增刪改查
類型 window ttr eat val read 查找 element exc 1.xml的用途很廣,也很重要。 2.源代碼直接分享 using System;using System.Collections.Generic;using System.Component
java springboot與elasticsearch結合以及elasticsearch增刪改查例項
上一篇中,我們為同學們講解了如何安裝elasticsearch(下面簡稱es),如果還沒有es環境的同學,可以閱讀Linux 安裝Elasticsearch和配置ik分詞器步驟 來將es的環境先都準備好,今天我們要做的是將es融入到我們的java專案中,還是一如既往,我們還是使用
32、mysql資料庫增刪改查
建立資料庫 CREATE {DATABASE|SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] [CHARACTER SET=''] [DEFAULT] [COLLATE=''] IF NOT EXISTS 指定資料庫不存在的時候才建立 CHARACTER SET=
flask和django區別--資料庫增刪改查的區別
flask和django都是一樣的,在你建立了資料模型之後,兩個框架都會給你資料庫操作的api,供你使用;(create retrieve update delete) 假設我們有一個User類 增加(插入): 對於flask的插入分為三步走的情況: 1:建立python 物件;也就
SpringMVC+Spring+HIbernate 簡單增刪改查例項
SpringMVC+Spring+HIbernate 簡單增刪改查例項 HIbernate配置mysql資料庫的方式 和 Structs+spring+HIbernate 是一樣的。 可以理解為SpringMVC 把