/// <summary>
/// OleDbServer数据访问帮助类
/// </summary>
public sealed class OleDbHelper
{
public static string getACCESS07constr(string accessfilename, string thepassword = @"")
{
if (thepassword.Length == 0)
return @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = " + (char)34 + accessfilename + (char)34;
else
{ return @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + (char)34 + accessfilename + (char)34 + @";Persist Security Info=True;Jet OLEDB:Database password=" + (char)34 + thepassword + (char)34; }
}
public static string getACCESSconstr(string accessfilename, string thepassword = @"")
{
if (thepassword.Length == 0)
return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + (char)34 + accessfilename + (char)34;
else
return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + (char)34 + accessfilename + (char)34 + @";Persist Security Info=True;Jet OLEDB:Database password=" + (char)34 + thepassword + (char)34;
}
//private static string _connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =db1.accdb";//定义数据库连接参数"; private static string _connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =PlanDb.accdb";//定义数据库连接参数"; #region 私有构造函数和方法 private OleDbHelper() { } /// <summary>
/// 将OleDbParameter参数数组(参数值)分配给OleDbCommand命令.
/// 这个方法将给任何一个参数分配DBNull.Value;
/// 该操作将阻止默认值的使用.
/// </summary>
/// <param name="command">命令名</param>
/// <param name="commandParameters">OleDbParameters数组</param>
private static void AttachParameters(OleDbCommand command, OleDbParameter[] commandParameters)
{
if (command == null) throw new ArgumentNullException("command");
if (commandParameters != null)
{
foreach (OleDbParameter 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);
}
}
}
} /// <summary>
/// 将DataRow类型的列值分配到OleDbParameter参数数组.
/// </summary>
/// <param name="commandParameters">要分配值的OleDbParameter参数数组</param>
/// <param name="dataRow">将要分配给存储过程参数的DataRow</param>
private static void AssignParameterValues(OleDbParameter[] commandParameters, DataRow dataRow)
{
if ((commandParameters == null) || (dataRow == null))
{
return;
} int i = 0;
// 设置参数值
foreach (OleDbParameter 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++;
}
} /// <summary>
/// 将一个对象数组分配给OleDbParameter参数数组.
/// </summary>
/// <param name="commandParameters">要分配值的OleDbParameter参数数组</param>
/// <param name="parameterValues">将要分配给存储过程参数的对象数组</param>
private static void AssignParameterValues(OleDbParameter[] 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 paramInstance = (IDbDataParameter)parameterValues[i];
if (paramInstance.Value == null)
{
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];
}
}
} /// <summary>
/// 预处理用户提供的命令,数据库连接/事务/命令类型/参数
/// </summary>
/// <param name="command">要处理的OleDbCommand</param>
/// <param name="connection">数据库连接</param>
/// <param name="transaction">一个有效的事务或者是null值</param>
/// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param>
/// <param name="commandText">存储过程名或都T-SQL命令文本</param>
/// <param name="commandParameters">和命令相关联的OleDbParameter参数数组,如果没有参数为'null'</param>
/// <param name="mustCloseConnection"><c>true</c> 如果连接是打开的,则为true,其它情况下为false.</param>
private static void PrepareCommand(OleDbCommand command, OleDbConnection connection, OleDbTransaction transaction, CommandType commandType, string commandText, OleDbParameter[] commandParameters, out bool mustCloseConnection)
{
if (command == null) throw new ArgumentNullException("command");
if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); // 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("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
command.Transaction = transaction;
} // 设置命令类型.
command.CommandType = commandType; // 分配命令参数
if (commandParameters != null)
{
AttachParameters(command, commandParameters);
}
return;
} #endregion 私有构造函数和方法结束 #region 数据库连接
/// <summary>
/// 一个有效的数据库连接字符串
/// </summary>
/// <returns></returns>
public static string GetConnString()
{
return _connectionString; }
/// <summary>
/// 一个有效的数据库连接对象
/// </summary>
/// <returns></returns>
public static OleDbConnection GetConnection()
{
OleDbConnection Connection = new OleDbConnection(OleDbHelper.GetConnString());
return Connection;
}
#endregion #region ExecuteNonQuery命令 /// <summary>
/// 执行指定连接字符串,类型的OleDbCommand.
/// </summary>
/// <remarks>
/// 示例:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders");
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param>
/// <param name="commandText">存储过程名称或SQL语句</param>
/// <returns>返回命令影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
{
return ExecuteNonQuery(connectionString, commandType, commandText, (OleDbParameter[])null);
} /// <summary>
/// 执行指定连接字符串,类型的OleDbCommand.如果没有提供参数,不返回结果.
/// </summary>
/// <remarks>
/// 示例:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param>
/// <param name="commandText">存储过程名称或SQL语句</param>
/// <param name="commandParameters">OleDbParameter参数数组</param>
/// <returns>返回命令影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open(); return ExecuteNonQuery(connection, commandType, commandText, commandParameters);
}
} /// <summary>
/// 执行指定连接字符串的存储过程,将对象数组的值赋给存储过程参数,
/// 此方法需要在参数缓存方法中探索参数并生成参数.
/// </summary>
/// <remarks>
/// 这个方法没有提供访问输出参数和返回值.
/// 示例:
/// int result = ExecuteNonQuery(connString, "PublishOrders", 24, 36);
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串/param>
/// <param name="spName">存储过程名称</param>
/// <param name="parameterValues">分配到存储过程输入参数的对象数组</param>
/// <returns>返回受影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果存在参数值
if ((parameterValues != null) && (parameterValues.Length > 0))
{
// 从探索存储过程参数(加载到缓存)并分配给存储过程参数数组.
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connectionString, spName); // 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues); return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
// 没有参数情况下
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定数据库连接对象的命令
/// </summary>
/// <remarks>
/// 示例:
/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders");
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <returns>返回影响的行数</returns>
public static int ExecuteNonQuery(OleDbConnection connection, CommandType commandType, string commandText)
{
return ExecuteNonQuery(connection, commandType, commandText, (OleDbParameter[])null);
} /// <summary>
/// 执行指定数据库连接对象的命令
/// </summary>
/// <remarks>
/// 示例:
/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
/// <param name="commandText">T存储过程名称或T-SQL语句</param>
/// <param name="commandParameters">OleDbParamter参数数组</param>
/// <returns>返回影响的行数</returns>
public static int ExecuteNonQuery(OleDbConnection connection, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
if (connection == null) throw new ArgumentNullException("connection"); // 创建OleDbCommand命令,并进行预处理
OleDbCommand cmd = new OleDbCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (OleDbTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection); // Finally, execute the command
int retval = cmd.ExecuteNonQuery(); // 清除参数,以便再次使用.
cmd.Parameters.Clear();
if (mustCloseConnection)
connection.Close();
return retval;
} /// <summary>
/// 执行指定数据库连接对象的命令,将对象数组的值赋给存储过程参数.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输出参数和返回值
/// 示例:
/// int result = ExecuteNonQuery(conn, "PublishOrders", 24, 36);
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
/// <returns>返回影响的行数</returns>
public static int ExecuteNonQuery(OleDbConnection connection, string spName, params object[] parameterValues)
{
if (connection == null) throw new ArgumentNullException("connection");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0))
{
// 从缓存中加载存储过程参数
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection, spName); // 给存储过程分配参数值
AssignParameterValues(commandParameters, parameterValues); return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行带事务的OleDbCommand.
/// </summary>
/// <remarks>
/// 示例.:
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders");
/// </remarks>
/// <param name="transaction">一个有效的数据库连接对象</param>
/// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <returns>返回影响的行数/returns>
public static int ExecuteNonQuery(OleDbTransaction transaction, CommandType commandType, string commandText)
{
return ExecuteNonQuery(transaction, commandType, commandText, (OleDbParameter[])null);
} /// <summary>
/// 执行带事务的OleDbCommand(指定参数).
/// </summary>
/// <remarks>
/// 示例:
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="transaction">一个有效的数据库连接对象</param>
/// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="commandParameters">OleDbParamter参数数组</param>
/// <returns>返回影响的行数</returns>
public static int ExecuteNonQuery(OleDbTransaction transaction, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); // 预处理
OleDbCommand cmd = new OleDbCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection); // 执行
int retval = cmd.ExecuteNonQuery(); // 清除参数集,以便再次使用.
cmd.Parameters.Clear();
return retval;
} /// <summary>
/// 执行带事务的OleDbCommand(指定参数值).
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输出参数和返回值
/// 示例:
/// int result = ExecuteNonQuery(conn, trans, "PublishOrders", 24, 36);
/// </remarks>
/// <param name="transaction">一个有效的数据库连接对象</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
/// <returns>返回受影响的行数</returns>
public static int ExecuteNonQuery(OleDbTransaction transaction, string spName, params object[] parameterValues)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0))
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection, spName); // 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues); // 调用重载方法
return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
// 没有参数值
return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
}
} #endregion ExecuteNonQuery方法结束 #region ExecuteDataset方法 /// <summary>
/// 执行指定数据库连接字符串的命令,返回DataSet.
/// </summary>
/// <remarks>
/// 示例:
/// DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders");
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <returns>返回一个包含结果集的DataSet</returns>
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
{
return ExecuteDataset(connectionString, commandType, commandText, (OleDbParameter[])null);
} /// <summary>
/// 执行指定数据库连接字符串的命令,返回DataSet.
/// </summary>
/// <remarks>
/// 示例:
/// DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="commandParameters">OleDbParamters参数数组</param>
/// <returns>返回一个包含结果集的DataSet</returns>
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); // 创建并打开数据库连接对象,操作完成释放对象.
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open(); // 调用指定数据库连接字符串重载方法.
return ExecuteDataset(connection, commandType, commandText, commandParameters);
}
} /// <summary>
/// 执行指定数据库连接字符串的命令,直接提供参数值,返回DataSet.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输出参数和返回值.
/// 示例:
/// DataSet ds = ExecuteDataset(connString, "GetOrders", 24, 36);
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
/// <returns>返回一个包含结果集的DataSet</returns>
public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); if ((parameterValues != null) && (parameterValues.Length > 0))
{
// 从缓存中检索存储过程参数
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connectionString, spName); // 给存储过程参数分配值
AssignParameterValues(commandParameters, parameterValues); return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定数据库连接对象的命令,返回DataSet.
/// </summary>
/// <remarks>
/// 示例:
/// DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders");
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <returns>返回一个包含结果集的DataSet</returns>
public static DataSet ExecuteDataset(OleDbConnection connection, CommandType commandType, string commandText)
{
return ExecuteDataset(connection, commandType, commandText, (OleDbParameter[])null);
} /// <summary>
/// 执行指定数据库连接对象的命令,指定存储过程参数,返回DataSet.
/// </summary>
/// <remarks>
/// 示例:
/// DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">OleDbParamter参数数组</param>
/// <returns>返回一个包含结果集的DataSet</returns>
public static DataSet ExecuteDataset(OleDbConnection connection, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
if (connection == null) throw new ArgumentNullException("connection"); // 预处理
OleDbCommand cmd = new OleDbCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (OleDbTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection); // 创建OleDbDataAdapter和DataSet.
using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
{
DataSet ds = new DataSet(); // 填充DataSet.
da.Fill(ds); cmd.Parameters.Clear(); if (mustCloseConnection)
connection.Close(); return ds;
}
} /// <summary>
/// 执行指定数据库连接对象的命令,指定参数值,返回DataSet.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输入参数和返回值.
/// 示例.:
/// DataSet ds = ExecuteDataset(conn, "GetOrders", 24, 36);
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
/// <returns>返回一个包含结果集的DataSet</returns>
public static DataSet ExecuteDataset(OleDbConnection connection, string spName, params object[] parameterValues)
{
if (connection == null) throw new ArgumentNullException("connection");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); if ((parameterValues != null) && (parameterValues.Length > 0))
{
// 比缓存中加载存储过程参数
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection, spName); // 给存储过程参数分配值
AssignParameterValues(commandParameters, parameterValues); return ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return ExecuteDataset(connection, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定事务的命令,返回DataSet.
/// </summary>
/// <remarks>
/// 示例:
/// DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");
/// </remarks>
/// <param name="transaction">事务</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <returns>返回一个包含结果集的DataSet</returns>
public static DataSet ExecuteDataset(OleDbTransaction transaction, CommandType commandType, string commandText)
{
return ExecuteDataset(transaction, commandType, commandText, (OleDbParameter[])null);
} /// <summary>
/// 执行指定事务的命令,指定参数,返回DataSet.
/// </summary>
/// <remarks>
/// 示例:
/// DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="transaction">事务</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">OleDbParamter参数数组</param>
/// <returns>返回一个包含结果集的DataSet</returns>
public static DataSet ExecuteDataset(OleDbTransaction transaction, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); // 预处理
OleDbCommand cmd = new OleDbCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection); // 创建 DataAdapter & DataSet
using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
} /// <summary>
/// 执行指定事务的命令,指定参数值,返回DataSet.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输入参数和返回值.
/// 示例.:
/// DataSet ds = ExecuteDataset(trans, "GetOrders", 24, 36);
/// </remarks>
/// <param name="transaction">事务</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
/// <returns>返回一个包含结果集的DataSet</returns>
public static DataSet ExecuteDataset(OleDbTransaction transaction, string spName, params object[] parameterValues)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); if ((parameterValues != null) && (parameterValues.Length > 0))
{
// 从缓存中加载存储过程参数
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection, spName); // 给存储过程参数分配值
AssignParameterValues(commandParameters, parameterValues); return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return ExecuteDataset(transaction, CommandType.StoredProcedure, spName);
}
} #endregion ExecuteDataset数据集命令结束 #region ExecuteReader 数据阅读器 /// <summary>
/// 枚举,标识数据库连接是由OleDbHelper提供还是由调用者提供
/// </summary>
private enum OleDbConnectionOwnership
{
/// <summary>由OleDbHelper提供连接</summary>
Internal,
/// <summary>由调用者提供连接</summary>
External
} /// <summary>
/// 执行指定数据库连接对象的数据阅读器.
/// </summary>
/// <remarks>
/// 如果是OleDbHelper打开连接,当连接关闭DataReader也将关闭.
/// 如果是调用都打开连接,DataReader由调用都管理.
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="transaction">一个有效的事务,或者为 'null'</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">OleDbParameters参数数组,如果没有参数则为'null'</param>
/// <param name="connectionOwnership">标识数据库连接对象是由调用者提供还是由OleDbHelper提供</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
private static OleDbDataReader ExecuteReader(OleDbConnection connection, OleDbTransaction transaction, CommandType commandType, string commandText, OleDbParameter[] commandParameters, OleDbConnectionOwnership connectionOwnership)
{
if (connection == null) throw new ArgumentNullException("connection"); bool mustCloseConnection = false;
// 创建命令
OleDbCommand cmd = new OleDbCommand();
try
{
PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection); // 创建数据阅读器
OleDbDataReader dataReader; if (connectionOwnership == OleDbConnectionOwnership.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 command
// then the OleDbReader can磘 set its values.
// When this happen, the parameters can磘 be used again in other command.
bool canClear = true;
foreach (OleDbParameter commandParameter in cmd.Parameters)
{
if (commandParameter.Direction != ParameterDirection.Input)
canClear = false;
} if (canClear)
{
cmd.Parameters.Clear();
} return dataReader;
}
catch
{
if (mustCloseConnection)
connection.Close();
throw;
}
} /// <summary>
/// 执行指定数据库连接字符串的数据阅读器.
/// </summary>
/// <remarks>
/// 示例:
/// OleDbDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders");
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)
{
return ExecuteReader(connectionString, commandType, commandText, (OleDbParameter[])null);
} /// <summary>
/// 执行指定数据库连接字符串的数据阅读器,指定参数.
/// </summary>
/// <remarks>
/// 示例:
/// OleDbDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <param name="commandParameters">OleDbParamter参数数组(new OleDbParameter("@prodid", 24))</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
OleDbConnection connection = null;
try
{
connection = new OleDbConnection(connectionString);
connection.Open(); return ExecuteReader(connection, null, commandType, commandText, commandParameters, OleDbConnectionOwnership.Internal);
}
catch
{
// If we fail to return the OleDbDatReader, we need to close the connection ourselves
if (connection != null) connection.Close();
throw;
} } /// <summary>
/// 执行指定数据库连接字符串的数据阅读器,指定参数值.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输出参数和返回值参数.
/// 示例:
/// OleDbDataReader dr = ExecuteReader(connString, "GetOrders", 24, 36);
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); if ((parameterValues != null) && (parameterValues.Length > 0))
{
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connectionString, spName); AssignParameterValues(commandParameters, parameterValues); return ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return ExecuteReader(connectionString, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定数据库连接对象的数据阅读器.
/// </summary>
/// <remarks>
/// 示例:
/// OleDbDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders");
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名或T-SQL语句</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReader(OleDbConnection connection, CommandType commandType, string commandText)
{
return ExecuteReader(connection, commandType, commandText, (OleDbParameter[])null);
} /// <summary>
/// [调用者方式]执行指定数据库连接对象的数据阅读器,指定参数.
/// </summary>
/// <remarks>
/// 示例:
/// OleDbDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandParameters">OleDbParamter参数数组</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReader(OleDbConnection connection, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
return ExecuteReader(connection, (OleDbTransaction)null, commandType, commandText, commandParameters, OleDbConnectionOwnership.External);
} /// <summary>
/// [调用者方式]执行指定数据库连接对象的数据阅读器,指定参数值.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输出参数和返回值参数.
/// 示例:
/// OleDbDataReader dr = ExecuteReader(conn, "GetOrders", 24, 36);
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="spName">T存储过程名</param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReader(OleDbConnection connection, string spName, params object[] parameterValues)
{
if (connection == null) throw new ArgumentNullException("connection");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); if ((parameterValues != null) && (parameterValues.Length > 0))
{
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection, spName); AssignParameterValues(commandParameters, parameterValues); return ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return ExecuteReader(connection, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值.
/// </summary>
/// <remarks>
/// 示例:
/// OleDbDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders");
/// </remarks>
/// <param name="transaction">一个有效的连接事务</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReader(OleDbTransaction transaction, CommandType commandType, string commandText)
{
return ExecuteReader(transaction, commandType, commandText, (OleDbParameter[])null);
} /// <summary>
/// [调用者方式]执行指定数据库事务的数据阅读器,指定参数.
/// </summary>
/// <remarks>
/// 示例:
/// OleDbDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="transaction">一个有效的连接事务</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="commandParameters">分配给命令的OleDbParamter参数数组</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReader(OleDbTransaction transaction, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, OleDbConnectionOwnership.External);
} /// <summary>
/// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输出参数和返回值参数.
///
/// 示例:
/// OleDbDataReader dr = ExecuteReader(trans, "GetOrders", 24, 36);
/// </remarks>
/// <param name="transaction">一个有效的连接事务</param>
/// <param name="spName">存储过程名称</param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReader(OleDbTransaction transaction, string spName, params object[] parameterValues)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0))
{
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection, spName); AssignParameterValues(commandParameters, parameterValues); return ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
// 没有参数值
return ExecuteReader(transaction, CommandType.StoredProcedure, spName);
}
} #endregion ExecuteReader数据阅读器 #region ExecuteScalar 返回结果集中的第一行第一列 /// <summary>
/// 执行指定数据库连接字符串的命令,返回结果集中的第一行第一列.
/// </summary>
/// <remarks>
/// 示例:
/// int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount");
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText)
{
// 执行参数为空的方法
return ExecuteScalar(connectionString, commandType, commandText, (OleDbParameter[])null);
} /// <summary>
/// 执行指定数据库连接字符串的命令,指定参数,返回结果集中的第一行第一列.
/// </summary>
/// <remarks>
/// 示例:
/// int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="commandParameters">分配给命令的OleDbParamter参数数组</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
// 创建并打开数据库连接对象,操作完成释放对象.
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open(); // 调用指定数据库连接字符串重载方法.
return ExecuteScalar(connection, commandType, commandText, commandParameters);
}
} /// <summary>
/// 执行指定数据库连接字符串的命令,指定参数值,返回结果集中的第一行第一列.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输出参数和返回值参数.
///
/// 示例:
/// int orderCount = (int)ExecuteScalar(connString, "GetOrderCount", 24, 36);
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="spName">存储过程名称</param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0))
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connectionString, spName); // 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues); // 调用重载方法
return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
// 没有参数值
return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定数据库连接对象的命令,返回结果集中的第一行第一列.
/// </summary>
/// <remarks>
/// 示例:
/// int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount");
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(OleDbConnection connection, CommandType commandType, string commandText)
{
// 执行参数为空的方法
return ExecuteScalar(connection, commandType, commandText, (OleDbParameter[])null);
} /// <summary>
/// 执行指定数据库连接对象的命令,指定参数,返回结果集中的第一行第一列.
/// </summary>
/// <remarks>
/// 示例:
/// int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="commandParameters">分配给命令的OleDbParamter参数数组</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(OleDbConnection connection, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
if (connection == null) throw new ArgumentNullException("connection"); // 创建OleDbCommand命令,并进行预处理
OleDbCommand cmd = new OleDbCommand(); bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (OleDbTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection); // 执行OleDbCommand命令,并返回结果.
object retval = cmd.ExecuteScalar(); // 清除参数,以便再次使用.
cmd.Parameters.Clear(); if (mustCloseConnection)
connection.Close(); return retval;
} /// <summary>
/// 执行指定数据库连接对象的命令,指定参数值,返回结果集中的第一行第一列.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输出参数和返回值参数.
///
/// 示例:
/// int orderCount = (int)ExecuteScalar(conn, "GetOrderCount", 24, 36);
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="spName">存储过程名称</param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(OleDbConnection connection, string spName, params object[] parameterValues)
{
if (connection == null) throw new ArgumentNullException("connection");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0))
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection, spName); // 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues); // 调用重载方法
return ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
// 没有参数值
return ExecuteScalar(connection, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定数据库事务的命令,返回结果集中的第一行第一列.
/// </summary>
/// <remarks>
/// 示例:
/// int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount");
/// </remarks>
/// <param name="transaction">一个有效的连接事务</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(OleDbTransaction transaction, CommandType commandType, string commandText)
{
// 执行参数为空的方法
return ExecuteScalar(transaction, commandType, commandText, (OleDbParameter[])null);
} /// <summary>
/// 执行指定数据库事务的命令,指定参数,返回结果集中的第一行第一列.
/// </summary>
/// <remarks>
/// 示例:
/// int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="transaction">一个有效的连接事务</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="commandParameters">分配给命令的OleDbParamter参数数组</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(OleDbTransaction transaction, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); // 创建OleDbCommand命令,并进行预处理
OleDbCommand cmd = new OleDbCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection); // 执行OleDbCommand命令,并返回结果.
object retval = cmd.ExecuteScalar(); // 清除参数,以便再次使用.
cmd.Parameters.Clear();
return retval;
} /// <summary>
/// 执行指定数据库事务的命令,指定参数值,返回结果集中的第一行第一列.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输出参数和返回值参数.
///
/// 示例:
/// int orderCount = (int)ExecuteScalar(trans, "GetOrderCount", 24, 36);
/// </remarks>
/// <param name="transaction">一个有效的连接事务</param>
/// <param name="spName">存储过程名称</param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(OleDbTransaction transaction, string spName, params object[] parameterValues)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0))
{
// PPull the parameters for this stored procedure from the parameter cache ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection, spName); // 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues); // 调用重载方法
return ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
// 没有参数值
return ExecuteScalar(transaction, CommandType.StoredProcedure, spName);
}
} #endregion ExecuteScalar #region FillDataset 填充数据集
/// <summary>
/// 执行指定数据库连接字符串的命令,映射数据表并填充数据集.
/// </summary>
/// <remarks>
/// 示例:
/// FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="dataSet">要填充结果集的DataSet实例</param>
/// <param name="tableNames">表映射的数据表数组
/// 用户定义的表名 (可有是实际的表名.)</param>
public static void FillDataset(string connectionString, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (dataSet == null) throw new ArgumentNullException("dataSet"); // 创建并打开数据库连接对象,操作完成释放对象.
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open(); // 调用指定数据库连接字符串重载方法.
FillDataset(connection, commandType, commandText, dataSet, tableNames);
}
} /// <summary>
/// 执行指定数据库连接字符串的命令,映射数据表并填充数据集.指定命令参数.
/// </summary>
/// <remarks>
/// 示例:
/// FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="commandParameters">分配给命令的OleDbParamter参数数组</param>
/// <param name="dataSet">要填充结果集的DataSet实例</param>
/// <param name="tableNames">表映射的数据表数组
/// 用户定义的表名 (可有是实际的表名.)
/// </param>
public static void FillDataset(string connectionString, CommandType commandType,
string commandText, DataSet dataSet, string[] tableNames,
params OleDbParameter[] commandParameters)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (dataSet == null) throw new ArgumentNullException("dataSet");
// 创建并打开数据库连接对象,操作完成释放对象.
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open(); // 调用指定数据库连接字符串重载方法.
FillDataset(connection, commandType, commandText, dataSet, tableNames, commandParameters);
}
} /// <summary>
/// 执行指定数据库连接字符串的命令,映射数据表并填充数据集,指定存储过程参数值.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输出参数和返回值参数.
///
/// 示例:
/// FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, 24);
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataSet">要填充结果集的DataSet实例</param>
/// <param name="tableNames">表映射的数据表数组
/// 用户定义的表名 (可有是实际的表名.)
/// </param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
public static void FillDataset(string connectionString, string spName,
DataSet dataSet, string[] tableNames,
params object[] parameterValues)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (dataSet == null) throw new ArgumentNullException("dataSet");
// 创建并打开数据库连接对象,操作完成释放对象.
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open(); // 调用指定数据库连接字符串重载方法.
FillDataset(connection, spName, dataSet, tableNames, parameterValues);
}
} /// <summary>
/// 执行指定数据库连接对象的命令,映射数据表并填充数据集.
/// </summary>
/// <remarks>
/// 示例:
/// FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="dataSet">要填充结果集的DataSet实例</param>
/// <param name="tableNames">表映射的数据表数组
/// 用户定义的表名 (可有是实际的表名.)
/// </param>
public static void FillDataset(OleDbConnection connection, CommandType commandType,
string commandText, DataSet dataSet, string[] tableNames)
{
FillDataset(connection, commandType, commandText, dataSet, tableNames, null);
} /// <summary>
/// 执行指定数据库连接对象的命令,映射数据表并填充数据集,指定参数.
/// </summary>
/// <remarks>
/// 示例:
/// FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="dataSet">要填充结果集的DataSet实例</param>
/// <param name="tableNames">表映射的数据表数组
/// 用户定义的表名 (可有是实际的表名.)
/// </param>
/// <param name="commandParameters">分配给命令的OleDbParamter参数数组</param>
public static void FillDataset(OleDbConnection connection, CommandType commandType,
string commandText, DataSet dataSet, string[] tableNames,
params OleDbParameter[] commandParameters)
{
FillDataset(connection, null, commandType, commandText, dataSet, tableNames, commandParameters);
} /// <summary>
/// 执行指定数据库连接对象的命令,映射数据表并填充数据集,指定存储过程参数值.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输出参数和返回值参数.
///
/// 示例:
/// FillDataset(conn, "GetOrders", ds, new string[] {"orders"}, 24, 36);
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataSet">要填充结果集的DataSet实例</param>
/// <param name="tableNames">表映射的数据表数组
/// 用户定义的表名 (可有是实际的表名.)
/// </param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
public static void FillDataset(OleDbConnection connection, string spName,
DataSet dataSet, string[] tableNames,
params object[] parameterValues)
{
if (connection == null) throw new ArgumentNullException("connection");
if (dataSet == null) throw new ArgumentNullException("dataSet");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0))
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection, spName); // 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues); // 调用重载方法
FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters);
}
else
{
// 没有参数值
FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames);
}
} /// <summary>
/// 执行指定数据库事务的命令,映射数据表并填充数据集.
/// </summary>
/// <remarks>
/// 示例:
/// FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});
/// </remarks>
/// <param name="transaction">一个有效的连接事务</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="dataSet">要填充结果集的DataSet实例</param>
/// <param name="tableNames">表映射的数据表数组
/// 用户定义的表名 (可有是实际的表名.)
/// </param>
public static void FillDataset(OleDbTransaction transaction, CommandType commandType,
string commandText,
DataSet dataSet, string[] tableNames)
{
FillDataset(transaction, commandType, commandText, dataSet, tableNames, null);
} /// <summary>
/// 执行指定数据库事务的命令,映射数据表并填充数据集,指定参数.
/// </summary>
/// <remarks>
/// 示例:
/// FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="transaction">一个有效的连接事务</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="dataSet">要填充结果集的DataSet实例</param>
/// <param name="tableNames">表映射的数据表数组
/// 用户定义的表名 (可有是实际的表名.)
/// </param>
/// <param name="commandParameters">分配给命令的OleDbParamter参数数组</param>
public static void FillDataset(OleDbTransaction transaction, CommandType commandType,
string commandText, DataSet dataSet, string[] tableNames,
params OleDbParameter[] commandParameters)
{
FillDataset(transaction.Connection, transaction, commandType, commandText, dataSet, tableNames, commandParameters);
} /// <summary>
/// 执行指定数据库事务的命令,映射数据表并填充数据集,指定存储过程参数值.
/// </summary>
/// <remarks>
/// 此方法不提供访问存储过程输出参数和返回值参数.
///
/// 示例:
/// FillDataset(trans, "GetOrders", ds, new string[]{"orders"}, 24, 36);
/// </remarks>
/// <param name="transaction">一个有效的连接事务</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataSet">要填充结果集的DataSet实例</param>
/// <param name="tableNames">表映射的数据表数组
/// 用户定义的表名 (可有是实际的表名.)
/// </param>
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
public static void FillDataset(OleDbTransaction transaction, string spName,
DataSet dataSet, string[] tableNames,
params object[] parameterValues)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
if (dataSet == null) throw new ArgumentNullException("dataSet");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果有参数值
if ((parameterValues != null) && (parameterValues.Length > 0))
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection, spName); // 给存储过程参数赋值
AssignParameterValues(commandParameters, parameterValues); // 调用重载方法
FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters);
}
else
{
// 没有参数值
FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames);
}
} /// <summary>
/// [私有方法][内部调用]执行指定数据库连接对象/事务的命令,映射数据表并填充数据集,DataSet/TableNames/OleDbParameters.
/// </summary>
/// <remarks>
/// 示例:
/// FillDataset(conn, trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new OleDbParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="transaction">一个有效的连接事务</param>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名称或T-SQL语句</param>
/// <param name="dataSet">要填充结果集的DataSet实例</param>
/// <param name="tableNames">表映射的数据表数组
/// 用户定义的表名 (可有是实际的表名.)
/// </param>
/// <param name="commandParameters">分配给命令的OleDbParamter参数数组</param>
private static void FillDataset(OleDbConnection connection, OleDbTransaction transaction, CommandType commandType,
string commandText, DataSet dataSet, string[] tableNames,
params OleDbParameter[] commandParameters)
{
if (connection == null) throw new ArgumentNullException("connection");
if (dataSet == null) throw new ArgumentNullException("dataSet"); // 创建OleDbCommand命令,并进行预处理
OleDbCommand command = new OleDbCommand();
bool mustCloseConnection = false;
PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection); // 执行命令
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command))
{ // 追加表映射
if (tableNames != null && tableNames.Length > 0)
{
string tableName = "Table";
for (int index = 0; index < tableNames.Length; index++)
{
if (tableNames[index] == null || tableNames[index].Length == 0) throw new ArgumentException("The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames");
dataAdapter.TableMappings.Add(tableName, tableNames[index]);
tableName += (index + 1).ToString();
}
} // 填充数据集使用默认表名称
dataAdapter.Fill(dataSet); // 清除参数,以便再次使用.
command.Parameters.Clear();
} if (mustCloseConnection)
connection.Close();
}
#endregion #region UpdateDataset 更新数据集
/// <summary>
/// 执行数据集更新到数据库,指定inserted, updated, or deleted命令.
/// </summary>
/// <remarks>
/// 示例:
/// UpdateDataset(conn, insertCommand, deleteCommand, updateCommand, dataSet, "Order");
/// </remarks>
/// <param name="insertCommand">[追加记录]一个有效的T-SQL语句或存储过程</param>
/// <param name="deleteCommand">[删除记录]一个有效的T-SQL语句或存储过程</param>
/// <param name="updateCommand">[更新记录]一个有效的T-SQL语句或存储过程</param>
/// <param name="dataSet">要更新到数据库的DataSet</param>
/// <param name="tableName">要更新到数据库的DataTable</param>
public static void UpdateDataset(OleDbCommand insertCommand, OleDbCommand deleteCommand, OleDbCommand updateCommand, DataSet dataSet, string tableName)
{
if (insertCommand == null) throw new ArgumentNullException("insertCommand");
if (deleteCommand == null) throw new ArgumentNullException("deleteCommand");
if (updateCommand == null) throw new ArgumentNullException("updateCommand");
if (tableName == null || tableName.Length == 0) throw new ArgumentNullException("tableName"); // 创建OleDbDataAdapter,当操作完成后释放.
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter())
{
// 设置数据适配器命令
dataAdapter.UpdateCommand = updateCommand;
dataAdapter.InsertCommand = insertCommand;
dataAdapter.DeleteCommand = deleteCommand; // 更新数据集改变到数据库
dataAdapter.Update(dataSet, tableName); // 提交所有改变到数据集.
dataSet.AcceptChanges();
}
}
#endregion #region CreateCommand 创建一条OleDbCommand命令
/// <summary>
/// 创建OleDbCommand命令,指定数据库连接对象,存储过程名和参数.
/// </summary>
/// <remarks>
/// 示例:
/// OleDbCommand command = CreateCommand(conn, "AddCustomer", "CustomerID", "CustomerName");
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="spName">存储过程名称</param>
/// <param name="sourceColumns">源表的列名称数组</param>
/// <returns>返回OleDbCommand命令</returns>
public static OleDbCommand CreateCommand(OleDbConnection connection, string spName, params string[] sourceColumns)
{
if (connection == null) throw new ArgumentNullException("connection");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 创建命令
OleDbCommand cmd = new OleDbCommand(spName, connection);
cmd.CommandType = CommandType.StoredProcedure; // 如果有参数值
if ((sourceColumns != null) && (sourceColumns.Length > 0))
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection, spName); // 将源表的列到映射到DataSet命令中.
for (int index = 0; index < sourceColumns.Length; index++)
commandParameters[index].SourceColumn = sourceColumns[index]; // Attach the discovered parameters to the OleDbCommand object
AttachParameters(cmd, commandParameters);
} return cmd;
}
#endregion #region ExecuteNonQueryTypedParams 类型化参数(DataRow)
/// <summary>
/// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回受影响的行数.
/// </summary>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataRow">使用DataRow作为参数值</param>
/// <returns>返回影响的行数</returns>
public static int ExecuteNonQueryTypedParams(String connectionString, String spName, DataRow dataRow)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connectionString, spName); // 分配参数值
AssignParameterValues(commandParameters, dataRow); return OleDbHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return OleDbHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回受影响的行数.
/// </summary>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataRow">使用DataRow作为参数值</param>
/// <returns>返回影响的行数</returns>
public static int ExecuteNonQueryTypedParams(OleDbConnection connection, String spName, DataRow dataRow)
{
if (connection == null) throw new ArgumentNullException("connection");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection, spName); // 分配参数值
AssignParameterValues(commandParameters, dataRow); return OleDbHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return OleDbHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定连接数据库事物的存储过程,使用DataRow做为参数值,返回受影响的行数.
/// </summary>
/// <param name="transaction">一个有效的连接事务 object</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataRow">使用DataRow作为参数值</param>
/// <returns>返回影响的行数</returns>
public static int ExecuteNonQueryTypedParams(OleDbTransaction transaction, String spName, DataRow dataRow)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // Sf the row has values, the store procedure parameters must be initialized
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection, spName); // 分配参数值
AssignParameterValues(commandParameters, dataRow); return OleDbHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return OleDbHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
}
}
#endregion #region ExecuteDatasetTypedParams 类型化参数(DataRow)
/// <summary>
/// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回DataSet.
/// </summary>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataRow">使用DataRow作为参数值</param>
/// <returns>返回一个包含结果集的DataSet.</returns>
public static DataSet ExecuteDatasetTypedParams(string connectionString, String spName, DataRow dataRow)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); //如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connectionString, spName); // 分配参数值
AssignParameterValues(commandParameters, dataRow); return OleDbHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return OleDbHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回DataSet.
/// </summary>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataRow">使用DataRow作为参数值</param>
/// <returns>返回一个包含结果集的DataSet.</returns>
///
public static DataSet ExecuteDatasetTypedParams(OleDbConnection connection, String spName, DataRow dataRow)
{
if (connection == null) throw new ArgumentNullException("connection");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection, spName); // 分配参数值
AssignParameterValues(commandParameters, dataRow); return OleDbHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return OleDbHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定连接数据库事务的存储过程,使用DataRow做为参数值,返回DataSet.
/// </summary>
/// <param name="transaction">一个有效的连接事务 object</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataRow">使用DataRow作为参数值</param>
/// <returns>返回一个包含结果集的DataSet.</returns>
public static DataSet ExecuteDatasetTypedParams(OleDbTransaction transaction, String spName, DataRow dataRow)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection, spName); // 分配参数值
AssignParameterValues(commandParameters, dataRow); return OleDbHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return OleDbHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName);
}
} #endregion #region ExecuteReaderTypedParams 类型化参数(DataRow)
/// <summary>
/// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回DataReader.
/// </summary>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataRow">使用DataRow作为参数值</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReaderTypedParams(String connectionString, String spName, DataRow dataRow)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connectionString, spName); // 分配参数值
AssignParameterValues(commandParameters, dataRow); return OleDbHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return OleDbHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回DataReader.
/// </summary>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataRow">使用DataRow作为参数值</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReaderTypedParams(OleDbConnection connection, String spName, DataRow dataRow)
{
if (connection == null) throw new ArgumentNullException("connection");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection, spName); // 分配参数值
AssignParameterValues(commandParameters, dataRow); return OleDbHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return OleDbHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定连接数据库事物的存储过程,使用DataRow做为参数值,返回DataReader.
/// </summary>
/// <param name="transaction">一个有效的连接事务 object</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataRow">使用DataRow作为参数值</param>
/// <returns>返回包含结果集的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReaderTypedParams(OleDbTransaction transaction, String spName, DataRow dataRow)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection, spName); // 分配参数值
AssignParameterValues(commandParameters, dataRow); return OleDbHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return OleDbHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName);
}
}
#endregion #region ExecuteScalarTypedParams 类型化参数(DataRow)
/// <summary>
/// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回结果集中的第一行第一列.
/// </summary>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataRow">使用DataRow作为参数值</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalarTypedParams(String connectionString, String spName, DataRow dataRow)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connectionString, spName); // 分配参数值
AssignParameterValues(commandParameters, dataRow); return OleDbHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return OleDbHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回结果集中的第一行第一列.
/// </summary>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataRow">使用DataRow作为参数值</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalarTypedParams(OleDbConnection connection, String spName, DataRow dataRow)
{
if (connection == null) throw new ArgumentNullException("connection");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection, spName); // 分配参数值
AssignParameterValues(commandParameters, dataRow); return OleDbHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return OleDbHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName);
}
} /// <summary>
/// 执行指定连接数据库事务的存储过程,使用DataRow做为参数值,返回结果集中的第一行第一列.
/// </summary>
/// <param name="transaction">一个有效的连接事务 object</param>
/// <param name="spName">存储过程名称</param>
/// <param name="dataRow">使用DataRow作为参数值</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalarTypedParams(OleDbTransaction transaction, String spName, DataRow dataRow)
{
if (transaction == null) throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); // 如果row有值,存储过程必须初始化.
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection, spName); // 分配参数值
AssignParameterValues(commandParameters, dataRow); return OleDbHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return OleDbHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName);
}
}
#endregion } /// <summary>
/// OleDbHelperParameterCache提供缓存存储过程参数,并能够在运行时从存储过程中探索参数.
/// </summary>
public sealed class OleDbHelperParameterCache
{
#region 私有方法,字段,构造函数
// 私有构造函数,妨止类被实例化.
private OleDbHelperParameterCache() { } // 这个方法要注意
private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable()); /// <summary>
/// 探索运行时的存储过程,返回OleDbParameter参数数组.
/// 初始化参数值为 DBNull.Value.
/// </summary>
/// <param name="connection">一个有效的数据库连接</param>
/// <param name="spName">存储过程名称</param>
/// <param name="includeReturnValueParameter">是否包含返回值参数</param>
/// <returns>返回OleDbParameter参数数组</returns>
private static OleDbParameter[] DiscoverSpParameterSet(OleDbConnection connection, string spName, bool includeReturnValueParameter)
{
if (connection == null) throw new ArgumentNullException("connection");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); OleDbCommand cmd = new OleDbCommand(spName, connection);
cmd.CommandType = CommandType.StoredProcedure; connection.Open();
// 检索cmd指定的存储过程的参数信息,并填充到cmd的Parameters参数集中.
OleDbCommandBuilder.DeriveParameters(cmd);
connection.Close();
// 如果不包含返回值参数,将参数集中的每一个参数删除.
if (!includeReturnValueParameter)
{
cmd.Parameters.RemoveAt(0);
} // 创建参数数组
OleDbParameter[] discoveredParameters = new OleDbParameter[cmd.Parameters.Count];
// 将cmd的Parameters参数集复制到discoveredParameters数组.
cmd.Parameters.CopyTo(discoveredParameters, 0); // 初始化参数值为 DBNull.Value.
foreach (OleDbParameter discoveredParameter in discoveredParameters)
{
discoveredParameter.Value = DBNull.Value;
}
return discoveredParameters;
} /// <summary>
/// OleDbParameter参数数组的深层拷贝.
/// </summary>
/// <param name="originalParameters">原始参数数组</param>
/// <returns>返回一个同样的参数数组</returns>
private static OleDbParameter[] CloneParameters(OleDbParameter[] originalParameters)
{
OleDbParameter[] clonedParameters = new OleDbParameter[originalParameters.Length]; for (int i = 0, j = originalParameters.Length; i < j; i++)
{
clonedParameters[i] = (OleDbParameter)((ICloneable)originalParameters[i]).Clone();
} return clonedParameters;
} #endregion 私有方法,字段,构造函数结束 #region 缓存方法 /// <summary>
/// 追加参数数组到缓存.
/// </summary>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandText">存储过程名或SQL语句</param>
/// <param name="commandParameters">要缓存的参数数组</param>
public static void CacheParameterSet(string connectionString, string commandText, params OleDbParameter[] commandParameters)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); string hashKey = connectionString + ":" + commandText; paramCache[hashKey] = commandParameters;
} /// <summary>
/// 从缓存中获取参数数组.
/// </summary>
/// <param name="connectionString">一个有效的数据库连接字符</param>
/// <param name="commandText">存储过程名或SQL语句</param>
/// <returns>参数数组</returns>
public static OleDbParameter[] GetCachedParameterSet(string connectionString, string commandText)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); string hashKey = connectionString + ":" + commandText; OleDbParameter[] cachedParameters = paramCache[hashKey] as OleDbParameter[];
if (cachedParameters == null)
{
return null;
}
else
{
return CloneParameters(cachedParameters);
}
} #endregion 缓存方法结束 #region 检索指定的存储过程的参数集 /// <summary>
/// 返回指定的存储过程的参数集
/// </summary>
/// <remarks>
/// 这个方法将查询数据库,并将信息存储到缓存.
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符</param>
/// <param name="spName">存储过程名</param>
/// <returns>返回OleDbParameter参数数组</returns>
public static OleDbParameter[] GetSpParameterSet(string connectionString, string spName)
{
return GetSpParameterSet(connectionString, spName, false);
} /// <summary>
/// 返回指定的存储过程的参数集
/// </summary>
/// <remarks>
/// 这个方法将查询数据库,并将信息存储到缓存.
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符.</param>
/// <param name="spName">存储过程名</param>
/// <param name="includeReturnValueParameter">是否包含返回值参数</param>
/// <returns>返回OleDbParameter参数数组</returns>
public static OleDbParameter[] GetSpParameterSet(string connectionString, string spName, bool includeReturnValueParameter)
{
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); using (OleDbConnection connection = new OleDbConnection(connectionString))
{
return GetSpParameterSetInternal(connection, spName, includeReturnValueParameter);
}
} /// <summary>
/// [内部]返回指定的存储过程的参数集(使用连接对象).
/// </summary>
/// <remarks>
/// 这个方法将查询数据库,并将信息存储到缓存.
/// </remarks>
/// <param name="connection">一个有效的数据库连接字符</param>
/// <param name="spName">存储过程名</param>
/// <returns>返回OleDbParameter参数数组</returns>
internal static OleDbParameter[] GetSpParameterSet(OleDbConnection connection, string spName)
{
return GetSpParameterSet(connection, spName, false);
} /// <summary>
/// [内部]返回指定的存储过程的参数集(使用连接对象)
/// </summary>
/// <remarks>
/// 这个方法将查询数据库,并将信息存储到缓存.
/// </remarks>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="spName">存储过程名</param>
/// <param name="includeReturnValueParameter">
/// 是否包含返回值参数
/// </param>
/// <returns>返回OleDbParameter参数数组</returns>
internal static OleDbParameter[] GetSpParameterSet(OleDbConnection connection, string spName, bool includeReturnValueParameter)
{
if (connection == null) throw new ArgumentNullException("connection");
using (OleDbConnection clonedConnection = (OleDbConnection)((ICloneable)connection).Clone())
{
return GetSpParameterSetInternal(clonedConnection, spName, includeReturnValueParameter);
}
} /// <summary>
/// [私有]返回指定的存储过程的参数集(使用连接对象)
/// </summary>
/// <param name="connection">一个有效的数据库连接对象</param>
/// <param name="spName">存储过程名</param>
/// <param name="includeReturnValueParameter">是否包含返回值参数</param>
/// <returns>返回OleDbParameter参数数组</returns>
private static OleDbParameter[] GetSpParameterSetInternal(OleDbConnection connection, string spName, bool includeReturnValueParameter)
{
if (connection == null) throw new ArgumentNullException("connection");
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName"); string hashKey = connection.ConnectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter" : ""); OleDbParameter[] cachedParameters; cachedParameters = paramCache[hashKey] as OleDbParameter[];
if (cachedParameters == null)
{
OleDbParameter[] spParameters = DiscoverSpParameterSet(connection, spName, includeReturnValueParameter);
paramCache[hashKey] = spParameters;
cachedParameters = spParameters;
} return CloneParameters(cachedParameters);
} #endregion 参数集检索结束 }

