软件工程实验(5)

2019-09-01 11:45

using System;

using System.Data;

using System.Configuration; using System.Web;

using System.Web.Security; using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;

///

/// DataBase 的摘要说明 ///

public class DataBase:IDisposable {

public DataBase() {

//

// TODO: 在此处添加构造函数逻辑 // }

private SqlConnection con; //创建连接对象

#region 打开数据库连接 ///

/// 打开数据库连接. ///

private void Open() {

// 打开数据库连接 if (con == null) {

con =

SqlConnection(ConfigurationManager.AppSettings[\ }

if (con.State == System.Data.ConnectionState.Closed) con.Open(); }

#endregion

#region 关闭连接 ///

new

/// 关闭数据库连接 ///

public void Close() {

if (con != null) con.Close(); }

#endregion

#region 释放数据库连接资源 ///

/// 释放资源 ///

public void Dispose() {

// 确认连接是否已经关闭 if (con != null) {

con.Dispose(); con = null; } }

#endregion

#region 传入参数并且转换为SqlParameter类型 ///

/// 转换参数 ///

/// /// /// /// /// 新的 parameter 对象

public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value) {

return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value); }

///

/// 初始化参数值 ///

/// ///

///

/// /// /// 新的 parameter 对象

public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value) {

SqlParameter param;

if (Size > 0)

param = new SqlParameter(ParamName, DbType, Size); else

param = new SqlParameter(ParamName, DbType); param.Direction = Direction;

if (!(Direction == ParameterDirection.Output && Value == null)) param.Value = Value; return param; }

#endregion

#region 执行参数命令文本(无数据库中数据返回) ///

/// 执行命令 ///

/// /// ///

public int RunProc(string procName, SqlParameter[] prams) {

SqlCommand cmd = CreateCommand(procName, prams); cmd.ExecuteNonQuery(); this.Close();

//得到执行成功返回值

return (int)cmd.Parameters[\ }

///

/// 直接执行SQL语句 ///

/// ///

public int RunProc(string procName) {

this.Open();

SqlCommand cmd = new SqlCommand(procName, con);

cmd.ExecuteNonQuery(); this.Close(); return 1; }

#endregion

#region 执行参数命令文本(有返回值) ///

/// 执行查询命令文本,并且返回DataSet数据集 ///

/// /// /// ///

public DataSet RunProcReturn(string procName, SqlParameter[] prams, string tbName) {

SqlDataAdapter dap = CreateDataAdaper(procName, prams); DataSet ds = new DataSet(); dap.Fill(ds, tbName); this.Close();

//得到执行成功返回值 return ds; }

///

/// 执行命令文本,并且返回DataSet数据集 ///

/// /// /// DataSet

public DataSet RunProcReturn(string procName, string tbName) {

SqlDataAdapter dap = CreateDataAdaper(procName, null); DataSet ds = new DataSet(); dap.Fill(ds, tbName); this.Close();

//得到执行成功返回值 return ds; }

#endregion

#region 将命令文本添加到SqlDataAdapter ///

/// 创建一个SqlDataAdapter对象以此来执行命令文本 ///

/// /// ///

private SqlDataAdapter CreateDataAdaper(string procName, SqlParameter[] prams) {

this.Open();

SqlDataAdapter dap = new SqlDataAdapter(procName, con);

dap.SelectCommand.CommandType = CommandType.Text; //执行类型:命令文本 if (prams != null) {

foreach (SqlParameter parameter in prams)

dap.SelectCommand.Parameters.Add(parameter); }

//加入返回参数

dap.SelectCommand.Parameters.Add(new SqlParameter(\SqlDbType.Int, 4,

ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));

return dap; }

#endregion

#region 将命令文本添加到SqlCommand ///

/// 创建一个SqlCommand对象以此来执行命令文本 ///

/// /// /// 返回SqlCommand对象

private SqlCommand CreateCommand(string procName, SqlParameter[] prams) {

// 确认打开连接 this.Open();

SqlCommand cmd = new SqlCommand(procName, con);

cmd.CommandType = CommandType.Text; //执行类型:命令文本

// 依次把参数传入命令文本 if (prams != null)


软件工程实验(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:IT规划岗位竞聘演讲稿范文[精品]

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: