利用DataGridView类编写的学生信息登记系统
来源:互联网 发布:压缩包打开软件 编辑:程序博客网 时间:2024/06/05 17:22
步骤一
数据库以及表的创建
*对于数据库张字段的数据类型的使用,方便数据在输入的时候不会出错,进而经常会使用varchar类型
*对于涉及到的数据库连接对单独放到可配置文件当中
即app文件
步骤二
利用winform窗体的进行相关控件的使用
*主要控件的使用DateGridView控件
DateGridView控件的相关的属性:row 、cells、
DateGridView控件的相关的事件:cellsClick
DateGridView控件的相关的类:DateGridViewRow
步骤三
在vs中进行编码的编写
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.SqlClient;
using System.Configuration;
namespace xiangmu
{
public partial class 数据查询 : Form
{
public 数据查询()
{
InitializeComponent();
}
private void Date()
{
#region 对数据库中的数据进行查询
//创建可配置文件,进行存放数据库的连接
string sql = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
//应用SqlConnection类建立连接的对象
SqlConnection conn = new SqlConnection(sql);
//打开数据库
conn.Open();
//应用SqlCommand类建立命令对象
//三种格式创建命令对象
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select *from information where name=@name or Mobile =@Mobile ";
/*SqlCommand comm=new SqlCommand();
comm.Connection = conn;
comm.CommandText = "select *from information where name=@name or Mobile =@Mobile ";*/
/*SqlCommand comm=new SqlCommand("select *from information where name=@name or Mobile =@Mobile",conn);*/
comm.Parameters.AddWithValue("@name", textBox1.Text.ToString());
comm.Parameters.AddWithValue("@Mobile", textBox1.Text.ToString());
//应用数据适配器创建对象,执行数据库语句,获得相关的数据
SqlDataAdapter adapter = new SqlDataAdapter(comm);
//应用数据表类,创建对象,进行数据库的数据的存放
DataTable data = new DataTable();
//将从数据库获得信息放置在数据表类中的一个对象当中,实现从数据库中显示在vs中
adapter.Fill(data);
this.dataGridView1.DataSource = data;
//数据库连接的关闭
conn.Close();
//命令的释放
comm.Dispose();
//连接的释放
#endregion
}
private void button1_Click(object sender, EventArgs e)
{
//对数据查询函数的调用
Date();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
#region 从 DataGridView控件中获得相关的信息
if (this.dataGridView1.SelectedRows.Count > 0)
{
//应用 DataGridViewRow 类创建他的对象,
DataGridViewRow row = this.dataGridView1.SelectedRows[0];
//从 DataGridView控件中获得相关的信息
this.textBox2.Text = row.Cells[0].Value.ToString();
this.textBox6.Text = row.Cells[1].Value.ToString();
string gender = row.Cells[2].Value.ToString();
if (gender == "男")
{
radioButton1.Checked = true;
}
if (gender == "女")
{
radioButton2.Checked = true;
}
this.textBox3.Text = row.Cells[3].Value.ToString();
this.comboBox1.Text = "";
this.comboBox1.Text = row.Cells[4].Value.ToString();
this.comboBox2.Text = "";
this.comboBox2.Text = row.Cells[5].Value.ToString();
this.textBox4.Text = row.Cells[6].Value.ToString();
this.textBox5.Text = row.Cells[7].Value.ToString();
#endregion
}
}
private void button2_Click(object sender, EventArgs e)
{
#region 对数据中的数据进行更改
string sql = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
SqlConnection conn = new SqlConnection(sql);
conn.Open();
SqlCommand comm = conn.CreateCommand();
string id = textBox1.Text;
//对数据进行更改
comm.CommandText = "update information set name=@name, sex=@sex ,Mobile=@Mobile,fasten=@fasten ,award=@award ,scord=@scord ,address=@address whereId=@id ";
comm.Parameters.AddWithValue("@id", textBox6.Text.ToString());
comm.Parameters.AddWithValue("@name", textBox2.Text.ToString());
if (radioButton1.Checked == true)
{
comm.Parameters.AddWithValue("@sex", radioButton1.Text.ToString());
}
if (radioButton2.Checked == true)
{
comm.Parameters.AddWithValue("@sex", radioButton2.Text.ToString());
}
comm.Parameters.AddWithValue("@Mobile", textBox3.Text.ToString());
comm.Parameters.AddWithValue("@fasten", comboBox1.Text.ToString());
comm.Parameters.AddWithValue("@award", comboBox2.Text.ToString());
comm.Parameters.AddWithValue("@scord ", textBox4.Text.ToString());
comm.Parameters.AddWithValue("@address", textBox5.Text.ToString());
comm.ExecuteNonQuery();
MessageBox.Show("更改成功");
Date();
#endregion
}
private void button3_Click(object sender, EventArgs e)
{
#region 对数据库中的数据进行删除
DataGridViewRow row = this.dataGridView1.SelectedRows[0];
string sql = "Data Source=(local);Initial Catalog=denglu;Integrated Security=True";
SqlConnection conn = new SqlConnection(sql);
conn.Open();
SqlCommand comm = conn.CreateCommand();
string id = textBox1.Text;
comm.CommandText = "delete from information where Id=@Id";
comm.Parameters.AddWithValue("@Id", row.Cells[1].Value.ToString());
comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
conn.Dispose();
MessageBox.Show("删除成功");
textBox2.Text = "";
textBox6.Text = "";
textBox3.Text = "";
comboBox1.Text = "";
comboBox2.Text = "";
textBox4.Text = "";
textBox5.Text = "";
//获取当前选中的行,返回值为DataGridViewRow
this.dataGridView1.Rows.Remove(row);
#endregion
}
}
}
在很多时候坐在电脑面前编写代码,对一个小程序的思考后,按照自己的思路将效果实现,这里的思考,有时我们会把问题思考的变得稍微难一些。其结果,我们可以看到几行代码就可以让我们看到很好的有趣的程序的诞生,所以,我们对于编程,用简单的代码编写最完美的程序。
- 利用DataGridView类编写的学生信息登记系统
- c# 学生信息登记
- 学生登记系统
- 学会利用java编写学生信息
- VC中对列表框的操作(学生信息登记)
- 编写一个学生管理系统程序,用来记录学生的信息(包括姓名、年龄、性别、学号、分数),提供增加、删除、查询学生信息的入口
- Java编程---2、(补充)用数组编写简易的学生管理系统-----定义学生类
- 第十三章 带参方法 模拟学生信息登记,实现查询,录入,修改学生信息
- 利用jsshell 实现的学生信息管理系统
- 关于信息登记的一点心得
- 学生信息小系统
- 根据学生信息的XML文档编写DTD
- 两款用C语言编写的学生信息成绩管理系统
- 班级信息的学生类
- 基于手工登记的被动访客系统
- c语言使用链表编写一个可以实现班级学生管理系统,增加,删除,修改学生信息
- java中用集合编写简单的学生信息管理系统
- 用链表编写学生管理系统(涵盖文件的输入输出)
- python 类
- Java中用Cache
- STL容器总结
- 在2013年营销是什么?
- Android实现 ScrollView + ListView无滚动条滚动
- 利用DataGridView类编写的学生信息登记系统
- 多播、组播
- 送一首诗给心急的、望子成龙的父母们——《牵一只蜗牛去散步》
- 20130911,微软9月11日发布13个安全补丁
- many-to-one单向关联映射
- struts2 +jquey uploadify3.2 实现多文件上传,可预览、删除、排序
- 不写了,转载文章吧
- 在PHP语言中使用JSON
- tail 命令