📄 paperdb.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
namespace ExamineSystem
{
/// <summary>
/// Summary description for PaperDB.
/// </summary>
public class PaperDB
{
public static readonly string paramPaperID = "PaperID";
public const string paramAddPaper = "PaperID_TotalMark";
private const string paramGetSingleOrDeleteRole = "Paper_RoleID";
public SqlDataReader GetPaperCountByRole(int nRoleID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLHelper.DBCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetPaperCountByRole",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter[] paramCache = SQLHelper.GetCachedParameters(paramGetSingleOrDeleteRole);
if(paramCache == null)
{
paramCache = new SqlParameter[]{
new SqlParameter("@RoleID",SqlDbType.Int,4)};
SQLHelper.CacheParameters(paramGetSingleOrDeleteRole,paramCache);
}
SQLHelper.AddMyCommandParams(myCommand,paramCache);
paramCache[0].Value = nRoleID;
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new MyException("10001","数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new MyException("10001",ex.Message,ex);
}
//返回 dr
return dr;
}
public SqlDataReader GetPaperByRole(int nRoleID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLHelper.DBCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetPaperByRole",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter[] paramCache = SQLHelper.GetCachedParameters(paramGetSingleOrDeleteRole);
if(paramCache == null)
{
paramCache = new SqlParameter[]{
new SqlParameter("@RoleID",SqlDbType.Int,4)};
SQLHelper.CacheParameters(paramGetSingleOrDeleteRole,paramCache);
}
SQLHelper.AddMyCommandParams(myCommand,paramCache);
paramCache[0].Value = nRoleID;
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new MyException("10001","数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new MyException("10001",ex.Message,ex);
}
//返回 dr
return dr;
}
public SqlDataReader GetPapers()
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLHelper.DBCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetPapers",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new MyException("10001","数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new MyException("10001",ex.Message,ex);
}
//返回 dr
return dr;
}
public SqlDataReader GetPaperKinds(int nPaperID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLHelper.DBCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetPaperKinds",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter[] paramCache = SQLHelper.GetCachedParameters(paramPaperID);
if(paramCache == null)
{
paramCache = new SqlParameter[]{
new SqlParameter("@PaperID",SqlDbType.Int,4)};
SQLHelper.CacheParameters(paramPaperID,paramCache);
}
SQLHelper.AddMyCommandParams(myCommand,paramCache);
paramCache[0].Value = nPaperID;
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new MyException("10001","数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new MyException("10001",ex.Message,ex);
}
//返回 dr
return dr;
}
public int AddPaper(int nTotalMark,int nMaxDefficult,int nMinDefficult,int nUserID,int nRoleID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLHelper.DBCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_AddPaper",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter[] paramCache = SQLHelper.GetCachedParameters(paramAddPaper);
if(paramCache == null)
{
paramCache = new SqlParameter[]{
new SqlParameter("@TotalMark",SqlDbType.Int,4),
new SqlParameter("@MinDefficult",SqlDbType.Int,4),
new SqlParameter("@MaxDefficult",SqlDbType.Int,4),
new SqlParameter("@UserID",SqlDbType.Int,4),
new SqlParameter("@RoleID",SqlDbType.Int),
new SqlParameter("@PaperID",SqlDbType.Int,4)};
SQLHelper.CacheParameters(paramAddPaper,paramCache);
}
SQLHelper.AddMyCommandParams(myCommand,paramCache);
paramCache[0].Value = nTotalMark;
paramCache[1].Value = nMinDefficult;
paramCache[2].Value = nMaxDefficult;
paramCache[3].Value = nUserID;
paramCache[4].Value = nRoleID;
paramCache[5].Direction = ParameterDirection.ReturnValue;
try
{
//打开数据库的连接
if(myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
}
catch(Exception ex)
{
throw new MyException("10001","数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
myCommand.ExecuteNonQuery();
}
catch(Exception ex)
{
throw new MyException("10001",ex.Message,ex);
}
finally
{
if (myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
}
return (int)paramCache[5].Value;
}
public void DeletePaper(int nPaperID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLHelper.DBCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_DeletePaper",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter[] paramCache = SQLHelper.GetCachedParameters(paramPaperID);
if(paramCache == null)
{
paramCache = new SqlParameter[]{
new SqlParameter("@PaperID",SqlDbType.Int,4)};
SQLHelper.CacheParameters(paramPaperID,paramCache);
}
SQLHelper.AddMyCommandParams(myCommand,paramCache);
paramCache[0].Value = nPaperID;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new MyException("10001","数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
myCommand.ExecuteNonQuery();
}
catch(Exception ex)
{
throw new MyException("10001",ex.Message,ex);
}
finally
{
if (myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
}
}
}
public class PaperKindDB
{
private const string paramGetKindByPaper = "PaperID";
private const string paramAddOrDeletePaperKind = "PaperID_KindID";
public SqlDataReader GetKindByPaper(int nPaperID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLHelper.DBCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetKindByPaper",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter[] paramCache = SQLHelper.GetCachedParameters(paramGetKindByPaper);
if(paramCache == null)
{
paramCache = new SqlParameter[]{
new SqlParameter("@PaperID",SqlDbType.Int,4)};
SQLHelper.CacheParameters(paramGetKindByPaper,paramCache);
}
SQLHelper.AddMyCommandParams(myCommand,paramCache);
paramCache[0].Value = nPaperID;
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new MyException("10001","数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new MyException("10001",ex.Message,ex);
}
//返回 dr
return dr;
}
public void AddPaperKind(int nPaperID,int nKindID,int nKindNum)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLHelper.DBCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_AddPaperKind",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter[] paramCache = SQLHelper.GetCachedParameters(paramAddOrDeletePaperKind);
if(paramCache == null)
{
paramCache = new SqlParameter[]{
new SqlParameter("@PaperID",SqlDbType.Int,4),
new SqlParameter("@KindNum",SqlDbType.Int,4),
new SqlParameter("@KindID",SqlDbType.Int,4)};
SQLHelper.CacheParameters(paramAddOrDeletePaperKind,paramCache);
}
SQLHelper.AddMyCommandParams(myCommand,paramCache);
paramCache[0].Value = nPaperID;
paramCache[1].Value = nKindNum;
paramCache[2].Value = nKindID;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new MyException("10001","数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
myCommand.ExecuteNonQuery();
}
catch(Exception ex)
{
throw new MyException("10001",ex.Message,ex);
}
finally
{
if (myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
}
}
public void DeletePaperKind(int nPaperID,int nKindID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLHelper.DBCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_DeletePaperKind",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter[] paramCache = SQLHelper.GetCachedParameters(paramAddOrDeletePaperKind);
if(paramCache == null)
{
paramCache = new SqlParameter[]{
new SqlParameter("@PaperID",SqlDbType.Int,4),
new SqlParameter("@KindID",SqlDbType.Int,4)};
SQLHelper.CacheParameters(paramAddOrDeletePaperKind,paramCache);
}
SQLHelper.AddMyCommandParams(myCommand,paramCache);
paramCache[0].Value = nPaperID;
paramCache[1].Value = nKindID;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new MyException("10001","数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
myCommand.ExecuteNonQuery();
}
catch(Exception ex)
{
throw new MyException("10001",ex.Message,ex);
}
finally
{
if (myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -