discussiondb.cs
来自「ASP C#代码实例 适合初学人士学习使用」· CS 代码 · 共 942 行 · 第 1/2 页
CS
942 行
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Example_10_5
{
/// <summary>
/// Summary description for DisAreaDB.
/// </summary>
public class DiscussionDB
{
public static string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
public DataSet GetAreaDataSet(int nStart,int nMaxCount)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlDataAdapter da = new SqlDataAdapter("Pr_GetAreaDataSet",myConnection);
//定义访问数据库的方式为存储过程
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
da.Fill(ds,nStart,nMaxCount,"DisAreas");
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
finally
{
if (myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
}
//返回 ds
return ds;
}
public SqlDataReader GetAreaCount()
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetAreaCount",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
//返回 dr
return dr;
}
public SqlDataReader GetSingleDisArea(int nDisAreaID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetSingleDisArea",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter parameterDisAreaID = new SqlParameter("@DisAreaID",SqlDbType.Int,4);
parameterDisAreaID.Value = nDisAreaID;
myCommand.Parameters.Add(parameterDisAreaID);
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
//返回 dr
return dr;
}
public SqlDataReader GetPrevNextArea(int nDisAreaID,String sFlag)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetPrevNextArea",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter parameterDisAreaID = new SqlParameter("@DisAreaID",SqlDbType.Int,4);
parameterDisAreaID.Value = nDisAreaID;
myCommand.Parameters.Add(parameterDisAreaID);
SqlParameter parameterFlag = new SqlParameter("@Flag",SqlDbType.VarChar,10);
parameterFlag.Value = sFlag;
myCommand.Parameters.Add(parameterFlag);
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
//返回 dr
return dr;
}
public int AddDisArea(String sAreaName,int nOwnerID,int nShowOrder)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_AddDisArea",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterAreaName = new SqlParameter("@AreaName",SqlDbType.VarChar,50);
parameterAreaName.Value = sAreaName;
myCommand.Parameters.Add(parameterAreaName);
SqlParameter parameterOwnerID = new SqlParameter("@OwnerID",SqlDbType.Int,4);
parameterOwnerID.Value = nOwnerID;
myCommand.Parameters.Add(parameterOwnerID);
SqlParameter parameterShowOrder = new SqlParameter("@ShowOrder",SqlDbType.Int,4);
parameterShowOrder.Value = nShowOrder;
myCommand.Parameters.Add(parameterShowOrder);
SqlParameter parameterDisAreaID = new SqlParameter("@DisAreaID",SqlDbType.Int,4);
parameterDisAreaID.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(parameterDisAreaID);
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
myCommand.ExecuteNonQuery();
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
finally
{
if (myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
}
return (int)parameterDisAreaID.Value;
}
public void DeleteDisArea(int nDisAreaID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_DeleteDisArea",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterDisAreaID = new SqlParameter("@DisAreaID",SqlDbType.Int,4);
parameterDisAreaID.Value = nDisAreaID;
myCommand.Parameters.Add(parameterDisAreaID);
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ec)
{
throw new Exception("数据库连接失败!",ec);
}
try
{
//执行数据库的存储过程(访问数据库)
myCommand.ExecuteNonQuery();
}
catch(Exception er)
{
throw new Exception(er.Message,er);
}
finally
{
if(myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
}
}
}
/// <summary>
/// Summary description for DisTitleDB.
/// </summary>
public class TitleDB
{
public DataSet GetThreeTitle(int nAreaID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(DiscussionDB.SQLCONNECTIONSTRING);
SqlDataAdapter da = new SqlDataAdapter("Pr_GetTitles",myConnection);
//定义访问数据库的方式为存储过程
da.SelectCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter parameterAreaID = new SqlParameter("@AreaID",SqlDbType.Int,4);
parameterAreaID.Value = nAreaID;
da.SelectCommand.Parameters.Add(parameterAreaID);
DataSet ds = new DataSet();
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
//0:表示记录的开始
//3:表示结果的记录的条数
da.Fill(ds,0,3,"DisTitles");
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
finally
{
if (myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
}
//返回 ds
return ds;
}
public DataSet GetDisTitleDataSet(int nDisAreaID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(DiscussionDB.SQLCONNECTIONSTRING);
SqlDataAdapter da = new SqlDataAdapter("Pr_GetDisTitles",myConnection);
//定义访问数据库的方式为存储过程
da.SelectCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter parameterDisAreaID = new SqlParameter("@DisAreaID",SqlDbType.Int,4);
parameterDisAreaID.Value = nDisAreaID;
da.SelectCommand.Parameters.Add(parameterDisAreaID);
DataSet ds = new DataSet();
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
da.Fill(ds);
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
finally
{
if (myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
}
//返回 ds
return ds;
}
public SqlDataReader GetDisTitles(int nDisAreaID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(DiscussionDB.SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetDisTitles",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter parameterDisAreaID = new SqlParameter("@DisAreaID",SqlDbType.Int,4);
parameterDisAreaID.Value = nDisAreaID;
myCommand.Parameters.Add(parameterDisAreaID);
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
//返回 dr
return dr;
}
public SqlDataReader GetTitleCount(int nDisAreaID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(DiscussionDB.SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetTitleCount",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter parameterDisAreaID = new SqlParameter("@DisAreaID",SqlDbType.Int,4);
parameterDisAreaID.Value = nDisAreaID;
myCommand.Parameters.Add(parameterDisAreaID);
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
//返回 dr
return dr;
}
public SqlDataReader GetSingleTitle(int nTitleID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(DiscussionDB.SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetSingleTitle",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter parameterTitleID = new SqlParameter("@TitleID",SqlDbType.Int,4);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?