📄 leaveworddb.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
namespace Example_10_2
{
/// <summary>
/// Summary description for MessageDB.
/// </summary>
public class LeavewordDB
{
private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
public SqlDataReader GetLeavewords()
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetLeavewords",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 GetSingleLeaveword(int nLeavewordID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetSingleLeaveword",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter parameterLeavewordID = new SqlParameter("@LeavewordID",SqlDbType.Int,4);
parameterLeavewordID.Value = nLeavewordID;
myCommand.Parameters.Add(parameterLeavewordID);
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 AddLeaveword(String sTitle,String sBody)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_AddLeaveword",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterTitle = new SqlParameter("@Title",SqlDbType.VarChar,100);
parameterTitle.Value = sTitle;
myCommand.Parameters.Add(parameterTitle);
SqlParameter parameterBody = new SqlParameter("@Body",SqlDbType.VarChar,2000);
parameterBody.Value = sBody;
myCommand.Parameters.Add(parameterBody);
SqlParameter parameterLeavewordID = new SqlParameter("@LeavewordID",SqlDbType.Int,4);
parameterLeavewordID.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(parameterLeavewordID);
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)parameterLeavewordID.Value;
}
public void DeleteLeaveword(int nLeavewordID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_DeleteLeaveword",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterLeavewordID = new SqlParameter("@LeavewordID",SqlDbType.Int,4);
parameterLeavewordID.Value = nLeavewordID;
myCommand.Parameters.Add(parameterLeavewordID);
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();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -