在上一篇文章中已經分析了AdoHelper的部分代碼,接下來將繼續分析剩余的部分代碼,這裡分析ExecuteNonQuery方法的實現,代碼塊1-1:
// <summary>
//通過提供的參數,執行無結果集的數據庫操作命令
// 並返回執行數據庫操作所影響的行數。
// </summary>
// <param name="connectionString">數據庫連接字符串</param>
// <param name="commandType">執行命令的類型(存儲過程或T-SQL,等等)</param>
// <param name="commandText">存儲過程名稱或者T-SQL命令行<</param>
// <param name="commandParameters">執行命令所需的參數數組</param>
// <returns>返回通過執行命令所影響的行數</returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
{
IDbCommand cmd = DbFactory.CreateDbCommand();
using (IDbConnection conn = DbFactory.CreateDbConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
//=============================================================================================
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText)
{
IDbCommand cmd = DbFactory.CreateDbCommand();
using (IDbConnection conn = DbFactory.CreateDbConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
//=============================================================================================
public static int ExecuteNonQuery(IDbConnection connection, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
{
IDbCommand cmd = DbFactory.CreateDbCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
//=============================================================================================
public static int ExecuteNonQuery(IDbConnection connection, CommandType cmdType, string cmdText)
{
IDbCommand cmd = DbFactory.CreateDbCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, null);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
//=============================================================================================
// <summary>
//通過提供的參數,執行無結果集返回的數據庫操作命令
//並返回執行數據庫操作所影響的行數。
// </summary>
// <remarks>
// e.g.:
// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
// </remarks>
// <param name="trans">sql事務對象</param>
// <param name="commandType">執行命令的類型(存儲過程或T-SQL,等等)</param>
// <param name="commandText">存儲過程名稱或者T-SQL命令行<</param>
// <param name="commandParameters">執行命令所需的參數數組</param>
// <returns>返回通過執行命令所影響的行數</returns>
public static int ExecuteNonQuery(IDbTransaction trans, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
{
IDbConnection conn = null;
if (trans == null)
{
conn = DbFactory.CreateDbConnection(ConnectionString);
}
else
{
conn = trans.Connection;
}
IDbCommand cmd = DbFactory.CreateDbCommand();
PrepareCommand(cmd, conn, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
//=============================================================================================
public static int ExecuteNonQuery(IDbTransaction trans, CommandType cmdType, string cmdText)
{
IDbCommand cmd = DbFactory.CreateDbCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, null);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
//=============================================================================================