高校特种设备管理系统完整项目的源代码,一个SQLHelper类和9个窗体,有些地方重复,不要觉得太多哦!
一、数据连接、断开及操纵类SQLHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace SpecialEquipmentManagement
{
class SQLHelper
{
private static string lastError;//最近一次异常信息,定义为静态字段,方便被静态方法引用
private SqlConnection conn;
/// <summary>
/// 无参构造函数
/// </summary>
public SQLHelper()
{
string str = @"Data Source=127.0.0.1;Initial Catalog=DBSpeEquManagement;Integrated Security=true";
this.conn = new SqlConnection();//实例化一个数据连接对象
this.conn.ConnectionString = str;
}
public void OpenDB()
{
this.conn.Open();
}
public void CloseDB ()
{
this.conn.Close();
}
/// <summary>
/// 最近一次异常信息
/// </summary>
public static string LastError
{
get { return lastError; }
//set { lastError = value; }
}
public SqlConnection Conn
{
get { return conn; }
set { conn = value; }
}
/// <summary>
/// 通用异常处理函数
/// </summary>
/// <param name="e">需要处理的异常</param>
private static void HandleException(Exception e)
{
if (e is SqlException)
{
lastError = string.Format("在打开连接时出现连接级别的错误:{0}", e.Message);
}
else if (e is InvalidOperationException)
{
lastError = e.Message;
}
else if (e is DBConcurrencyException)
{
//数据并发异常
lastError = string.Format("尝试执行 INSERT、UPDATE 或 DELETE 语句,但没有记录受到影响:{0}", e.Message);
}
else
{
lastError = string.Format("未预料的异常:{0}", e.Message);
}
}
/// <summary>
/// 无返回值的SQL语句执行
/// </summary>
/// <param name="sql">欲执行的SQL语句</param>
/// <param name="parameters">参数集合</param>
/// <returns>影响记录的行数</returns>
public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
{
string str = @"Data Source=127.0.0.1;Initial Catalog=DBSpeEquManagement;Integrated Security=true";
using (SqlConnection conn = new SqlConnection(str))
{
try
{
//Open异常捕获
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
foreach (SqlParameter param in parameters)
{
cmd.Parameters.Add(param);
}
return cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
HandleException(ex);
MessageBox.Show(SQLHelper.LastError);
}
}
return -1;
}
/// <summary>
/// 获得离线数据集合
/// </summary>
/// <param name="sql">欲执行的SQL语句</param>
/// <param name="parameters">与SQL相关的参数</param>
/// <returns>返回查询结果集合</returns>
public static DataSet ExecSQLByDataSet(string sql, params SqlParameter[] parameters)
{
//parameters是可变的数组参数
string str = @"Data Source=127.0.0.1;Initial Catalog=DBSpeEquManagement;Integrated Security=true";
using (SqlConnection conn = new SqlConnection(str))
{
try
{
conn.Open();//打开数据库连接
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
foreach (SqlParameter parameter in parameters)
{
cmd.Parameters.Add(parameter);
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();//有必要关闭数据库连接的吗?
return ds;
}
}
catch (Exception ex)
{
HandleException(ex);
MessageBox.Show(SQLHelper.LastError);
}
}
return null;
}
/// <summary>
/// 将本地修改的结果集提交至服务器
/// </summary>
/// <param name="ds">已修改的结果集合</param>
public static void UpdateByDataSet(DataSet ds)
{
string str = @"Data Source=127.0.0.1;Initial Catalog=DBSpeEquManagement;Integrated Security=true";
using (SqlConnection conn = new SqlConnection(str))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
new SqlCommandBuilder(da);
da.ContinueUpdateOnError = true;
try
{
da.Update(ds);
}
catch (Exception ex)
{
HandleException(ex);
MessageBox.Show(SQLHelper.LastError);
}
}
}
}
}
}
二、用户注册模块
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;
namespace SpecialEquipmentManagement
{
public partial class GUIRegisterUser : Form
{
public GUIRegisterUser()
{
InitializeComponent();
//设置Lable控件的背景透明,即去掉背景颜色
label1.BackColor = System.Drawing.Color.Transparent;
label2.BackColor = System.Drawing.Color.Transparent;
lblUserName.BackColor = System.Drawing.Color.Transparent;
lblPassword.BackColor = System.Drawing.Color.Transparent;
}
SQLHelper sqlHelper = new SQLHelper();
private void btnEnter_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnRegister_Click(object sender, EventArgs e)
{
if (txtUserName.Text == "")
{
MessageBox.Show("用户名不能为空!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;//txtUserName内容为空,结束该事件
}
else if (txtPassword.Text == "")
{
MessageBox.Show("密码不能为空!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (txtPassword.Text.Length != 8)
{
MessageBox.Show("密码必须为8位!");
txtPassword.Focus();//获得焦点
txtPassword.SelectAll();//全选文本中的内容
return;
}
else if (txtPassword.Text != txtCheckPassword.Text)
{
MessageBox.Show("两次输入密码不一致!请再次确认密码!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtPassword.Focus();//txtPassword获得焦点
txtPassword.SelectAll();//全选txtPassword文本内容,便于修改
return;
}
try
{
string sql = "insert into T_SystemUser values ('" + txtUserName.Text + "','" + txtPassword.Text + "')";
//SqlCommand cmd = new SqlCommand(sql, sqlHelper.Conn);
SQLHelper.ExecuteNonQuery(sql);
MessageBox.Show("恭喜您!注册成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 键按下时触发的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.PasswordChar = '*';
//只能输入数字、字母和退格键
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar < 65 || e.KeyChar > 90) && (e.KeyChar < 97 || e.KeyChar > 122) && e.KeyChar != 8)
{
e.Handled = true;
}
}
private void GUIRegisterUser_Load(object sender, EventArgs e)
{
//窗体初始化时,在文本框中显示输入提示信息
txtUserName.Text = "<请输入真实姓名>";
txtPassword.Text = "<8位数字或字母>";
}
private void txtUserName_MouseDown(object sender, MouseEventArgs e)
{
//鼠标按下时,清空文本
if (txtUserName.Text == "<请输入真实姓名>")
{
txtUserName.Text = "";
}
}
private void txtUserName_Leave(object sender, EventArgs e)
{
//光标离开时,如没有输入数据,则显示提示信息
if (txtUserName.Text == "")
{
txtUserName.Text = "<请输入真实姓名>";
}
}
private void txtPassword_Leave(object sender, EventArgs e)
{
//光标离开时,如没有输入数据,则显示提示信息
if (txtPassword.Text == "")
{
txtPassword.Text = "<8位数字或字母>";
}
}
private void txtPassword_MouseDown(object sender, MouseEventArgs e)
{
//鼠标按下时,清空文本
if (txtPassword.Text == "<8位数字或字母>")
{
txtPassword.Text = "";
}
}
}
}
三、用户登录模块
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Lin