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之窗体应用程序的更多相关文章

  1. C#WinForm窗体内Panel容器中嵌入子窗体、程序主窗体设计例子

    C#WinForm父级窗体内Panel容器中嵌入子窗体.程序主窗体设计例子 在项目开发中经常遇到父级窗体嵌入子窗体所以写了一个例子程序,顺便大概划分了下界面模块和配色,不足之处还望指点 主窗体窗体采用 ...

  2. C# winform 窗体应用程序之图片上传Oracle数据库保存字段BLOB

    C# winform 窗体应用程序之图片上传Oracle数据库保存字段BLOB 我用的数据库是Oracle,就目前来看,许多数据库现在都倾向于Oracle数据库,对ORACLE数据库基本的操作也是必须 ...

  3. C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部【转载】

    这是最近在做的一个项目中提到的需求,把一个现有的窗体应用程序界面嵌入到自己开发的窗体中来,看起来就像自己开发的一样(实际上……跟自己开发的还是有一点点区别的,就是内嵌程序和宿主程序的窗口激活状态问题) ...

  4. 【转】C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部

    PS:文末的附件已更新,这次我放到博客园里面了,不会弹出广告,放心下载,O(∩_∩)O谢谢! 这是最近在做的一个项目中提到的需求,把一个现有的窗体应用程序界面嵌入到自己开发的窗体中来,看起来就像自己开 ...

  5. Java Swing快速构建窗体应用程序

    以前接触java感觉其在桌面开发上,总是不太方便,没有一个好的拖拽界面布局工具,可以快速构建窗体. 最近学习了一下NetBeans IDE 8.1,感觉其窗体设计工具还是很不错的 , 就尝试一下做了一 ...

  6. winform基础窗体设置及基础控件

    WinForm - 也叫做C/S  客户端 另:B/S是 网页端 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序 特点: 不需要联网也可以打开使用部分功能,但是现在的情况是许多功能依然需要 ...

  7. WinForm开发,窗体显示和窗体传值相关知识总结

    主窗体中代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void b ...

  8. c#winform自定义窗体,重绘标题栏,自定义控件学习

    c#winform自定义窗体,重绘标题栏 虽然现在都在说winform窗体太丑了,但是我也能尽量让桌面应用程序漂亮那么一点点话不多说,先上图 重绘标题栏先将原生窗体设置成无边框,FormBoderSt ...

  9. SNF开发平台WinForm之八-自动升级程序部署使用说明-SNF快速开发平台3.3-Spring.Net.Framework

    9.1运行效果: 9.2开发实现: 1.首先配置服务器端,把“SNFAutoUpdate2.0\服务器端部署“目录按网站程序进行发布到IIS服务器上. 2.粘贴语句,生成程序 需要调用的应用程序的Lo ...

随机推荐

  1. modal结合art-template

    内容div <div id="modal-cont"></div> 模板tpl <script id="modal-tpl" ty ...

  2. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile

    完整的错误信息: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile ( ...

  3. 堆排序 java实现

    import java.util.Arrays; /* * 思路: * 1.方法adjustDown:对于一个数组a[],针对第i个数进行向下(直到len-1)调整,使得该位置成为大顶堆 * 2.方法 ...

  4. TypeError: add() argument after * must be an iterable, not Settings的错误原因

    在抄代码的时候发现有个错误: TypeError: add() argument after * must be an iterable, not Settings 看不懂,百度才知道原因,原来是第2 ...

  5. Asp.net core 学习笔记 ( Web Api )

    asp.net core 把之前的 webapi 和 mvc 做了结合. mvc 既是 api. 但是后呢,又发现, api 确实有独到之处,所以又开了一些补助的方法. namespace Proje ...

  6. Linux中ulimit -c生成core文件()

    理解这六个shell脚本语言的功能 echo "kernel.core_pattern = /tmp/core-%e-%p-%t" >> /etc/sysctl.con ...

  7. c# DLL封装并调用

    1.封装自己的dll: a.打开visual studio - 文件 - 新建 - 项目- 类库 - 名称MyTestDll: b.右键Class1.cs - 修改为 TestDll.cs; c.在里 ...

  8. (转)C# 的 String.CompareTo、 Equals和==的比较

    String.CompareTo 语法 public int CompareTo(    string strB) 返回值 小于 0,实例小于参数 strB: 0,实例等于参数 strB: 大于 0, ...

  9. change color1

    private void Form1_Load(object sender, EventArgs e)         {             string str = "server= ...

  10. gem "ransack"(4000✨) 简单介绍

    Object-based searching:演示. git:  https://github.com/activerecord-hackery/ransack Gorails视频和我的博客记录:ht ...