📄 columndb.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ComputerWeb
{
/// <summary>
/// Summary description for ColumnDB.
/// </summary>
public class ColumnDB
{
public SqlDataReader GetColumns(int nModuleID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_GetColumns",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 SqlDataReader GetSingleColumn(int nColumnID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_GetSingleColumn",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter parameterColumnID = new SqlParameter("@ColumnID",SqlDbType.Int,4);
parameterColumnID.Value = nColumnID;
myCommand.Parameters.Add(parameterColumnID);
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 AddColumn(String sColumnName,int nModuleID,int nColumnOrder)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_AddColumn",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterColumnName = new SqlParameter("@ColumnName",SqlDbType.VarChar,32);
parameterColumnName.Value = sColumnName;
myCommand.Parameters.Add(parameterColumnName);
SqlParameter parameterModuleID = new SqlParameter("@ModuleID",SqlDbType.Int,4);
parameterModuleID.Value = nModuleID;
myCommand.Parameters.Add(parameterModuleID);
SqlParameter parameterColumnOrder = new SqlParameter("@ColumnOrder",SqlDbType.Int,4);
parameterColumnOrder.Value = nColumnOrder;
myCommand.Parameters.Add(parameterColumnOrder);
SqlParameter parameterColumnID = new SqlParameter("@ColumnID",SqlDbType.Int,4);
parameterColumnID.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(parameterColumnID);
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)parameterColumnID.Value;
}
public void UpdateColumnName(int nColumnID,String sColumnName)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_UpdateColumnName",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterColumnID = new SqlParameter("@ColumnID",SqlDbType.Int,4);
parameterColumnID.Value = nColumnID;
myCommand.Parameters.Add(parameterColumnID);
SqlParameter parameterColumnName = new SqlParameter("@ColumnName",SqlDbType.VarChar,32);
parameterColumnName.Value = sColumnName;
myCommand.Parameters.Add(parameterColumnName);
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 UpdateColumnOrder(int nModuleID,int nColumnID,String sMoveFlag)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_UpdateColumnOrder",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterModuleID = new SqlParameter("@ModuleID",SqlDbType.Int,4);
parameterModuleID.Value = nModuleID;
myCommand.Parameters.Add(parameterModuleID);
SqlParameter parameterColumnID = new SqlParameter("@ColumnID",SqlDbType.Int,4);
parameterColumnID.Value = nColumnID;
myCommand.Parameters.Add(parameterColumnID);
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 DeleteColumn(int nColumnID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_DeleteColumn",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterColumnID = new SqlParameter("@ColumnID",SqlDbType.Int,4);
parameterColumnID.Value = nColumnID;
myCommand.Parameters.Add(parameterColumnID);
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 + -