📄 moduledb.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ComputerWeb
{
/// <summary>
/// Summary description for ModuleDB.
/// </summary>
public class ModuleDB
{
public SqlDataReader GetAllModules()
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_GetAllModules",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 GetModules(int nTabID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_GetModules",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter parameterTabID = new SqlParameter("@TabID",SqlDbType.Int,4);
parameterTabID.Value = nTabID;
myCommand.Parameters.Add(parameterTabID);
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 GetSingleModule(int nModuleID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_GetSingleModule",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter parameterModuleID = new SqlParameter("@ModuleID",SqlDbType.Int,4);
parameterModuleID.Value = nModuleID;
myCommand.Parameters.Add(parameterModuleID);
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 AddModule(String sModuleName,int nTabID,int nModuleOrder)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_AddModule",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterModuleName = new SqlParameter("@ModuleName",SqlDbType.VarChar,32);
parameterModuleName.Value = sModuleName;
myCommand.Parameters.Add(parameterModuleName);
SqlParameter parameterTabID = new SqlParameter("@TabID",SqlDbType.Int,4);
parameterTabID.Value = nTabID;
myCommand.Parameters.Add(parameterTabID);
SqlParameter parameterModuleOrder = new SqlParameter("@ModuleOrder",SqlDbType.Int,4);
parameterModuleOrder.Value = nModuleOrder;
myCommand.Parameters.Add(parameterModuleOrder);
SqlParameter parameterModuleID = new SqlParameter("@ModuleID",SqlDbType.Int,4);
parameterModuleID.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(parameterModuleID);
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();
}
}
return (int)parameterModuleID.Value;
}
public void UpdateModuleName(int nModuleID,String sModuleName)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_UpdateModuleName",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterModuleID = new SqlParameter("@ModuleID",SqlDbType.Int,4);
parameterModuleID.Value = nModuleID;
myCommand.Parameters.Add(parameterModuleID);
SqlParameter parameterModuleName = new SqlParameter("@ModuleName",SqlDbType.VarChar,32);
parameterModuleName.Value = sModuleName;
myCommand.Parameters.Add(parameterModuleName);
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 UpdateModuleOrder(int nTabID,int nModuleID,String sMoveFlag)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_UpdateModuleOrder",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterTabID = new SqlParameter("@TabID",SqlDbType.Int,4);
parameterTabID.Value = nTabID;
myCommand.Parameters.Add(parameterTabID);
SqlParameter parameterModuleID = new SqlParameter("@ModuleID",SqlDbType.Int,4);
parameterModuleID.Value = nModuleID;
myCommand.Parameters.Add(parameterModuleID);
SqlParameter parameterMoveFlag = new SqlParameter("@MoveFlag",SqlDbType.VarChar,20);
parameterMoveFlag.Value = sMoveFlag;
myCommand.Parameters.Add(parameterMoveFlag);
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 DeleteModule(int nModuleID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_DeleteModule",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterModuleID = new SqlParameter("@ModuleID",SqlDbType.Int,4);
parameterModuleID.Value = nModuleID;
myCommand.Parameters.Add(parameterModuleID);
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ec)
{
throw new MyException("10001","数据库连接失败!",ec);
}
try
{
//执行数据库的存储过程(访问数据库)
myCommand.ExecuteNonQuery();
}
catch(Exception er)
{
throw new MyException("10001",er.Message,er);
}
finally
{
if(myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -