当前位置:
首页 > Python基础教程 >
-
C#教程之WinForm之窗体应用程序
基本简单数据库操作(增删改查)
1 using System; 2 using System.Collections.Generic; 3 using System.Windows.Forms; 4 5 namespace DataBaseOperation 6 { 7 static class Program 8 { 9 /// <summary> 10 /// 应用程序的主入口点。 11 /// </summary> 12 [STAThread] 13 static void Main() 14 { 15 Application.EnableVisualStyles(); 16 Application.SetCompatibleTextRenderingDefault(false); 17 Application.Run(new frmMain()); 18 } 19 } 20 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 namespace DataBaseOperation 10 { 11 public partial class frmMain : Form 12 { 13 public frmMain() 14 { 15 InitializeComponent(); 16 } 17 18 private void btnSelect_Click(object sender, EventArgs e) 19 { 20 frmSelect fs = new frmSelect(); 21 fs.ShowDialog(); 22 } 23 24 private void btnInsert_Click(object sender, EventArgs e) 25 { 26 frmInsert fi = new frmInsert(); 27 fi.ShowDialog(); 28 } 29 30 private void btnUpdate_Click(object sender, EventArgs e) 31 { 32 frmUpdate fu = new frmUpdate(); 33 fu.ShowDialog(); 34 } 35 36 private void btnDelete_Click(object sender, EventArgs e) 37 { 38 frmDelete fd = new frmDelete(); 39 fd.ShowDialog(); 40 } 41 42 private void frmMain_Load(object sender, EventArgs e) 43 { 44 45 this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width; 46 this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height; 47 } 48 } 49 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Data.SqlClient;// 9 10 namespace DataBaseOperation 11 { 12 public partial class frmDelete : Form 13 { 14 public frmDelete() 15 { 16 InitializeComponent(); 17 } 18 19 SqlDataAdapter sda;// 20 DataSet ds = new DataSet();// 21 22 private void frmDelete_Load(object sender, EventArgs e) 23 { 24 //窗体加载时查询表中全部信息 25 26 //1. 27 string sql = "select sid,sname,ssex,saddress,semail from students"; 28 29 //2. 30 sda = new SqlDataAdapter(sql, DBHelper.connection); 31 32 int result = sda.Fill(ds); 33 34 if (result > 0) 35 { 36 this.dataGridView1.DataSource = ds.Tables[0]; 37 } 38 else 39 { 40 MessageBox.Show("表中无信息"); 41 } 42 43 } 44 45 private void 删除选中行ToolStripMenuItem_Click(object sender, EventArgs e) 46 { 47 if (this.dataGridView1.SelectedRows.Count > 0) 48 { 49 //1. 50 string id = this.dataGridView1.SelectedRows[0].Cells["sid"].Value.ToString(); 51 52 //2. 53 string sql = string.Format("delete from students where sid={0}", id); 54 55 //3. 56 57 try 58 { 59 SqlCommand command = new SqlCommand(sql, DBHelper.connection); 60 DBHelper.connection.Open(); 61 int result = command.ExecuteNonQuery(); 62 if (result > 0) 63 { 64 MessageBox.Show("成功删除该行信息!"); 65 } 66 else 67 { 68 MessageBox.Show("操作失败!"); 69 } 70 } 71 catch (Exception ex) 72 { 73 74 MessageBox.Show(ex.Message); 75 } 76 finally 77 { 78 DBHelper.connection.Close(); 79 } 80 } 81 else 82 { 83 MessageBox.Show("请选中要删除的行"); 84 } 85 86 //刷新控件中信息行 87 this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]); 88 } 89 90 private void btnSearchAll_Click(object sender, EventArgs e) 91 { 92 //1.清空ds中的表信息 93 ds.Tables.Clear(); 94 95 //2. 96 int result = sda.Fill(ds); 97 if (result > 0) 98 { 99 this.dataGridView1.DataSource = ds.Tables[0]; 100 } 101 else 102 { 103 MessageBox.Show("表中无信息"); 104 } 105 } 106 107 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 108 { 109 110 } 111 112 private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e) 113 { 114 115 } 116 117 private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) 118 { 119 120 } 121 } 122 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Data.SqlClient;// 9 10 namespace DataBaseOperation 11 { 12 public partial class frmInsert : Form 13 { 14 public frmInsert() 15 { 16 InitializeComponent(); 17 } 18 19 private void btnReset_Click(object sender, EventArgs e) 20 { 21 this.txtName.Text = ""; 22 this.txtAddress.Text = ""; 23 this.txtEmail.Text = ""; 24 this.radMan.Checked = true; 25 this.txtName.Focus(); 26 } 27 28 private void btnInsert_Click(object sender, EventArgs e) 29 { 30 //1.获取控件中用户输入的学员信息 31 string name = this.txtName.Text.Trim(); 32 string sex; 33 if (this.radMan.Checked) 34 { 35 sex = "1"; 36 } 37 else 38 { 39 sex = "0"; 40 } 41 MessageBox.Show(sex); 42 string address = this.txtAddress.Text.Trim(); 43 string email= this.txtEmail.Text.Trim(); 44 45 //2. 46 string sql=string.Format("insert into students (sname,ssex,saddress,semail) values('{0}',{1},'{2}','{3}')",name,sex,address,email); 47 48 //3. 49 try 50 { 51 SqlCommand command = new SqlCommand(sql, DBHelper.connection); 52 DBHelper.connection.Open(); 53 int result = command.ExecuteNonQuery(); 54 if (result > 0) 55 { 56 MessageBox.Show("成功添加一条学员信息"); 57 } 58 else 59 { 60 MessageBox.Show("操作失败!"); 61 } 62 } 63 catch (Exception ex) 64 { 65 66 MessageBox.Show(ex.Message); 67 } 68 finally 69 { 70 DBHelper.connection.Close(); 71 } 72 } 73 } 74 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Data.SqlClient;// 9 10 namespace DataBaseOperation 11 { 12 public partial class frmSelect : Form 13 { 14 public frmSelect() 15 { 16 InitializeComponent(); 17 } 18 19 private void btnSearchName_Click(object sender, EventArgs e) 20 { 21 //根据学号查询学员姓名 22 string id = this.txtNum1.Text.Trim(); 23 if (id != "") 24 { 25 string sql = string.Format("select sname from students where sid={0}", id); 26 try 27 { 28 SqlCommand command = new SqlCommand(sql, DBHelper.connection); 29 DBHelper.connection.Open(); 30 object name = command.ExecuteScalar(); 31 if (name != null) 32 { 33 this.lblName.Text = "此学员的姓名为:" + name.ToString(); 34 } 35 else 36 { 37 MessageBox.Show("查无此人!"); 38 this.lblName.Text = ""; 39 this.txtNum1.Text = ""; 40 this.txtNum1.Focus(); 41 } 42 43 44 } 45 catch (Exception ex) 46 { 47 48 MessageBox.Show(ex.Message); 49 } 50 finally 51 { 52 DBHelper.connection.Close(); 53 } 54 } 55 else 56 { 57 MessageBox.Show("请输入学号!"); 58 } 59 } 60 61 private void btnSearchStudentInfo_Click(object sender, EventArgs e) 62 { 63 //根据学号查询学员信息 64 string id = this.txtNum2.Text.Trim(); 65 if (id != "") 66 { 67 string sql = string.Format("select sname,ssex,saddress,semail from students where sid={0}", id); 68 try 69 { 70 SqlCommand command = new SqlCommand(sql, DBHelper.connection); 71 DBHelper.connection.Open(); 72 SqlDataReader sdr = command.ExecuteReader(); 73 if (sdr.Read()) 74 { 75 this.txtName1.Text = sdr["sname"].ToString(); 76 // MessageBox.Show("性别字段的值:"+sdr["ssex"].ToString()); 77 if (sdr["ssex"].ToString().ToLower() == "true") 78 { 79 this.radMan1.Checked = true; 80 } 81 else 82 { 83 this.radWoman1.Checked = true; 84 } 85 this.txtAddress.Text = sdr["saddress"].ToString(); 86 this.txtEmail.Text = sdr["semail"].ToString(); 87 } 88 else 89 { 90 MessageBox.Show("查无此人!"); 91 this.txtNum2.Text = ""; 92 this.txtNum2.Focus(); 93 this.txtName1.Text = ""; 94 this.radMan1.Checked = true; 95 this.txtAddress.Text = ""; 96 this.txtEmail.Text = ""; 97 } 98 99 sdr.Close(); 100 101 102 } 103 catch (Exception ex) 104 { 105 106 MessageBox.Show(ex.Message); 107 } 108 finally 109 { 110 DBHelper.connection.Close(); 111 } 112 } 113 else 114 { 115 MessageBox.Show("请输入学号!"); 116 } 117 } 118 119 DataSet ds = new DataSet();//创建数据集对象 120 SqlDataAdapter sda;//声明数据适配器 121 122 private void btnSearchBySex1_Click(object sender, EventArgs e) 123 { 124 //清空数据集中表信息 125 ds.Tables.Clear(); 126 127 //根据性别查询学员信息 128 string sex; 129 if (this.radMan2.Checked) 130 { 131 sex = this.radMan2.Tag.ToString(); 132 } 133 else 134 { 135 sex = this.radWoman2.Tag.ToString(); 136 } 137 MessageBox.Show("性别的值为:" + sex); 138 139 string sql = string.Format("select sid,sname,ssex,saddress,semail from student where ssex={0}", sex); 140 //创建数据适配器对象 141 sda = new SqlDataAdapter(sql, DBHelper.connection); 142 int result = sda.Fill(ds); 143 if (result > 0) 144 { 145 this.dgvStudentInfo.DataSource = ds.Tables[0]; 146 } 147 else 148 { 149 MessageBox.Show("无查询结果"); 150 } 151 152 153 } 154 155 private void btnSearchBySex2_Click(object sender, EventArgs e) 156 { 157 //清空数据集中表信息 158 ds.Tables.Clear(); 159 160 //根据性别查询学员信息 161 string sex; 162 if (this.cboSex1.Text != "") 163 { 164 if (this.cboSex1.Text == "男") 165 { 166 sex = "1"; 167 } 168 else 169 { 170 sex = "0"; 171 } 172 MessageBox.Show("性别的值为:" + sex); 173 } 174 else 175 { 176 MessageBox.Show("请选择性别"); 177 return; 178 } 179 180
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式