ITPub博客

首页 > 数据库 > 数据库开发技术 > TREEVIEW只输入表名,父ID,节点ID,节点名就得到树型结构之八(转)

TREEVIEW只输入表名,父ID,节点ID,节点名就得到树型结构之八(转)

原创 数据库开发技术 作者:BugRobber 时间:2007-07-06 21:17:31 0 删除 编辑
///
/// This enum is used to indicate whether the connection was provided by the caller, or created by SqlHelper, so that
/// we can set the appropriate CommandBehavior when calling ExecuteReader()
///

private enum SqlConnectionOwnership
{
/// Connection is owned and managed by SqlHelper
Internal,
/// Connection is owned and managed by the caller
External
}

///


/// Create and prepare a SqlCommand, and call ExecuteReader with the appropriate CommandBehavior.
///

///
/// If we created and opened the connection, we want the connection to be closed when the DataReader is closed.
///
/// If the caller provided the connection, we want to leave it to them to manage.
///

/// A valid SqlConnection, on which to execute this command
/// A valid SqlTransaction, or 'null'
/// The CommandType (stored procedure, text, etc.)
/// The stored procedure name or T-SQL command
/// An array of SqlParameters to be associated with the command or 'null' if no parameters are required
/// Indicates whether the connection parameter was provided by the caller, or created by SqlHelper
/// SqlDataReader containing the results of the command
private static SqlDataReader ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership)
{
if( connection == null ) throw new ArgumentNullException( "connection" );

bool mustCloseConnection = false;
// Create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );

// Create a reader
SqlDataReader dataReader;

// Call ExecuteReader with the appropriate CommandBehavior
if (connectionOwnership == SqlConnectionOwnership.External)
{
dataReader = cmd.ExecuteReader();
}
else
{
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}

// Detach the SqlParameters from the command object, so they can be used again.
// 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 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;
}
}

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8781179/viewspace-924787/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    372
  • 访问量
    200926