完整SqlHelper类详细中文注释
更新时间:2024-02-03 11:13:01 阅读量: 教育文库 文档下载
下面是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, \
///
///
/// /// ///
return ExecuteNonQuery(connectionString, (SqlParameter[])null); }
/**////
/// 执行指定连接字符串,类型的SqlCommand.如果没有提供参数,不返回结果. /// ///
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, \
///
///
///
connectionString,
CommandType
commandType, commandText,
/// /// ///
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) ...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \
using (SqlConnection connection = new SqlConnection(connectionString)) ...{
connection.Open();
return commandParameters); }
}
/**////
/// 执行指定连接字符串的存储过程,将对象数组的值赋给存储过程参数, /// 此方法需要在参数缓存方法中探索参数并生成参数. /// ///
/// 这个方法没有提供访问输出参数和返回值. /// 示例:
/// int result = ExecuteNonQuery(connString, \ ///
/// /// ///
ExecuteNonQuery(connection,
commandType,
commandText,
///
public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \ if(
spName
==
null
||
spName.Length
==
0
)
throw
new
ArgumentNullException( \
// 如果存在参数值
if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
// 从探索存储过程参数(加载到缓存)并分配给存储过程参数数组. SqlParameter[]
commandParameters
=
SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
// 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues);
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters); } else
...{
// 没有参数情况下
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
} }
/**////
/// 执行指定数据库连接对象的命令 /// ///
/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, \
///
///
/// /// ///
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText) ...{
return (SqlParameter[])null); }
/**////
/// 执行指定数据库连接对象的命令 /// ///
int
result
=
ExecuteNonQuery(conn,
CommandType.StoredProcedure,
\ ///
///
ExecuteNonQuery(connection,
commandType,
commandText,
/// /// /// ///
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) ...{
if( connection == null ) throw new ArgumentNullException( \
// 创建SqlCommand命令,并进行预处理 SqlCommand cmd = new SqlCommand(); bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (SqlTransaction)null, commandText, commandParameters, out mustCloseConnection );
// Finally, execute the command int retval = cmd.ExecuteNonQuery();
// 清除参数,以便再次使用. cmd.Parameters.Clear(); if( mustCloseConnection ) connection.Close(); return retval; }
/**////
/// 执行指定数据库连接对象的命令,将对象数组的值赋给存储过程参数. /// ///
/// 此方法不提供访问存储过程输出参数和返回值 /// 示例:
/// int result = ExecuteNonQuery(conn, \ ///
///
/// /// ///
public static int ExecuteNonQuery(SqlConnection connection, string spName, params object[] parameterValues) ...{
if( connection == null ) throw new ArgumentNullException( \ if( spName == null ArgumentNullException( \
||
spName.Length
==
0
)
throw
new
commandType,
// 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
// 从缓存中加载存储过程参数 SqlParameter[]
commandParameters
=
SqlHelperParameterCache.GetSpParameterSet(connection, spName);
// 给存储过程分配参数值
AssignParameterValues(commandParameters, parameterValues);
return ExecuteNonQuery(connection, spName, commandParameters); } else ...{
return spName);
} }
/**////
/// 执行带事务的SqlCommand. /// ///
result
=
ExecuteNonQuery(trans,
CommandType.StoredProcedure,
ExecuteNonQuery(connection,
CommandType.StoredProcedure, CommandType.StoredProcedure,
///
///
/// /// ///
public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText) ...{
return (SqlParameter[])null); }
/**////
/// 执行带事务的SqlCommand(指定参数). /// ///
ExecuteNonQuery(transaction,
commandType,
commandText,
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, \new SqlParameter(\ ///
///
/// /// ///
///
public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters) ...{
if( transaction == null ) throw new ArgumentNullException( \ if( transaction != null && transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\
// 预处理
SqlCommand cmd = new SqlCommand(); bool mustCloseConnection = false; PrepareCommand(cmd, // 执行
int retval = cmd.ExecuteNonQuery();
// 清除参数集,以便再次使用. cmd.Parameters.Clear(); return retval; }
/**////
/// 执行带事务的SqlCommand(指定参数值).
/// ///
/// 此方法不提供访问存储过程输出参数和返回值 /// 示例:
transaction.Connection,
transaction,
commandType,
commandText, commandParameters, out mustCloseConnection );
/// int result = ExecuteNonQuery(conn, trans, \ ///
///
/// /// ///
public static int ExecuteNonQuery(SqlTransaction transaction, string spName, params object[] parameterValues)
...{
if( transaction == null ) throw new ArgumentNullException( \
if( transaction != null && transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\ if(
spName
==
null
||
spName.Length
==
0
)
throw
new
ArgumentNullException( \
// 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0))
...{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
SqlParameter[]
commandParameters
=
SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
// 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues);
// 调用重载方法 return
ExecuteNonQuery(transaction,
CommandType.StoredProcedure,
spName, commandParameters); } else ...{
// 没有参数值 return spName);
} }
#endregion ExecuteNonQuery方法结束
ExecuteDataset方法#region ExecuteDataset方法
/**////
/// 执行指定数据库连接字符串的命令,返回DataSet. /// ///
/// DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, \
///
///
ExecuteNonQuery(transaction,
CommandType.StoredProcedure,
/// /// ///
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText) ...{ return (SqlParameter[])null); }
/**////
/// 执行指定数据库连接字符串的命令,返回DataSet. /// ///
/// DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, \ ///
/// /// /// /// ///
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \
// 创建并打开数据库连接对象,操作完成释放对象.
using (SqlConnection connection = new SqlConnection(connectionString)) ...{
connection.Open();
// 调用指定数据库连接字符串重载方法. return commandParameters); } }
/**////
/// 执行指定数据库连接字符串的命令,直接提供参数值,返回DataSet. /// ///
ExecuteDataset(connection,
commandType,
commandText,
ExecuteDataset(connectionString,
commandType,
commandText,
/// 此方法不提供访问存储过程输出参数和返回值. /// 示例:
/// DataSet ds = ExecuteDataset(connString, \ ///
/// /// /// ///
public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues)
...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \
if( spName == null || spName.Length ArgumentNullException( \
if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
// 从缓存中检索存储过程参数 SqlParameter[]
// 给存储过程参数分配值
AssignParameterValues(commandParameters, parameterValues);
return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters); } else ...{
return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName); } }
/**////
/// 执行指定数据库连接对象的命令,返回DataSet. /// ///
/// DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, \ ///
///
///
commandParameters
=
SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
==
0
)
throw
new
/// ///
public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText) ...{ return (SqlParameter[])null); }
/**////
/// 执行指定数据库连接对象的命令,指定存储过程参数,返回DataSet. /// ///
/// DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, \new SqlParameter(\
///
///
/// /// /// ///
public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) ...{
if( connection == null ) throw new ArgumentNullException( \
// 预处理
SqlCommand cmd = new SqlCommand(); bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (SqlTransaction)null, commandText, commandParameters, out mustCloseConnection );
// 创建SqlDataAdapter和DataSet.
using( SqlDataAdapter da = new SqlDataAdapter(cmd) ) ...{
DataSet ds = new DataSet();
// 填充DataSet. da.Fill(ds);
cmd.Parameters.Clear();
if( mustCloseConnection )
commandType,
ExecuteDataset(connection,
commandType,
commandText,
connection.Close();
return ds; } }
/**////
/// 执行指定数据库连接对象的命令,指定参数值,返回DataSet. ///
///
/// 此方法不提供访问存储过程输入参数和返回值. /// 示例.:
/// DataSet ds = ExecuteDataset(conn, \ ///
/// ///
/// ///
public static DataSet ExecuteDataset(SqlConnection connection, string spName, params object[] parameterValues) ...{
if( connection == null ) throw new ArgumentNullException( \
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( \
if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
// 比缓存中加载存储过程参数
SqlParameter[] commandParameters SqlHelperParameterCache.GetSpParameterSet(connection, spName);
// 给存储过程参数分配值
AssignParameterValues(commandParameters, parameterValues);
return ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters); } else ...{
return ExecuteDataset(connection, CommandType.StoredProcedure, spName); }
}
/**////
=
/// 执行指定事务的命令,返回DataSet. /// ///
/// DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, \ ///
///
/// /// ///
public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText) ...{
return (SqlParameter[])null); }
/**////
/// 执行指定事务的命令,指定参数,返回DataSet. /// ///
/// DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, \new SqlParameter(\ ///
///
/// /// /// ///
public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters) ...{
if( transaction == null ) throw new ArgumentNullException( \ if( transaction != null && transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\
// 预处理
SqlCommand cmd = new SqlCommand(); bool mustCloseConnection = false;
PrepareCommand(cmd, transaction.Connection, transaction, commandText, commandParameters, out mustCloseConnection );
commandType,
ExecuteDataset(transaction,
commandType,
commandText,
// 创建 DataAdapter & DataSet
using( SqlDataAdapter da = new SqlDataAdapter(cmd) ) ...{
DataSet ds = new DataSet(); da.Fill(ds);
cmd.Parameters.Clear(); return ds; } }
/**////
/// 执行指定事务的命令,指定参数值,返回DataSet.
/// ///
/// 此方法不提供访问存储过程输入参数和返回值. /// 示例.:
/// DataSet ds = ExecuteDataset(trans, \ ///
///
/// /// ///
public static DataSet ExecuteDataset(SqlTransaction transaction, string spName, params object[] parameterValues) ...{
if( transaction == null ) throw new ArgumentNullException( \
if( transaction != null && transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( \
if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
// 从缓存中加载存储过程参数 SqlParameter[]
// 给存储过程参数分配值
AssignParameterValues(commandParameters, parameterValues);
return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters); }
commandParameters
=
SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
else
...{
return ExecuteDataset(transaction, CommandType.StoredProcedure, spName); } }
#endregion ExecuteDataset数据集命令结束
ExecuteReader 数据阅读器#region ExecuteReader 数据阅读器
/**////
/// 枚举,标识数据库连接是由SqlHelper提供还是由调用者提供 ///
private enum SqlConnectionOwnership
...{
/**////
///
///
/// 执行指定数据库连接对象的数据阅读器.
/// ///
/// 如果是SqlHelper打开连接,当连接关闭DataReader也将关闭. /// 如果是调用都打开连接,DataReader由调用都管理.
///
/// ///
/// ///
///
///
///
private static SqlDataReader ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership) ...{
if( connection == null ) throw new ArgumentNullException( \
bool mustCloseConnection = false;
// 创建命令
SqlCommand cmd = new SqlCommand(); try ...{
PrepareCommand(cmd, connection,
// 创建数据阅读器
SqlDataReader dataReader; transaction, commandType,
commandText, commandParameters, out mustCloseConnection );
command
if (connectionOwnership == SqlConnectionOwnership.External) ...{
dataReader = cmd.ExecuteReader(); } else ...{
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); }
// 清除参数,以便再次使用..
// HACK: There is a problem here, the output parameter values are fletched // when the reader is closed, so if the parameters are detached from the // then the SqlReader can磘 set its values.
// When this happen, the parameters can磘 be used again in other command. bool canClear = true;
foreach(SqlParameter commandParameter in cmd.Parameters) ...{
if (commandParameter.Direction != ParameterDirection.Input) canClear = false; }
if (canClear) ...{
cmd.Parameters.Clear(); } return dataReader; } catch ...{
if( mustCloseConnection ) connection.Close(); throw;
}
}
/**////
/// 执行指定数据库连接字符串的数据阅读器. ///
///
/// SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, \
///
/// /// /// ///
public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText) ...{
return ExecuteReader(connectionString, (SqlParameter[])null); }
/**////
/// 执行指定数据库连接字符串的数据阅读器,指定参数. /// ///
/// SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, \ ///
///
/// ///
///
///
public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) ...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \ SqlConnection connection = null; try ...{
commandType, commandText,
connection = new SqlConnection(connectionString);
connection.Open();
return ExecuteReader(connection, null, commandType, commandText, commandParameters,SqlConnectionOwnership.Internal); } catch
...{
// If we fail to return the SqlDatReader, we need to close the connection ourselves
if( connection != null ) connection.Close(); throw; } }
/**////
/// 执行指定数据库连接字符串的数据阅读器,指定参数值. ///
///
/// 此方法不提供访问存储过程输出参数和返回值参数. /// 示例:
/// SqlDataReader dr = ExecuteReader(connString, \ ///
/// /// /// ///
public static SqlDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues) ...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \
if( spName == null || spName.Length ArgumentNullException( \
if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
SqlParameter[]
AssignParameterValues(commandParameters, parameterValues);
return ExecuteReader(connectionString, CommandType.StoredProcedure,
commandParameters
=
==
0
)
throw
new
SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
spName, commandParameters);
} else ...{
return ExecuteReader(connectionString, CommandType.StoredProcedure, spName); } }
/**////
/// 执行指定数据库连接对象的数据阅读器. /// ///
/// SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, \
///
///
/// /// ///
public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText) ...{
return ExecuteReader(connection, commandType, commandText, (SqlParameter[])null); }
/**////
/// [调用者方式]执行指定数据库连接对象的数据阅读器,指定参数.
/// ///
/// SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure,
\ ///
///
/// /// /// ///
public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) ...{
return ExecuteReader(connection, (SqlTransaction)null, commandType,
commandText, commandParameters, SqlConnectionOwnership.External); }
/**////
/// [调用者方式]执行指定数据库连接对象的数据阅读器,指定参数值. ///
///
/// 此方法不提供访问存储过程输出参数和返回值参数. /// 示例:
/// SqlDataReader dr = ExecuteReader(conn, \ ///
/// ///
/// ///
public static SqlDataReader ExecuteReader(SqlConnection connection, string spName, params object[] parameterValues) ...{
if( connection == null ) throw new ArgumentNullException( \ if(
spName
==
null
||
spName.Length
==
0
)
throw
new
ArgumentNullException( \
if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
SqlParameter[] commandParameters SqlHelperParameterCache.GetSpParameterSet(connection, spName);
AssignParameterValues(commandParameters, parameterValues);
return ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters); } else
...{
return ExecuteReader(connection, CommandType.StoredProcedure, spName); } }
/**////
/// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值. /// ///
=
/// SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, \
///
///
/// /// ///
public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText) ...{ return (SqlParameter[])null); }
/**////
/// [调用者方式]执行指定数据库事务的数据阅读器,指定参数. /// ///
/// SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, \
///
///
/// ///
ExecuteReader(transaction,
commandType,
commandText,
///
///
public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters) ...{
if( transaction == null ) throw new ArgumentNullException( \ if( transaction != null && transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\
return
ExecuteReader(transaction.Connection,
transaction,
commandType,
commandText, commandParameters, SqlConnectionOwnership.External); }
/**////
/// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值. /// ///
/// 此方法不提供访问存储过程输出参数和返回值参数. ///
/// 示例:
/// SqlDataReader dr = ExecuteReader(trans, \ ///
/// ///
/// ///
public static SqlDataReader ExecuteReader(SqlTransaction transaction, string spName, params object[] parameterValues) ...{
if( transaction == null ) throw new ArgumentNullException( \
if( transaction != null && transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\
if( spName == null ArgumentNullException( \
// 如果有参数值
||
spName.Length
==
0
)
throw
new
if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
SqlParameter[] commandParameters
SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
AssignParameterValues(commandParameters, parameterValues);
return ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters); }
else ...{
// 没有参数值
return ExecuteReader(transaction, CommandType.StoredProcedure, spName); } }
#endregion ExecuteReader数据阅读器
ExecuteScalar 返回结果集中的第一行第一列#region ExecuteScalar 返回结果集中的第一行第一列
/**////
/// 执行指定数据库连接字符串的命令,返回结果集中的第一行第一列.
=
/// ///
/// int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, \
///
/// /// /// ///
// 执行参数为空的方法
return ExecuteScalar(connectionString, (SqlParameter[])null);
commandType,
commandText,
connectionString,
CommandType
}
/**////
/// 执行指定数据库连接字符串的命令,指定参数,返回结果集中的第一行第一列. /// ///
/// int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, \
///
/// /// ///
///
///
public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \
// 创建并打开数据库连接对象,操作完成释放对象.
using (SqlConnection connection = new SqlConnection(connectionString)) ...{
connection.Open();
// 调用指定数据库连接字符串重载方法. return
ExecuteScalar(connection,
commandType,
commandText,
commandParameters);
} }
/**////
/// 执行指定数据库连接字符串的命令,指定参数值,返回结果集中的第一行第一列. ///
///
/// 此方法不提供访问存储过程输出参数和返回值参数. ///
/// 示例:
/// int orderCount = (int)ExecuteScalar(connString, \ ///
/// /// /// ///
public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues) ...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \
if( spName == null || spName.Length ArgumentNullException( \
// 如果有参数值
==
0
)
throw
new
if ((parameterValues != null) && (parameterValues.Length > 0))
...{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
SqlParameter[] commandParameters SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
// 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues);
// 调用重载方法
return ExecuteScalar(connectionString, spName, commandParameters); } else
...{
// 没有参数值 return
ExecuteScalar(connectionString,
CommandType.StoredProcedure, CommandType.StoredProcedure,
=
spName);
} }
/**////
/// 执行指定数据库连接对象的命令,返回结果集中的第一行第一列. /// ///
/// int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, \
///
///
/// /// ///
public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText) ...{
// 执行参数为空的方法 return
ExecuteScalar(connection,
commandType,
commandText,
(SqlParameter[])null); }
/**////
/// 执行指定数据库连接对象的命令,指定参数,返回结果集中的第一行第一列. /// ///
/// int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, \ ///
///
/// ///
///
///
public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) ...{
if( connection == null ) throw new ArgumentNullException( \
// 创建SqlCommand命令,并进行预处理
SqlCommand cmd = new SqlCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (SqlTransaction)null, commandText, commandParameters, out mustCloseConnection );
// 执行SqlCommand命令,并返回结果. object retval = cmd.ExecuteScalar();
// 清除参数,以便再次使用. cmd.Parameters.Clear();
if( mustCloseConnection ) connection.Close();
return retval;
}
/**////
/// 执行指定数据库连接对象的命令,指定参数值,返回结果集中的第一行第一列. ///
///
/// 此方法不提供访问存储过程输出参数和返回值参数. ///
/// 示例:
/// int orderCount = (int)ExecuteScalar(conn, \ ///
/// /// ///
commandType,
///
public static object ExecuteScalar(SqlConnection connection, string spName, params object[] parameterValues)
...{
if( connection == null ) throw new ArgumentNullException( \
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( \
// 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0))
...{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () SqlParameter[]
commandParameters
=
SqlHelperParameterCache.GetSpParameterSet(connection, spName);
// 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues);
// 调用重载方法
return ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters); } else
...{
// 没有参数值
return ExecuteScalar(connection, CommandType.StoredProcedure, spName); } }
/**////
/// 执行指定数据库事务的命令,返回结果集中的第一行第一列. /// ///
/// int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, \
///
///
/// /// ///
public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText) ...{
// 执行参数为空的方法 return (SqlParameter[])null); }
/**////
/// 执行指定数据库事务的命令,指定参数,返回结果集中的第一行第一列. /// ///
/// int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, \ ///
ExecuteScalar(transaction,
commandType,
commandText,
///
/// ///
///
///
public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters) ...{
if( transaction == null ) throw new ArgumentNullException( \ if( transaction != null && transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\
// 创建SqlCommand命令,并进行预处理 SqlCommand cmd = new SqlCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, transaction.Connection, transaction, commandText, commandParameters, out mustCloseConnection );
// 执行SqlCommand命令,并返回结果. object retval = cmd.ExecuteScalar();
// 清除参数,以便再次使用. cmd.Parameters.Clear(); return retval; }
/**////
/// 执行指定数据库事务的命令,指定参数值,返回结果集中的第一行第一列.
/// ///
/// 此方法不提供访问存储过程输出参数和返回值参数. ///
/// 示例:
commandType,
/// int orderCount = (int)ExecuteScalar(trans, \ ///
/// ///
/// ///
public static object ExecuteScalar(SqlTransaction transaction, string spName, params object[] parameterValues) ...{
if( transaction == null ) throw new ArgumentNullException( \ if( transaction != null && transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( \
// 如果有参数值 if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
// PPull the parameters for this stored procedure from the parameter cache () SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
// 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues);
// 调用重载方法
return ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters); }
else ...{
// 没有参数值
return ExecuteScalar(transaction, CommandType.StoredProcedure, spName); } }
#endregion ExecuteScalar
ExecuteXmlReader XML阅读器#region ExecuteXmlReader XML阅读器
/**////
/// 执行指定数据库连接对象的SqlCommand命令,并产生一个XmlReader对象做为结果集返回.
/// ///
/// XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, \
///
///
/// ///
///
public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText) ...{
// 执行参数为空的方法 return (SqlParameter[])null); }
/**////
/// 执行指定数据库连接对象的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数. /// ///
/// XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, \ ///
///
/// ///
///
///
public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) ...{
if( connection == null ) throw new ArgumentNullException( \
bool mustCloseConnection = false;
// 创建SqlCommand命令,并进行预处理 SqlCommand cmd = new SqlCommand(); try
...{
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );
// 执行命令
XmlReader retval = cmd.ExecuteXmlReader();
// 清除参数,以便再次使用. cmd.Parameters.Clear();
ExecuteXmlReader(connection,
commandType,
commandText,
return retval;
} catch ...{
if( mustCloseConnection ) connection.Close(); throw; } }
/**////
/// 执行指定数据库连接对象的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数值. ///
///
/// 此方法不提供访问存储过程输出参数和返回值参数. ///
/// 示例:
/// XmlReader r = ExecuteXmlReader(conn, \ ///
///
/// ///
public static XmlReader ExecuteXmlReader(SqlConnection connection, string spName, params object[] parameterValues) ...{
if( connection == null ) throw new ArgumentNullException( \
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( \
// 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () SqlParameter[]
commandParameters
=
SqlHelperParameterCache.GetSpParameterSet(connection, spName);
// 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues);
// 调用重载方法 return
ExecuteXmlReader(connection,
CommandType.StoredProcedure,
spName, commandParameters); } else ...{
// 没有参数值 return spName); } }
/**////
/// 执行指定数据库事务的SqlCommand命令,并产生一个XmlReader对象做为结果集返回.
/// ///
/// XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, \
///
///
/// ///
///
public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText) ...{
// 执行参数为空的方法
return ExecuteXmlReader(transaction, (SqlParameter[])null); }
/**////
/// 执行指定数据库事务的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数. ///
///
/// XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, \
///
///
/// ///
commandType,
commandText,
ExecuteXmlReader(connection,
CommandType.StoredProcedure,
AUTO\
///
///
public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters) ...{
if( transaction == null ) throw new ArgumentNullException( \
if( transaction != null && transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\
// 创建SqlCommand命令,并进行预处理 SqlCommand cmd = new SqlCommand(); bool mustCloseConnection = false;
PrepareCommand(cmd, transaction.Connection, commandText, commandParameters, out mustCloseConnection );
// 执行命令
XmlReader retval = cmd.ExecuteXmlReader();
// 清除参数,以便再次使用. cmd.Parameters.Clear(); return retval; }
/**////
/// 执行指定数据库事务的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数值. ///
///
/// 此方法不提供访问存储过程输出参数和返回值参数. ///
/// 示例:
/// XmlReader r = ExecuteXmlReader(trans, \ ///
///
/// /// ///
public static XmlReader ExecuteXmlReader(SqlTransaction transaction, string spName, params object[] parameterValues) ...{
if( transaction == null ) throw new ArgumentNullException( \
transaction,
commandType,
if( transaction != null && transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( \
// 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () SqlParameter[]
commandParameters
=
SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
// 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues);
// 调用重载方法 return }
else ...{
// 没有参数值 return spName);
}
}
#endregion ExecuteXmlReader 阅读器结束
FillDataset 填充数据集#region FillDataset 填充数据集
/**////
/// 执行指定数据库连接字符串的命令,映射数据表并填充数据集. /// ///
/// FillDataset(connString, CommandType.StoredProcedure, \ds, new string[] ...{\
///
/// /// /// ///
ExecuteXmlReader(transaction,
CommandType.StoredProcedure,
ExecuteXmlReader(transaction,
CommandType.StoredProcedure,
spName, commandParameters);
///
public static void FillDataset(string connectionString, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames) ...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \
if( dataSet == null ) throw new ArgumentNullException( \
// 创建并打开数据库连接对象,操作完成释放对象.
using (SqlConnection connection = new SqlConnection(connectionString)) ...{
connection.Open();
// 调用指定数据库连接字符串重载方法.
FillDataset(connection, commandType, commandText, dataSet, tableNames); } }
/**////
/// 执行指定数据库连接字符串的命令,映射数据表并填充数据集.指定命令参数. /// ///
/// FillDataset(connString, CommandType.StoredProcedure, \ds, new string[] ...{\ ///
///
/// ///
///
/// ///
public static void FillDataset(string connectionString, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames, params SqlParameter[] commandParameters) ...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \
if( dataSet == null ) throw new ArgumentNullException( \ // 创建并打开数据库连接对象,操作完成释放对象.
using (SqlConnection connection = new SqlConnection(connectionString)) ...{
connection.Open();
// 调用指定数据库连接字符串重载方法.
FillDataset(connection, commandType, commandText, dataSet, tableNames, commandParameters); } }
/**////
/// 执行指定数据库连接字符串的命令,映射数据表并填充数据集,指定存储过程参数值.
///
///
/// 此方法不提供访问存储过程输出参数和返回值参数. ///
/// 示例:
/// FillDataset(connString, CommandType.StoredProcedure, \ds, new string[] ...{\
///
/// ///
/// ///
/// 用户定义的表名 (可有是实际的表名.)
///
/// public static void FillDataset(string connectionString, string spName, DataSet dataSet, string[] tableNames, params object[] parameterValues) ...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \
if( dataSet == null ) throw new ArgumentNullException( \ // 创建并打开数据库连接对象,操作完成释放对象.
using (SqlConnection connection = new SqlConnection(connectionString)) ...{
connection.Open();
// 调用指定数据库连接字符串重载方法.
FillDataset (connection, spName, dataSet, tableNames, parameterValues); } }
/**////
/// 执行指定数据库连接对象的命令,映射数据表并填充数据集. /// ///
/// FillDataset(conn, CommandType.StoredProcedure, \ds, new string[] ...{\ ///
///
/// /// /// ///
public static void FillDataset(SqlConnection connection, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames) ...{
FillDataset(connection, commandType, commandText, dataSet, tableNames, null); }
/**////
/// 执行指定数据库连接对象的命令,映射数据表并填充数据集,指定参数. /// ///
/// FillDataset(conn, CommandType.StoredProcedure, \ds, new string[] ...{\ ///
///
/// /// /// ///
///
///
public static void FillDataset(SqlConnection connection, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames,
params SqlParameter[] commandParameters) ...{
FillDataset(connection, null, commandType, commandText, dataSet, tableNames,
commandParameters);
}
/**////
/// 执行指定数据库连接对象的命令,映射数据表并填充数据集,指定存储过程参数值.
///
///
/// 此方法不提供访问存储过程输出参数和返回值参数. ///
/// 示例:
/// FillDataset(conn, \ ///
/// ///
/// ///
///
/// public static void FillDataset(SqlConnection connection, string spName, DataSet dataSet, string[] tableNames, params object[] parameterValues) ...{
if ( connection == null ) throw new ArgumentNullException( \ if (dataSet == null ) throw new ArgumentNullException( \
if( spName == null || spName.Length == 0 ) throw ArgumentNullException( \
// 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
SqlParameter[] commandParameters SqlHelperParameterCache.GetSpParameterSet(connection, spName);
// 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues);
// 调用重载方法
FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters); }
= new
else
...{
// 没有参数值
FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames); } }
/**////
/// 执行指定数据库事务的命令,映射数据表并填充数据集.
/// ///
/// FillDataset(trans, CommandType.StoredProcedure, \ds, new
string[] ...{\ ///
///
/// /// /// /// public static commandType,
void
FillDataset(SqlTransaction
transaction,
CommandType
string commandText,
DataSet dataSet, string[] tableNames)
...{
FillDataset (transaction, commandType, commandText, dataSet, tableNames, null); }
/**////
/// 执行指定数据库事务的命令,映射数据表并填充数据集,指定参数. /// ///
/// FillDataset(trans, CommandType.StoredProcedure, \ds, new string[] ...{\ ///
///
/// /// /// ///
/// 用户定义的表名 (可有是实际的表名.)
///
///
public commandType,
string commandText, DataSet dataSet, string[] tableNames, params SqlParameter[] commandParameters) ...{
FillDataset(transaction.Connection, transaction, commandType, commandText, dataSet, tableNames, commandParameters); }
/**////
/// 执行指定数据库事务的命令,映射数据表并填充数据集,指定存储过程参数值. ///
///
/// 此方法不提供访问存储过程输出参数和返回值参数. ///
/// 示例:
/// FillDataset(trans, \
///
/// ///
///
static
void
FillDataset(SqlTransaction
transaction,
CommandType
///
///
/// public static void FillDataset(SqlTransaction transaction, string spName, DataSet dataSet, string[] tableNames, params object[] parameterValues)
...{
if( transaction == null ) throw new ArgumentNullException( \ if( transaction != null && transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\
if( dataSet == null ) throw new ArgumentNullException( \
if( spName == null || spName.Length == 0 ) throw ArgumentNullException( \
// 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0)) ...{
new
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
SqlParameter[] commandParameters
SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
// 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues);
// 调用重载方法
FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters); }
else ...{
// 没有参数值
FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames);
} }
/**////
/// [私有方法][内部调用]执行指定数据库连接对象/事务的命令,映射数据表并填充数据集,DataSet/TableNames/SqlParameters. /// ///
/// FillDataset(conn, trans, CommandType.StoredProcedure, \ds, new string[] ...{\ ///
///
///
/// /// /// ///
///
///
private static void FillDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType,
string commandText, DataSet dataSet, string[] tableNames, params SqlParameter[] commandParameters) ...{
=
if( connection == null ) throw new ArgumentNullException( \ if( dataSet == null ) throw new ArgumentNullException( \
// 创建SqlCommand命令,并进行预处理 SqlCommand command = new SqlCommand(); bool mustCloseConnection = false; PrepareCommand(command,
connection,
transaction,
commandType,
commandText, commandParameters, out mustCloseConnection );
// 执行命令
using( SqlDataAdapter dataAdapter = new SqlDataAdapter(command) ) ...{
// 追加表映射
if (tableNames != null && tableNames.Length > 0) ...{
string tableName = \
for (int index=0; index < tableNames.Length; index++) ...{
if( tableNames[index] == null || tableNames[index].Length == 0 ) throw new ArgumentException( \was provided as null or empty string.\
dataAdapter.TableMappings.Add(tableName, tableNames[index]); tableName += (index + 1).ToString(); }
}
// 填充数据集使用默认表名称 dataAdapter.Fill(dataSet);
// 清除参数,以便再次使用. command.Parameters.Clear(); }
if( mustCloseConnection ) connection.Close(); }
#endregion
UpdateDataset 更新数据集#region UpdateDataset 更新数据集 /**////
/// 执行数据集更新到数据库,指定inserted, updated, or deleted命令. /// ///
/// 示例:
/// UpdateDataset(conn, insertCommand, deleteCommand, updateCommand, dataSet, \
///
///
///
///
/// ///
public static void UpdateDataset(SqlCommand insertCommand, SqlCommand deleteCommand, SqlCommand updateCommand, DataSet dataSet, string tableName) ...{ if(
insertCommand
==
null null null
) ) )
throw throw throw
new new new
ArgumentNullException( \ if( deleteCommand == ArgumentNullException( \ if(
updateCommand
==
ArgumentNullException( \
if( tableName == null || tableName.Length == 0 ) throw new ArgumentNullException( \
// 创建SqlDataAdapter,当操作完成后释放.
using (SqlDataAdapter dataAdapter = new SqlDataAdapter()) ...{
// 设置数据适配器命令
dataAdapter.UpdateCommand = updateCommand; dataAdapter.InsertCommand = insertCommand; dataAdapter.DeleteCommand = deleteCommand;
// 更新数据集改变到数据库
dataAdapter.Update (dataSet, tableName);
// 提交所有改变到数据集. dataSet.AcceptChanges(); } }
#endregion
CreateCommand 创建一条SqlCommand命令#region CreateCommand 创建一条SqlCommand命令 /**////
/// 创建SqlCommand命令,指定数据库连接对象,存储过程名和参数. /// ///
/// SqlCommand command = CreateCommand(conn, \\
///
/// ///
/// ///
public static SqlCommand CreateCommand(SqlConnection connection, string spName, params string[] sourceColumns) ...{
if( connection == null ) throw new ArgumentNullException( \
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( \
// 创建命令
SqlCommand cmd = new SqlCommand( spName, connection ); cmd.CommandType = CommandType.StoredProcedure;
// 如果有参数值
if ((sourceColumns != null) && (sourceColumns.Length > 0)) ...{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
SqlParameter[] commandParameters SqlHelperParameterCache.GetSpParameterSet(connection, spName);
// 将源表的列到映射到DataSet命令中.
for (int index=0; index < sourceColumns.Length; index++)
commandParameters[index].SourceColumn = sourceColumns[index];
// Attach the discovered parameters to the SqlCommand object AttachParameters (cmd, commandParameters); }
return cmd; }
#endregion
ExecuteNonQueryTypedParams 类型化ExecuteNonQueryTypedParams 类型化参数(DataRow)
参
数
(DataRow)#region
=
/**////
/// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回受影响的行数.
///
/// ///
///
///
public static int ExecuteNonQueryTypedParams(String connectionString, String spName, DataRow dataRow)
...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \
if( spName == null || spName.Length ArgumentNullException( \
// 如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0) ...{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
SqlParameter[] commandParameters SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
// 分配参数值
AssignParameterValues(commandParameters, dataRow);
return SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters); } else ...{
return
CommandType.StoredProcedure, spName); } }
/**////
/// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回受影响的行数.
///
/// ///
///
SqlHelper.ExecuteNonQuery(connectionString,
=
==
0
)
throw
new
///
public static int ExecuteNonQueryTypedParams(SqlConnection connection, String spName, DataRow dataRow) ...{
if( connection == null ) throw new ArgumentNullException( \ if(
spName
==
null
||
spName.Length
==
0
)
throw
new
ArgumentNullException( \
// 如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0)
...{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
SqlParameter[]
commandParameters
=
SqlHelperParameterCache.GetSpParameterSet(connection, spName);
// 分配参数值
AssignParameterValues(commandParameters, dataRow); return } else ...{
return
CommandType.StoredProcedure, spName); }
}
/**////
/// 执行指定连接数据库事物的存储过程,使用DataRow做为参数值,返回受影响的行数.
///
/// ///
/// ///
public static int ExecuteNonQueryTypedParams(SqlTransaction transaction, String spName, DataRow dataRow) ...{
if( transaction == null ) throw new ArgumentNullException( \ if( transaction != null && transaction.Connection == null ) throw new ArgumentException( \transaction was rollbacked or commited, please provide an open transaction.\
SqlHelper.ExecuteNonQuery(connection,
CommandType.StoredProcedure, spName, commandParameters);
SqlHelper.ExecuteNonQuery(connection,
if( spName == null || spName.Length == 0 ) throw new
ArgumentNullException( \
// Sf the row has values, the store procedure parameters must be initialized if (dataRow != null && dataRow.ItemArray.Length > 0)
...{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
SqlParameter[]
// 分配参数值
AssignParameterValues(commandParameters, dataRow);
return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters); } else ...{ return
CommandType.StoredProcedure, spName);
} }
#endregion
ExecuteDatasetTypedParams 类型ExecuteDatasetTypedParams 类型化参数(DataRow)
SqlHelper.ExecuteNonQuery(transaction, commandParameters
=
SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
化参数(DataRow)#region
/**////
/// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回DataSet.
///
/// ///
/// ///
public static DataSet ExecuteDatasetTypedParams(string connectionString, String spName, DataRow dataRow)
...{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( \ if(
spName
==
null
||
spName.Length
==
0
)
throw
new
ArgumentNullException( \
//如果row有值,存储过程必须初始化.
正在阅读:
完整SqlHelper类详细中文注释02-03
雅思写作常用连接词整理08-24
公路局深入开展扶贫领域自查自纠范文02-22
AVR单片机串行接口及应用05-23
2018年中国wifi音响发展现状与市场前景分析(目录) - 图文10-25
2018-2024年中国空气净化器行业运营态势报告(目录) - 图文03-06
关于印发重庆市“十二五”科学技术和战略性新兴产业发展规划的通知03-10
动物性食品安全03-18
东师 学前儿童游戏论16春在线作业204-15
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 注释
- 中文
- SqlHelper
- 完整
- 详细