完整SqlHelper类详细中文注释

2019-08-03 10:38

下面是SqlHelper类,如果你对SqlHelper类不知道怎么使用,可以查看SqlHelper用法简单示例

using ...System; using System.Data; using System.Xml;

using System.Data.SqlClient; using System.Collections;

namespace Book.DAL

...{

/**////

/// SqlServer数据访问帮助类 ///

public sealed class SqlHelper ...{

私有构造函数和方法#region 私有构造函数和方法

private SqlHelper() ...{}

/**////

/// 将SqlParameter参数数组(参数值)分配给SqlCommand命令. /// 这个方法将给任何一个参数分配DBNull.Value; /// 该操作将阻止默认值的使用.

///

///

///

private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters) ...{

if( command == null ) throw new ArgumentNullException( \ if( commandParameters != null )

...{

foreach (SqlParameter p in commandParameters) ...{

if( p != null )

...{

// 检查未分配值的输出参数,将其分配以DBNull.Value.

if ( ( p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input ) &&

(p.Value == null)) ...{ p.Value = DBNull.Value; }

command.Parameters.Add(p); } } } }

/**////

/// 将DataRow类型的列值分配到SqlParameter参数数组. ///

///

/// private static void AssignParameterValues(SqlParameter[] commandParameters, DataRow dataRow) ...{

if ((commandParameters == null) || (dataRow == null)) ...{ return; }

int i = 0;

// 设置参数值

foreach(SqlParameter commandParameter in commandParameters) ...{

// 创建参数名称,如果不存在,只抛出一个异常. if( commandParameter.ParameterName == null ||

commandParameter.ParameterName.Length <= 1 ) throw new Exception(

string.Format(\请提供参数...{0}一个有效的名称{1}.\i, commandParameter.ParameterName ) );

// 从dataRow的表中获取为参数数组中数组名称的列的索引. // 如果存在和参数名称相同的列,则将列值赋给当前名称的参数. if

(dataRow.Table.Columns.IndexOf(commandParameter.ParameterName.Substring(1)) != -1) commandParameter.Value dataRow[commandParameter.ParameterName.Substring(1)]; i++; } }

/**////

/// 将一个对象数组分配给SqlParameter参数数组.

///

/// ///

=

private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues) ...{

if ((commandParameters == null) || (parameterValues == null)) ...{ return; }

// 确保对象数组个数与参数个数匹配,如果不匹配,抛出一个异常. if (commandParameters.Length != parameterValues.Length) ...{

throw new ArgumentException(\参数值个数与参数不匹配.\ }

// 给参数赋值

for (int i = 0, j = commandParameters.Length; i < j; i++)

...{

// If the current array value derives from IDbDataParameter, then assign its Value property

if (parameterValues[i] is IDbDataParameter) ...{

IDbDataParameter (IDbDataParameter)parameterValues[i]; if( paramInstance.Value == null ) ...{

paramInstance

=

commandParameters[i].Value = DBNull.Value; }

else ...{

commandParameters[i].Value = paramInstance.Value; } }

else if (parameterValues[i] == null) ...{

commandParameters[i].Value = DBNull.Value; } else

...{

commandParameters[i].Value = parameterValues[i]; } }

}

/**////

/// 预处理用户提供的命令,数据库连接/事务/命令类型/参数

///

/// ///

///

/// /// ///

///

private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection )

...{

if( command == null ) throw new ArgumentNullException( \ if( commandText == null || commandText.Length == 0 ) throw new ArgumentNullException( \

// If the provided connection is not open, we will open it if (connection.State != ConnectionState.Open) ...{

mustCloseConnection = true; connection.Open(); } else ...{

mustCloseConnection = false; }

// 给命令分配一个数据库连接. command.Connection = connection;

// 设置命令文本(存储过程名或SQL语句) command.CommandText = commandText;

// 分配事务 if (transaction != null) ...{

if( transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\ command.Transaction = transaction; }

// 设置命令类型.

command.CommandType = commandType;

// 分配命令参数

if (commandParameters != null) ...{

AttachParameters(command, commandParameters); }

return; }

#endregion 私有构造函数和方法结束

ExecuteNonQuery命令#region ExecuteNonQuery命令

/**////

/// 执行指定连接字符串,类型的SqlCommand. ///

/// /// 示例:

/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, \

///

///

/// /// /// 返回命令影响的行数 public static int ExecuteNonQuery(string commandType, string commandText) ...{

return ExecuteNonQuery(connectionString, (SqlParameter[])null); }

/**////

/// 执行指定连接字符串,类型的SqlCommand.如果没有提供参数,不返回结果. ///

/// /// 示例:

/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, \

///

///

///

connectionString,

CommandType

commandType, commandText,


完整SqlHelper类详细中文注释.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:关于印发启用2000国家坐标系实施方案的通知

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

马上注册会员

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