开发环境
VS2012+sql server2014
需求分析
为了适应酒店管理发展的要求,目标系统必须具有操作简单、可维护等特点。酒店综合管理系统采用模块化设计,这为酒店的权限管理奠定了一定的基础。根据业务流程分析,设计了八个功能模块,包括前台服务、CRM管理、房态中心、休闲服务、财务管理、报表中心、报表中心、调度中心以及系统设置。每个模块对应于不同的管理机构,各模块之间又有错综复杂的数据联系。具体的系统功能模块划分如图所示:
数据库部分
采用sql server2014
部分表:
实现部分
代码导航:
页面展示:
初始化界面
登录页面:
主页面:
入住、退房、换房界面:
一键锁屏换肤:
便签(具有新建,打印,打开,自记录创建者等功能)
员工、订单、会员管理等功能:
更多功能:
打印输出报表,统计收支等等。
代码
1.数据库操作类SqlCompose
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
public class SqlCompose
{
#region 构造函数
///
/// 构造函数
///
///
public SqlCompose()
{
this.connStr = "Server=localhost ;DataBase= your database name; user id=your user id ;password=your password";
}
#endregion
#region 变量
//数据库连接对象
private SqlConnection _connection = null;
//数据库连接字符串
private string connStr = string.Empty;
#endregion
#region 属性
///
/// 数据库连接对象
///
private SqlConnection Connnection
{
get
{
if (this._connection == null || this._connection.State == ConnectionState.Broken || this._connection.State == ConnectionState.Closed)
{
this._connection = new SqlConnection(connStr);
}
return this._connection;
}
}
#endregion
#region 方法
#region BuildQueryCommand
///
/// 创建Command对象
///
/// sql语句或存储过程名称
/// 是否是存储过程
/// 输入参数集合
/// 输出参数集合
///
public SqlCommand BuildQueryCommand(string sqlOrprocedureName, bool isProcedure, object[] inParameters, object[] outParameters)
{
SqlCommand command = new SqlCommand(sqlOrprocedureName, this.Connnection);
command.CommandType = isProcedure ? CommandType.StoredProcedure : CommandType.Text;
if (isProcedure && ((inParameters != null && inParameters.Length > 0) || (outParameters != null && outParameters.Length > 0)))
{
//获取存储过程参数,获取参数顺序为,存储过程执行返回值ReturnValue>输入参数Input>输入输出参数InputOutput>输出参数Output。
//其中Output为纯输出参数,不用传参,但sqlserver存储过程里定义的输出参数,在C#中一般为InputOutput类型,除非存储过程中定义的是返回值而非输出参数。
command.Connection.Open();
SqlCommandBuilder.DeriveParameters(command);
command.Connection.Close();
try
{
//存储过程参数赋值
for (int i = 1; i < command.Parameters.Count; i++)
{
if (inParameters != null && inParameters.Length > 0 && i <= inParameters.Length)
{
command.Parameters[i].Value = inParameters[i - 1];
}
else if (outParameters != null && outParameters.Length > 0)
{
command.Parameters[i].Value = outParameters[i - (inParameters == null ? 0 : inParameters.Length) - 1];
}
}
}
catch
{
throw new Exception("传入的存储过程参数个数与存储过程实际参数个数不符");
}
}
return command;
}
#endregion
#region ExecuteSqlQuery
///
/// 执行SQL查询
///
/// sql语句
///
public DataSet ExecuteSqlQuery(string sql)
{
SqlDataAdapter oladpt = new SqlDataAdapter(this.BuildQueryCommand(sql, false, null, null));
DataSet dsData = new DataSet();
oladpt.Fill(dsData);
return dsData;
}
#endregion
#region ExecuteSqlNonQuery
///
/// 执行无返回的SQL语句,如插入、更新
///
/// sql语句
///
public bool ExecuteSqlNonQuery(string sql)
{
SqlCommand command = this.BuildQueryCommand(sql, false, null, null);
command.Connection.Open();
using (SqlTransaction trans = command.Connection.BeginTransaction())
{
try
{
command.Transaction = trans;
int result = command.ExecuteNonQuery();
trans.Commit();
return result > 0 ? true : false;
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
command.Connection.Close();
}
}
}
#endregion
#region ExecuteSqlNonQuery
///
/// 执行无返回的SQL语句,如插入、更新
///
/// sql语句列表
///
public bool ExecuteSqlNonQuery(ArrayList strSqlList)
{
SqlCommand command = this.BuildQueryCommand(null, false, null, null);
command.Connection.Open();
using (SqlTransaction trans = command.Connection.BeginTransaction())
{
try
{
command.Transaction = trans;
for (int i = 0; i < strSqlList.Count; i++)
{
command.CommandText = strSqlList[i].ToString();
command.ExecuteNonQuery();
}
trans.Commit();
return true;
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
command.Connection.Close();
}
}
}
#endregion
#region ExecuteSqlScalar
///
/// 返回SQL的首行数据
///
/// sql语句
///
public string ExecuteSqlScalar(string sql)
{
SqlCommand command = this.BuildQueryCommand(sql, false, null, null);
command.Connection.Open();
using (SqlTransaction trans = command.Connection.BeginTransaction())
{
try
{
command.Transaction = trans;
string result = command.ExecuteScalar().ToString();
trans.Commit();
return result;
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
command.Connection.Close();
}
}
}
#endregion
#region ExecuteProcedureQuery
///
/// 执行有返回结果集的存储过程
///
/// 存储过程名
/// 输入参数集合
///
public DataSet ExecuteProcedureQuery(string procedureName, object[] inPutParameters)
{
SqlDataAdapter oladpt = new SqlDataAdapter(this.BuildQueryCommand(procedureName, true, inPutParameters, null));
DataSet result = new DataSet();
oladpt.Fill(result);
return result;
}
#endregion
#region ExecuteProcedureQuery
///
/// 执行有返回结果集的存储过程
///
/// 存储过程名
///
public DataSet ExecuteProcedureQuery(string procedureName)
{
return this.ExecuteProcedureQuery(procedureName, null);
}
#endregion
#region ExecuteProcedureNonQuery
///
/// 执行无返回结果集的存储过程
///
/// 存储过程名称
/// 输入参数集合
///
public bool ExecuteProcedureNonQuery(string procedureName, object[] inPutParameters)
{
SqlCommand command = this.BuildQueryCommand(procedureName, true, inPutParameters, null);
command.Connection.Open();
using (SqlTransaction trans = command.Connection.BeginTransaction())
{
try
{
command.Transaction = trans;
command.ExecuteNonQuery();
trans.Commit();
return true;
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
command.Connection.Close();
}
}
}
#endregion
#region ExecuteProcedureNonQuery
///
/// 执行无返回结果集的存储过程
///
/// 存储过程名
///
public bool ExecuteProcedureNonQuery(string procedureName)
{
return this.ExecuteProcedureNonQuery(procedureName, null);
}
#endregion
#region ExecuteProcedureScalar
///
/// 执行存储过程返回首行数据
///
/// 存储过程名
/// 输入参数集合
///
public string ExecuteProcedureScalar(string procedureName, object[] inPutParameters)
{
SqlCommand command = this.BuildQueryCommand(procedureName, true, inPutParameters, null);
command.Connection.Open();
using (SqlTransaction trans = command.Connection.BeginTransaction())
{
try
{
command.Transaction = trans;
string result = command.ExecuteScalar().ToString();
trans.Commit();
return result;
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
command.Connection.Close();
}
}
}
#endregion
#region ExecuteProcedureScalar
///
/// 执行存储过程返回首行数据
///
/// 存储过程名
///
public string ExecuteProcedureScalar(string procedureName)
{
return this.ExecuteProcedureScalar(procedureName, null);
}
#endregion
#region ExecuteProcedureOutPut
///
/// 执行存储过程返回输出参数
///
/// 存储过程名称
/// 输入参数集合
/// 输出参数集合
///
public bool ExecuteProcedureOutPut(string procedureName, object[] inPutParameters, ref object[] outPutParameters)
{
SqlCommand command = this.BuildQueryCommand(procedureName, true, inPutParameters, outPutParameters);
command.Connection.Open();
using (SqlTransaction trans = command.Connection.BeginTransaction())
{
try
{
command.Transaction = trans;
command.ExecuteNonQuery();
trans.Commit();
if (outPutParameters != null && outPutParameters.Length > 0)
{
int index = 0;
foreach (SqlParameter spt in command.Parameters)
{
if (spt.Direction == ParameterDirection.InputOutput)
{
outPutParameters[index] = spt.Value;
index++;
}
}
}
else
{
outPutParameters = null;
int outCount = 0;
foreach (SqlParameter spt in command.Parameters)
{
if (spt.Direction == ParameterDirection.Output)
{
outCount++;
}
}
if (outCount > 0)
{
int index = 0;
outPutParameters = new object[outCount];
foreach (SqlParameter spt in command.Parameters)
{
if (spt.Direction == ParameterDirection.Output)
{
outPutParameters[index] = spt.Value;
index++;
}
}
}
}
return true;
}
catch (Exception ex)
{
trans.Rollback();
outPutParameters = null;
throw ex;
}
finally
{
command.Connection.Close();
}
}
}
#endregion
#region ExecuteProcedureOutPut
///
/// 执行存储过程返回输出参数
///
/// 存储过程名称
/// 输出参数集合
///
public bool ExecuteProcedureOutPut(string procedureName, ref object[] outPutParameters)
{
return this.ExecuteProcedureOutPut(procedureName, null, ref outPutParameters);
}
#endregion
#region ExecuteBatchInsertData
///
/// 批量插入数据,在大批量数据操作时,是DataAdapter和Command批量操作效率的30倍以上
///
/// 需要插入数据的表名
/// 需要插入的数据TABLE
///
public bool ExecuteBatchInsertData(string tableName, DataTable sourceDable)
{
//获取需要插入数据的表结构,非数据
string sql = "SELECT * FROM " + tableName + " WHERE 1 = 0";
DataTable dt = this.ExecuteSqlQuery(sql).Tables[0];
this.Connnection.Open();
using (SqlTransaction sqlTrans = this.Connnection.BeginTransaction())
{
try
{
using (SqlBulkCopy copy = new SqlBulkCopy(this.Connnection, SqlBulkCopyOptions.Default, sqlTrans))
{
//匹配表名
copy.DestinationTableName = tableName;
//匹配字段名,此方法是区分大小写的,所以获取的表结构大小写要与数据库完全一致
for (int i = 0; i < dt.Columns.Count; i++)
{
for (int j = 0; j < sourceDable.Columns.Count; j++)
{
if (dt.Columns[i].ColumnName.Trim().ToUpper() == sourceDable.Columns[j].ColumnName.Trim().ToUpper())
{
copy.ColumnMappings.Add(sourceDable.Columns[j].ColumnName, dt.Columns[i].ColumnName);
}
}
}
//插入数据
copy.WriteToServer(sourceDable);
}
sqlTrans.Commit();
return true;
}
catch (Exception ex)
{
sqlTrans.Rollback();
throw ex;
}
finally
{
this.Connnection.Close();
}
}
}
#endregion
#endregion
}
2.其余代码就不贴出来啦。
总结:项目虽小,但从中也学到不少winform开发的知识。
欢迎私聊讨论.
email:[email protected]