C#---OleDbHelper的更多相关文章

  1. OleDbHelper

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.D ...

  2. [aspx]控件及代码小例

    1. 原生 asp 方式遍历 DataTable  2. aspx 的控件 Repeater 后台绑定 <%-- DataTable dt = OleDbHelper.GetTable(&quo ...

  3. C#处理Excel

    C#处理Excel C#处理Excel 前言 OleDb 具体操作 NPOI 具体操作 Excel C# NPOI OleDb 前言 最近需要对Excel进行加密解密操作,本身是一个简单的事情,通过 ...

  4. C# 操作数据库就的那点代码

    操作数据库的那点代码,别在费劲每个数据库都写一遍SQLHelper,SQLiteHelper,OleDbHelper,了,这里都有了. 接口不发了,自己抽取定义就行了. public abstract ...

  5. c# 轻量级 ORM 框架 之 DBHelper 实现 (三)

    周末了比较清闲,把自己的orm框架整理了下,开源了. 已经做出来的东西通常感觉有些简单,一些新手或许听到"框架"一类的词觉得有些"高深",简单来说orm就是把a ...

  6. wpf:DataGrid使用

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" ...

  7. SqlHelper 帮助文档及详解--项目初步搭建

    微软SqlHelper类中文注释和使用方法 相关链接: http://blog.csdn.net/itmaxin/article/details/7609566 SqlHelper.cs是N年前微软出 ...

  8. asp.net读取Access数据库。

    注:数据库(表名 job  id 工作id ,job工作字段) 数据库放在app_data文件中.名称为database.mdb 如果用codesmith生成,选择的数据库连接类型如下图: 项目结构图 ...

  9. Asp.net 获取图片列表并打包下载

    先引用:ICSharpCode.SharpZipLib.dll 后台代码: using System.IO; using ICSharpCode.SharpZipLib.Zip; using ICSh ...

随机推荐

  1. python使用笔记009--小练习

    1.密码生成器 1 ''' 2 1.写一个生产密码的程序,输入几,就产生几条密码,密码产生的不重复. 3 要求密码:长度6-12,密码必须包含 大写字母.小写字母.数字 4 产生完密码后存到一个文件里 ...

  2. C语言:最小公倍数

    //求最小公倍数 #include <stdio.h> main() { int m,n,i,k,max,min; scanf("%d,%d",&m,& ...

  3. java集合(1)-概述

    Java集合类是一种特别有用的工具类,可用于存储数量不等的对象,并可以实现常用的数据结构,如栈,队列等,此外Java集合还可以用于保存具有映射关系的关联数组.java集合大致可分为Set,List,Q ...

  4. PAT乙级:1092 最好吃的月饼 (20分)

    PAT乙级:1092 最好吃的月饼 (20分) 题干 月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种. 若想评比出一种"最好吃"的月饼,那势必在吃货界引发一场腥风 ...

  5. 【每日算法】存在重复元素 II

    题目描述 这是 LeetCode 上的 219. 存在重复元素 II, 难度为 [简单] 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nu ...

  6. Redis主从复制那点事

    ​     我们在 Redis持久化机制你学会了吗?学习了AOF和RDB,如果Redis宕机,他们分别通过回放日志和重新读入RDB文件的方式恢复数据,从而提高可靠性.我们今天来想这么一个问题,假如我们 ...

  7. 类似express的路由封装方式

    1.原理 主要过程涉及三个元素的定义,如下: //用于承载一系列方法的对象 var funObj = {}; //用于根据条件执行funObj中的方法 var app = function(){}; ...

  8. 关于intouch/ifix嵌入视频控件并使用(海康,大华)

    2017年下半年项目开始接触利用intouch工控软件来进行项目二次开发.其中关于驱动的问题始终是上位机的重中之重,暂且不表(嘿嘿--),首先遇到的问题就是在弹窗中嵌入视频控件,监控设备的开停状态.经 ...

  9. js学习笔记之字符串统计出现次数最多的字符

    1.方法:把字符串中字符替换为空,并和之前的字符串的长度相减,得到已经被替换的字符的数量,依次比较获得出现次数最多的字符 var str ="adadfdfseffserfefsefseef ...

  10. VS Code的插件安装位置改变

    VS Code的相关配置 VS Code的插件安装位置改变 可以通过创建连接,将默认的extensions位置,改变到D盘 Windows 链接彻底解决 vscode插件安装位置问题 mklink / ...