使用C#制作MySchool项目代码
2009-09-17 21:10
441 查看
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 MySchool
{
/// <summary>
/// 查询学员用户窗体
/// </summary>
public partial class SearchStudentForm : Form
{
public SearchStudentForm()
{
InitializeComponent();
}
// 单击“取消”按钮时,关闭窗体
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
// 查找用户
private void btnSearch_Click(object sender, EventArgs e)
{
if (txtLoginId.Text == "") // 必须输入用户名才能查找
{
MessageBox.Show("请输入用户名", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtLoginId.Focus();
}
else // 查找用户
{
FillListView(); // 填充列表视图
}
}
// 删除用户
private void tsmiDelete_Click(object sender, EventArgs e)
{
// 确保用户选择了一个学员才执行删除操作
if (lvStudent.SelectedItems.Count == 0)
{
MessageBox.Show("您没有选择任何用户", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
// 为防止误删除,要先询问
DialogResult choice = MessageBox.Show("确定要删除该用户吗?","操作警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
// 如果确定删除,则执行删除操作
if (choice == DialogResult.Yes)
{
// 删除用sql语句
string sql = string.Format("DELETE FROM Student WHERE StudentID={0}",(int)lvStudent.SelectedItems[0].Tag);
// 创建Command对象
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
int result = 0; // 操作结果
try
{
DBHelper.connection.Open(); // 打开数据库连接
result = command.ExecuteNonQuery(); // 执行命令
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close(); // 关闭数据库连接
}
if (result < 1) // 操作失败
{
MessageBox.Show("删除失败!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else // 操作成功
{
MessageBox.Show("删除成功!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
FillListView(); // 重新查询信息填充列表视图
}
}
}
}
// 将用户状态修改为活动的
private void tsmiActive_Click(object sender, EventArgs e)
{
// 确保用户选择了一个学员才执行修改操作
if (lvStudent.SelectedItems.Count == 0)
{
MessageBox.Show("您没有选择任何用户", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
// 删除用sql语句
string sql = string.Format("Update Student SET UserStateId=1 WHERE StudentID={0}", (int)lvStudent.SelectedItems[0].Tag);
int result = 0; // 操作结果
try
{
// 创建Command对象
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open(); // 打开数据库连接
result = command.ExecuteNonQuery(); // 执行命令
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close(); // 关闭数据库连接
}
if (result < 1) // 操作失败
{
MessageBox.Show("修改失败!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else // 操作成功
{
MessageBox.Show("修改成功!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
FillListView(); // 重新查询信息填充列表视图
}
}
}
// 将用户状态修改为非活动的
private void tsmiInActive_Click(object sender, EventArgs e)
{
// 确保用户选择了一个学员才执行修改操作
if (lvStudent.SelectedItems.Count == 0)
{
MessageBox.Show("您没有选择任何用户", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
// 删除用sql语句
string sql = string.Format("Update Student SET UserStateId=0 WHERE StudentID={0}", (int)lvStudent.SelectedItems[0].Tag);
int result = 0; // 操作结果
try
{
// 创建Command对象
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open(); // 打开数据库连接
result = command.ExecuteNonQuery(); // 执行命令
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close(); // 关闭数据库连接
}
if (result < 1) // 操作失败
{
MessageBox.Show("修改失败!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else // 操作成功
{
MessageBox.Show("修改成功!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
FillListView(); // 重新查询信息填充列表视图
}
}
}
/// <summary>
/// 根据查询条件,从数据库中读取信息,填充列表视图
/// </summary>
private void FillListView()
{
string loginId; // 用户名
string studentName; // 姓名
string studentNO; // 学号
int userStateId; // 用户状态Id
string userState; // 用户状态
// 查找学员用户的sql语句
string sql = string.Format(
"SELECT StudentID,LoginId,StudentNO,StudentName,UserStateId FROM Student WHERE LoginId like '%{0}%'", txtLoginId.Text
);
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection); // 构造Command对象
DBHelper.connection.Open(); // 打开数据库连接
SqlDataReader dataReader = command.ExecuteReader(); // 执行查询用户命令
lvStudent.Items.Clear(); // 清除ListView中的所有项
// 如果结果中没有数据行,就弹出提示,
if (!dataReader.HasRows)
{
MessageBox.Show("抱歉,没有您要找的用户!", "结果提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
// 将查到的结果循环写到ListView中
while (dataReader.Read())
{
// 将从数据库中读取到的用户名、姓名、学号、用户状态赋给相应的变量
loginId = (string)dataReader["LoginId"];
studentName = (string)dataReader["StudentName"];
studentNO = (string)dataReader["StudentNO"];
userStateId = (int)dataReader["UserStateId"];
userState = (userStateId == 1) ? "活动" : "非活动";
ListViewItem lviStudent = new ListViewItem(loginId);//创建一个ListView项
lviStudent.Tag = (int)dataReader["StudentID"]; // 将ID放在Tag中
lvStudent.Items.Add(lviStudent); // 向ListView中添加一个新项
lviStudent.SubItems.AddRange(new string[] { studentName, studentNO, userState });//向当前项中添加子项
}
}
dataReader.Close(); //关闭dataReader
}
catch (Exception ex)
{
MessageBox.Show("查询数据库出错!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close(); // 关闭数据库连接
}
}
}
}
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 MySchool
{
/// <summary>
/// 查询学员用户窗体
/// </summary>
public partial class SearchStudentForm : Form
{
public SearchStudentForm()
{
InitializeComponent();
}
// 单击“取消”按钮时,关闭窗体
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
// 查找用户
private void btnSearch_Click(object sender, EventArgs e)
{
if (txtLoginId.Text == "") // 必须输入用户名才能查找
{
MessageBox.Show("请输入用户名", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtLoginId.Focus();
}
else // 查找用户
{
FillListView(); // 填充列表视图
}
}
// 删除用户
private void tsmiDelete_Click(object sender, EventArgs e)
{
// 确保用户选择了一个学员才执行删除操作
if (lvStudent.SelectedItems.Count == 0)
{
MessageBox.Show("您没有选择任何用户", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
// 为防止误删除,要先询问
DialogResult choice = MessageBox.Show("确定要删除该用户吗?","操作警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
// 如果确定删除,则执行删除操作
if (choice == DialogResult.Yes)
{
// 删除用sql语句
string sql = string.Format("DELETE FROM Student WHERE StudentID={0}",(int)lvStudent.SelectedItems[0].Tag);
// 创建Command对象
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
int result = 0; // 操作结果
try
{
DBHelper.connection.Open(); // 打开数据库连接
result = command.ExecuteNonQuery(); // 执行命令
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close(); // 关闭数据库连接
}
if (result < 1) // 操作失败
{
MessageBox.Show("删除失败!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else // 操作成功
{
MessageBox.Show("删除成功!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
FillListView(); // 重新查询信息填充列表视图
}
}
}
}
// 将用户状态修改为活动的
private void tsmiActive_Click(object sender, EventArgs e)
{
// 确保用户选择了一个学员才执行修改操作
if (lvStudent.SelectedItems.Count == 0)
{
MessageBox.Show("您没有选择任何用户", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
// 删除用sql语句
string sql = string.Format("Update Student SET UserStateId=1 WHERE StudentID={0}", (int)lvStudent.SelectedItems[0].Tag);
int result = 0; // 操作结果
try
{
// 创建Command对象
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open(); // 打开数据库连接
result = command.ExecuteNonQuery(); // 执行命令
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close(); // 关闭数据库连接
}
if (result < 1) // 操作失败
{
MessageBox.Show("修改失败!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else // 操作成功
{
MessageBox.Show("修改成功!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
FillListView(); // 重新查询信息填充列表视图
}
}
}
// 将用户状态修改为非活动的
private void tsmiInActive_Click(object sender, EventArgs e)
{
// 确保用户选择了一个学员才执行修改操作
if (lvStudent.SelectedItems.Count == 0)
{
MessageBox.Show("您没有选择任何用户", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
// 删除用sql语句
string sql = string.Format("Update Student SET UserStateId=0 WHERE StudentID={0}", (int)lvStudent.SelectedItems[0].Tag);
int result = 0; // 操作结果
try
{
// 创建Command对象
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open(); // 打开数据库连接
result = command.ExecuteNonQuery(); // 执行命令
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.connection.Close(); // 关闭数据库连接
}
if (result < 1) // 操作失败
{
MessageBox.Show("修改失败!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else // 操作成功
{
MessageBox.Show("修改成功!", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
FillListView(); // 重新查询信息填充列表视图
}
}
}
/// <summary>
/// 根据查询条件,从数据库中读取信息,填充列表视图
/// </summary>
private void FillListView()
{
string loginId; // 用户名
string studentName; // 姓名
string studentNO; // 学号
int userStateId; // 用户状态Id
string userState; // 用户状态
// 查找学员用户的sql语句
string sql = string.Format(
"SELECT StudentID,LoginId,StudentNO,StudentName,UserStateId FROM Student WHERE LoginId like '%{0}%'", txtLoginId.Text
);
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection); // 构造Command对象
DBHelper.connection.Open(); // 打开数据库连接
SqlDataReader dataReader = command.ExecuteReader(); // 执行查询用户命令
lvStudent.Items.Clear(); // 清除ListView中的所有项
// 如果结果中没有数据行,就弹出提示,
if (!dataReader.HasRows)
{
MessageBox.Show("抱歉,没有您要找的用户!", "结果提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
// 将查到的结果循环写到ListView中
while (dataReader.Read())
{
// 将从数据库中读取到的用户名、姓名、学号、用户状态赋给相应的变量
loginId = (string)dataReader["LoginId"];
studentName = (string)dataReader["StudentName"];
studentNO = (string)dataReader["StudentNO"];
userStateId = (int)dataReader["UserStateId"];
userState = (userStateId == 1) ? "活动" : "非活动";
ListViewItem lviStudent = new ListViewItem(loginId);//创建一个ListView项
lviStudent.Tag = (int)dataReader["StudentID"]; // 将ID放在Tag中
lvStudent.Items.Add(lviStudent); // 向ListView中添加一个新项
lviStudent.SubItems.AddRange(new string[] { studentName, studentNO, userState });//向当前项中添加子项
}
}
dataReader.Close(); //关闭dataReader
}
catch (Exception ex)
{
MessageBox.Show("查询数据库出错!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close(); // 关闭数据库连接
}
}
}
}
相关文章推荐
- 在VC++项目中使用.Net库,经过简单转换,可以直接使用C#代码
- C#新手入门代码 VS2015使用技巧 dll之新建类库,添加类库至现有项目,实例化类库中的类
- 使用C# 3.0编译器编译 Asp.Net 项目代码
- c# 制作自定义控件并生成dll文件旷展到其他项目使用
- C语言项目中头文件/代码文件的组织问题(使用VC6.0)
- C#制作鹰眼的详细全过程(带注释)实例代码
- 在C#中使用C++的DLL,并且在DLL中包含有opencv的代码,通过“障眼法”操作
- 通栏导航栏的制作,综合使用CSS属性,代码不超过30行
- 通过代码学习C#&.NET——委托使用(通用类设计)
- C#使用System.Speech制作语音提示功能
- 使用Git上传项目代码到github
- vs2008 使用c#写Activex插件,并在website中使用加载,并将cab制作为数字签名
- WIFI项目--Android Studio的.gitignore以及使用git提交拉取代码
- (转)使用C#尽可能以最少的代码完成多层次的软件配置(基于PropertyGrid控件)
- 如何在C#项目中使用NHibernate
- asp.net(c#) 使用Rex正则来生成字符串数组的代码
- 使用pyinstaller工具打包python项目代码生成exe文件
- [C#]分享一个以前的项目使用的DataBaseAccess类
- .net winform下 C#/VB.NET项目代码行数统计(2009-6-25新版本)
- C#分析数据库结构,使用XSL模板自动生成代码 - 清清月儿 .NET万花筒 Asp.net技术 Asp.net教程 Asp.net源码 Asp.net基础 Asp.net控件 Asp.net入门 - CSDNBlog