WinForm之窗体应用程序
WinForm之窗体应用程序
基本简单数据库操作(增删改查)
using System;
using System.Collections.Generic;
using System.Windows.Forms; namespace DataBaseOperation
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; namespace DataBaseOperation
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
} private void btnSelect_Click(object sender, EventArgs e)
{
frmSelect fs = new frmSelect();
fs.ShowDialog();
} private void btnInsert_Click(object sender, EventArgs e)
{
frmInsert fi = new frmInsert();
fi.ShowDialog();
} private void btnUpdate_Click(object sender, EventArgs e)
{
frmUpdate fu = new frmUpdate();
fu.ShowDialog();
} private void btnDelete_Click(object sender, EventArgs e)
{
frmDelete fd = new frmDelete();
fd.ShowDialog();
} private void frmMain_Load(object sender, EventArgs e)
{ this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
}
}
}
frmMain
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;// namespace DataBaseOperation
{
public partial class frmDelete : Form
{
public frmDelete()
{
InitializeComponent();
} SqlDataAdapter sda;//
DataSet ds = new DataSet();// private void frmDelete_Load(object sender, EventArgs e)
{
//窗体加载时查询表中全部信息 //1.
string sql = "select sid,sname,ssex,saddress,semail from students"; //2.
sda = new SqlDataAdapter(sql, DBHelper.connection); int result = sda.Fill(ds); if (result > )
{
this.dataGridView1.DataSource = ds.Tables[];
}
else
{
MessageBox.Show("表中无信息");
} } private void 删除选中行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedRows.Count > )
{
//1.
string id = this.dataGridView1.SelectedRows[].Cells["sid"].Value.ToString(); //2.
string sql = string.Format("delete from students where sid={0}", id); //3. try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
int result = command.ExecuteNonQuery();
if (result > )
{
MessageBox.Show("成功删除该行信息!");
}
else
{
MessageBox.Show("操作失败!");
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
else
{
MessageBox.Show("请选中要删除的行");
} //刷新控件中信息行
this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[]);
} private void btnSearchAll_Click(object sender, EventArgs e)
{
//1.清空ds中的表信息
ds.Tables.Clear(); //2.
int result = sda.Fill(ds);
if (result > )
{
this.dataGridView1.DataSource = ds.Tables[];
}
else
{
MessageBox.Show("表中无信息");
}
} private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{ } private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
{ } private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{ }
}
}
frmDelete
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;// namespace DataBaseOperation
{
public partial class frmInsert : Form
{
public frmInsert()
{
InitializeComponent();
} private void btnReset_Click(object sender, EventArgs e)
{
this.txtName.Text = "";
this.txtAddress.Text = "";
this.txtEmail.Text = "";
this.radMan.Checked = true;
this.txtName.Focus();
} private void btnInsert_Click(object sender, EventArgs e)
{
//1.获取控件中用户输入的学员信息
string name = this.txtName.Text.Trim();
string sex;
if (this.radMan.Checked)
{
sex = "";
}
else
{
sex = "";
}
MessageBox.Show(sex);
string address = this.txtAddress.Text.Trim();
string email= this.txtEmail.Text.Trim(); //2.
string sql=string.Format("insert into students (sname,ssex,saddress,semail) values('{0}',{1},'{2}','{3}')",name,sex,address,email); //3.
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
int result = command.ExecuteNonQuery();
if (result > )
{
MessageBox.Show("成功添加一条学员信息");
}
else
{
MessageBox.Show("操作失败!");
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
}
}
frmInsert
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;// namespace DataBaseOperation
{
public partial class frmSelect : Form
{
public frmSelect()
{
InitializeComponent();
} private void btnSearchName_Click(object sender, EventArgs e)
{
//根据学号查询学员姓名
string id = this.txtNum1.Text.Trim();
if (id != "")
{
string sql = string.Format("select sname from students where sid={0}", id);
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
object name = command.ExecuteScalar();
if (name != null)
{
this.lblName.Text = "此学员的姓名为:" + name.ToString();
}
else
{
MessageBox.Show("查无此人!");
this.lblName.Text = "";
this.txtNum1.Text = "";
this.txtNum1.Focus();
} }
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
else
{
MessageBox.Show("请输入学号!");
}
} private void btnSearchStudentInfo_Click(object sender, EventArgs e)
{
//根据学号查询学员信息
string id = this.txtNum2.Text.Trim();
if (id != "")
{
string sql = string.Format("select sname,ssex,saddress,semail from students where sid={0}", id);
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader sdr = command.ExecuteReader();
if (sdr.Read())
{
this.txtName1.Text = sdr["sname"].ToString();
// MessageBox.Show("性别字段的值:"+sdr["ssex"].ToString());
if (sdr["ssex"].ToString().ToLower() == "true")
{
this.radMan1.Checked = true;
}
else
{
this.radWoman1.Checked = true;
}
this.txtAddress.Text = sdr["saddress"].ToString();
this.txtEmail.Text = sdr["semail"].ToString();
}
else
{
MessageBox.Show("查无此人!");
this.txtNum2.Text = "";
this.txtNum2.Focus();
this.txtName1.Text = "";
this.radMan1.Checked = true;
this.txtAddress.Text = "";
this.txtEmail.Text = "";
} sdr.Close(); }
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
else
{
MessageBox.Show("请输入学号!");
}
} DataSet ds = new DataSet();//创建数据集对象
SqlDataAdapter sda;//声明数据适配器 private void btnSearchBySex1_Click(object sender, EventArgs e)
{
//清空数据集中表信息
ds.Tables.Clear(); //根据性别查询学员信息
string sex;
if (this.radMan2.Checked)
{
sex = this.radMan2.Tag.ToString();
}
else
{
sex = this.radWoman2.Tag.ToString();
}
MessageBox.Show("性别的值为:" + sex); string sql = string.Format("select sid,sname,ssex,saddress,semail from student where ssex={0}", sex);
//创建数据适配器对象
sda = new SqlDataAdapter(sql, DBHelper.connection);
int result = sda.Fill(ds);
if (result > )
{
this.dgvStudentInfo.DataSource = ds.Tables[];
}
else
{
MessageBox.Show("无查询结果");
} } private void btnSearchBySex2_Click(object sender, EventArgs e)
{
//清空数据集中表信息
ds.Tables.Clear(); //根据性别查询学员信息
string sex;
if (this.cboSex1.Text != "")
{
if (this.cboSex1.Text == "男")
{
sex = "";
}
else
{
sex = "";
}
MessageBox.Show("性别的值为:" + sex);
}
else
{
MessageBox.Show("请选择性别");
return;
} string sql = string.Format("select sid,sname,ssex,saddress,semail from students where ssex={0}", sex);
//创建数据适配器对象
sda = new SqlDataAdapter(sql, DBHelper.connection);
int result = sda.Fill(ds);
if (result > )
{
this.dgvStudentInfo.DataSource = ds.Tables[];
}
else
{
MessageBox.Show("无查询结果");
}
} private void btnSearchByName1_Click(object sender, EventArgs e)
{
//清空数据集中表信息
ds.Tables.Clear(); //根据学员姓名查询学员信息(模糊查询)
string name = this.txtName2.Text.Trim();
string sql = string.Format("select sid,sname,ssex,saddress,semail from students where sname like '%{0}%'", name);
//创建数据适配器对象
sda = new SqlDataAdapter(sql, DBHelper.connection);
int result = sda.Fill(ds);
if (result > )
{
this.dgvStudentInfo.DataSource = ds.Tables[];
}
else
{
MessageBox.Show("无查询结果");
} } private void btnSearchBySex3_Click(object sender, EventArgs e)
{
//清空ListView中的项
this.lstStudentInfo.Items.Clear(); //根据性别查询学员信息
string sex;
if (this.radMan3.Checked)
{
sex = this.radMan3.Tag.ToString();
}
else
{
sex = this.radWoman3.Tag.ToString();
}
MessageBox.Show("性别的值为:" + sex); string sql = string.Format("select sid,sname,ssex,saddress,semail from students where ssex={0}", sex);
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader sdr = command.ExecuteReader();
while (sdr.Read())
{
//1.
ListViewItem lvi = new ListViewItem(sdr["sid"].ToString());
//2.
if (sdr["ssex"].ToString().ToLower() == "true")
{
sex = "男";
}
else
{
sex = "女";
}
lvi.SubItems.AddRange(new string[] { sdr["sname"].ToString(), sex, sdr["saddress"].ToString(), sdr["semail"].ToString() });
//3.
this.lstStudentInfo.Items.Add(lvi);
}
//关闭sdr
sdr.Close();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
} } private void btnSearchBySex4_Click(object sender, EventArgs e)
{
//清空ListView中的项
this.lstStudentInfo.Items.Clear(); //根据性别查询学员信息
string sex;
if (this.cboSex2.Text != "")
{
if (this.cboSex2.Text == "男")
{
sex = "";
}
else
{
sex = "";
}
MessageBox.Show("性别的值为:" + sex);
}
else
{
MessageBox.Show("请选择性别");
return;
} string sql = string.Format("select sid,sname,ssex,saddress,semail from students where ssex={0}", sex);
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader sdr = command.ExecuteReader();
while (sdr.Read())
{
//1.
ListViewItem lvi = new ListViewItem(sdr["sid"].ToString());
//2.
if (sdr["ssex"].ToString().ToLower() == "true")
{
sex = "男";
}
else
{
sex = "女";
}
lvi.SubItems.AddRange(new string[] { sdr["sname"].ToString(), sex, sdr["saddress"].ToString(), sdr["semail"].ToString() });
//3.
this.lstStudentInfo.Items.Add(lvi);
}
//关闭sdr
sdr.Close();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
} private void btnSearchByName2_Click(object sender, EventArgs e)
{
//清空ListView中的项
this.lstStudentInfo.Items.Clear(); //根据学员姓名查询学员信息(模糊查询)
string name = this.txtName3.Text.Trim();
string sql = string.Format("select sid,sname,ssex,saddress,semail from students where sname like '%{0}%'", name);
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader sdr = command.ExecuteReader();
while (sdr.Read())
{
//1.
ListViewItem lvi = new ListViewItem(sdr["sid"].ToString());
//2.
string sex;
if (sdr["ssex"].ToString().ToLower() == "true")
{
sex = "男";
}
else
{
sex = "女";
}
lvi.SubItems.AddRange(new string[] { sdr["sname"].ToString(), sex, sdr["saddress"].ToString(), sdr["semail"].ToString() });
//3.将主键值写到lvi的Tag属性中
lvi.Tag = sdr["sid"].ToString(); //4.
this.lstStudentInfo.Items.Add(lvi);
}
//关闭sdr
sdr.Close();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
} private void dgvStudentInfo_CellClick(object sender, DataGridViewCellEventArgs e)
{
//单元格点击事件
if (this.dgvStudentInfo.SelectedRows.Count > )
{
string id = this.dgvStudentInfo.SelectedRows[].Cells["sid"].Value.ToString();
MessageBox.Show(id);
}
} private void lstStudentInfo_MouseClick(object sender, MouseEventArgs e)
{
//ListView控件点击事件
if (this.lstStudentInfo.SelectedItems.Count > )
{
string id = this.lstStudentInfo.SelectedItems[].Tag.ToString();
MessageBox.Show(id);
}
}
}
}
frmSelect
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;// namespace DataBaseOperation
{
public partial class frmUpdate : Form
{
public frmUpdate()
{
InitializeComponent();
} private void btnSearchStudentInfo_Click(object sender, EventArgs e)
{
//根据学号查询学员信息
string id = this.txtNum.Text.Trim();
if (id != "")
{
string sql = string.Format("select sname,ssex,saddress,semail from students where sid={0}", id);
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader sdr = command.ExecuteReader();
if (sdr.Read())
{
this.txtName.Text = sdr["sname"].ToString();
// MessageBox.Show("性别字段的值:"+sdr["ssex"].ToString());
if (sdr["ssex"].ToString().ToLower() == "true")
{
this.radMan.Checked = true;
}
else
{
this.radWoman.Checked = true;
}
this.txtAddress.Text = sdr["saddress"].ToString();
this.txtEmail.Text = sdr["semail"].ToString(); //激活或屏蔽窗体中部分控件
this.txtNum.Enabled = false;
this.txtName.Enabled = true;
this.txtAddress.Enabled = true;
this.txtEmail.Enabled = true;
this.radMan.Enabled = true;
this.radWoman.Enabled = true;
}
else
{
MessageBox.Show("查无此人!");
this.txtNum.Text = "";
this.txtNum.Focus();
this.txtName.Text = "";
this.radMan.Checked = true;
this.txtAddress.Text = "";
this.txtEmail.Text = "";
} sdr.Close(); }
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
else
{
MessageBox.Show("请输入学号!");
}
} private void btnUpdate_Click(object sender, EventArgs e)
{
//1.
string id = this.txtNum.Text.Trim();
string name = this.txtName.Text.Trim();
string sex;
if (this.radMan.Checked)
{
sex = "";
}
else
{
sex = "";
}
string address = this.txtAddress.Text.Trim();
string email = this.txtEmail.Text.Trim(); //2.
string sql = string.Format("update students set sname='{0}',ssex={1},saddress='{2}',semail='{3}' where sid={4}", name, sex, address, email, id); //3.
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
int result = command.ExecuteNonQuery();
if (result > )
{
MessageBox.Show("更新完毕!");
//激活或屏蔽窗体中部分控件
this.txtNum.Enabled = true;
this.txtName.Enabled = false;
this.txtAddress.Enabled = false;
this.txtEmail.Enabled = false;
this.radMan.Enabled = false;
this.radWoman.Enabled = false; }
else
{
MessageBox.Show("更新操作失败!");
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
}
}
frmUpdate
WinForm之窗体应用程序的更多相关文章
- C#WinForm窗体内Panel容器中嵌入子窗体、程序主窗体设计例子
C#WinForm父级窗体内Panel容器中嵌入子窗体.程序主窗体设计例子 在项目开发中经常遇到父级窗体嵌入子窗体所以写了一个例子程序,顺便大概划分了下界面模块和配色,不足之处还望指点 主窗体窗体采用 ...
- C# winform 窗体应用程序之图片上传Oracle数据库保存字段BLOB
C# winform 窗体应用程序之图片上传Oracle数据库保存字段BLOB 我用的数据库是Oracle,就目前来看,许多数据库现在都倾向于Oracle数据库,对ORACLE数据库基本的操作也是必须 ...
- C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部【转载】
这是最近在做的一个项目中提到的需求,把一个现有的窗体应用程序界面嵌入到自己开发的窗体中来,看起来就像自己开发的一样(实际上……跟自己开发的还是有一点点区别的,就是内嵌程序和宿主程序的窗口激活状态问题) ...
- 【转】C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部
PS:文末的附件已更新,这次我放到博客园里面了,不会弹出广告,放心下载,O(∩_∩)O谢谢! 这是最近在做的一个项目中提到的需求,把一个现有的窗体应用程序界面嵌入到自己开发的窗体中来,看起来就像自己开 ...
- Java Swing快速构建窗体应用程序
以前接触java感觉其在桌面开发上,总是不太方便,没有一个好的拖拽界面布局工具,可以快速构建窗体. 最近学习了一下NetBeans IDE 8.1,感觉其窗体设计工具还是很不错的 , 就尝试一下做了一 ...
- winform基础窗体设置及基础控件
WinForm - 也叫做C/S 客户端 另:B/S是 网页端 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序 特点: 不需要联网也可以打开使用部分功能,但是现在的情况是许多功能依然需要 ...
- WinForm开发,窗体显示和窗体传值相关知识总结
主窗体中代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void b ...
- c#winform自定义窗体,重绘标题栏,自定义控件学习
c#winform自定义窗体,重绘标题栏 虽然现在都在说winform窗体太丑了,但是我也能尽量让桌面应用程序漂亮那么一点点话不多说,先上图 重绘标题栏先将原生窗体设置成无边框,FormBoderSt ...
- SNF开发平台WinForm之八-自动升级程序部署使用说明-SNF快速开发平台3.3-Spring.Net.Framework
9.1运行效果: 9.2开发实现: 1.首先配置服务器端,把“SNFAutoUpdate2.0\服务器端部署“目录按网站程序进行发布到IIS服务器上. 2.粘贴语句,生成程序 需要调用的应用程序的Lo ...
随机推荐
- hdu 3836 Equivalent Sets trajan缩点
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- CAP原则
CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者不可兼得 分布式系 ...
- [osg][osgEarth][osgGA][原] EarthManipulator------基于oe的相机漫游器(浅析)
知识基础:osg漫游器基础 class OSGEARTHUTIL_EXPORT EarthManipulator : public osgGA::CameraManipulator EarthMani ...
- tips 移入悬浮功能
前景: 页面部分区域需要移入悬浮效果,当然默认的 title 也是可以的,最多只是格格不入,但是却是最为靠谱的.. 思路: 基于 jq 实例扩展 .使用立即执行函数保持功能独立. 自定义类实现功能封装 ...
- 从flask视角学习angular(一)整体对比
写在前面 前端框架完全不懂. 看着angular中文官网的英雄编辑器教程和核心知识,用偷懒的类比法,从flask django的角度 记录一下自己对angular的理解. 作为工科的武曲,自己的体会是 ...
- new int
new int, 在申请内存,定义int变量:new int (100),在申请内存,定义int变量,并初始化为100:new int[100] , 在申请内存,定义int数组变量.
- Codeforces 1071 C - Triple Flips
C - Triple Flips 思路: 小范围暴力 大范围递归构造 构造方法: solve(l, r) 表示使l 到 r 区间全变为0的方法 为了使反转次数小于等于n/3 + 12 我们只需要保证每 ...
- 算法笔记--最大流和最小割 && 最小费用最大流 && 上下界网络流
最大流: 给定指定的一个有向图,其中有两个特殊的点源S(Sources)和汇T(Sinks),每条边有指定的容量(Capacity),求满足条件的从S到T的最大流(MaxFlow). 最小割: 割是网 ...
- Python PyQt5 Pycharm 环境搭建及配置
PyQt5相关安装 python 版本 python 3.6.3 1.安装PyQt5 执行命令: pip install pyqt5 2.安装PyQt5-tools 执行命令:pip install ...
- python三级菜单实例(傻瓜版和进阶版)
程序: python三级菜单 要求: : 1.打印省.市.县三级菜单 2.可返回上一级 3.可随时退出程序 方案一:傻瓜版(其实傻瓜版考察的主要是思路!思路清楚了,那才不是傻瓜!O(∩_∩)O哈哈~) ...