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 ...
随机推荐
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E. Cards Sorting 树状数组
E. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- win10,python3.6,django2.0.3,项目基本命令
1.django-admin startproject project_name(创建项目) 2.python manage.py startapp appname(创建应用) 3.python ma ...
- AQS是什么?
AQS介绍 AQS,即AbstractQueuedSynchronizer, 队列同步器,它是Java并发用来构建锁和其他同步组件的基础框架.来看下同步组件对AQS的使用: AQS是一个抽象类,主是是 ...
- 力扣(LeetCode)231. 2的幂
给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 示例 1: 输入: 1 输出: true 解释: 20 = 1 示例 2: 输入: 16 输出: true 解释: 24 = 16 示例 3: ...
- python3+虹软2.0 离线人脸识别 demo
python3+虹软2.0的所有功能整合测试完成,并对虹软所有功能进行了封装,现提供demo主要功能,1.人脸识别2.人脸特征提取3.特征比对4.特征数据存储与比对其他特征没有添加 虹软SDK下载戳这 ...
- 原生ajax的请求封装get和post
一个完整的AJAX请求包括五个步骤: 1.创建XMLHTTPRequest对象 2.使用open方法创建http请求,并设置请求地址 3.设置发送的数据,开始和服务器端交互 4.注册事件 5.获取响应 ...
- bootstrap的渲染机制
bootstrap的渲染机制. http://www.cnblogs.com/djtao/p/5942620.html 源码解析: http://www.cnblogs.com/ahole/p/588 ...
- Redis的安装及命令返回值
Linux下安装Reids : http://redis.io/download 下载最新稳定版本 wget http://download.redis.io/releases/redis-3.0.7 ...
- 常见的ORACLE语句
基本 --新建表: create table table1( id varchar(300) primary key, name varchar(200) not null); --插入数据 inse ...
- 雷林鹏分享: C# 教程
C# 教程 C# 是一个简单的.现代的.通用的.面向对象的编程语言,它是由微软(Microsoft)开发的. 本教程将告诉您基础的 C# 编程,同时将向您讲解 C# 编程语言相关的各种先进理念. 现在 ...