本文整理汇总了C#中Microsoft.Practices.EnterpriseLibrary.Data.Database.GetNewOpenConnection方法的典型用法代码示例。如果您正苦于以下问题:C# Database.GetNewOpenConnection方法的具体用法?C# Database.GetNewOpenConnection怎么用?C# Database.GetNewOpenConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Practices.EnterpriseLibrary.Data.Database
的用法示例。
在下文中一共展示了Database.GetNewOpenConnection方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetConnection
public static DbConnection GetConnection(Database db)
{
Dictionary<string, DbConnection> dictionary;
DbConnection newOpenConnection;
Transaction current = Transaction.Current;
if (current == null)
{
return null;
}
transactionConnections.TryGetValue(current, out dictionary);
if (dictionary != null)
{
dictionary.TryGetValue(db.ConnectionString.ToString(), out newOpenConnection);
if (newOpenConnection != null)
{
return newOpenConnection;
}
}
else
{
dictionary = new Dictionary<string, DbConnection>();
lock (transactionConnections)
{
transactionConnections.Add(current, dictionary);
}
}
if (dictionary.ContainsKey(db.ConnectionString))
{
return dictionary[db.ConnectionString];
}
newOpenConnection = db.GetNewOpenConnection();
current.TransactionCompleted += new TransactionCompletedEventHandler(TransactionScopeConnections.OnTransactionCompleted);
dictionary.Add(db.ConnectionString, newOpenConnection);
return newOpenConnection;
}
示例2: GetConnection
/// <summary>
/// Returns a connection for the current transaction. This will be an existing <see cref="DbConnection"/>
/// instance or a new one if there is a <see cref="TransactionScope"/> active. Otherwise this method
/// returns null.
/// </summary>
/// <param name="db"></param>
/// <returns>Either a <see cref="DbConnection"/> instance or null.</returns>
public static DatabaseConnectionWrapper GetConnection(Database db)
{
Transaction currentTransaction = Transaction.Current;
if (currentTransaction == null)
return null;
Dictionary<string, DatabaseConnectionWrapper> connectionList;
DatabaseConnectionWrapper connection;
lock (transactionConnections)
{
if (!transactionConnections.TryGetValue(currentTransaction, out connectionList))
{
// We don't have a list for this transaction, so create a new one
connectionList = new Dictionary<string, DatabaseConnectionWrapper>();
transactionConnections.Add(currentTransaction, connectionList);
// We need to know when this previously unknown transaction is completed too
currentTransaction.TransactionCompleted += OnTransactionCompleted;
}
}
lock (connectionList)
{
// Next we'll see if there is already a connection. If not, we'll create a new connection and add it
// to the transaction's list of connections.
// This collection should only be modified by the thread where the transaction scope was created
// while the transaction scope is active.
// However there's no documentation to confirm this, so we err on the safe side and lock.
if (!connectionList.TryGetValue(db.ConnectionString, out connection))
{
// we're betting the cost of acquiring a new finer-grained lock is less than
// that of opening a new connection, and besides this allows threads to work in parallel
var dbConnection = db.GetNewOpenConnection();
connection = new DatabaseConnectionWrapper(dbConnection);
connectionList.Add(db.ConnectionString, connection);
}
connection.AddRef();
}
return connection;
}
示例3: GetConnection
/// <summary>
/// Returns a connection for the current transaction. This will be an existing <see cref="DbConnection"/>
/// instance or a new one if there is a <see cref="TransactionScope"/> active. Otherwise this method
/// returns null.
/// </summary>
/// <param name="db"></param>
/// <returns>Either a <see cref="DbConnection"/> instance or null.</returns>
public static DbConnection GetConnection(Database db)
{
Transaction currentTransaction = Transaction.Current;
if (currentTransaction == null)
return null;
Dictionary<string, DbConnection> connectionList;
transactionConnections.TryGetValue(currentTransaction, out connectionList);
DbConnection connection;
if (connectionList != null)
{
connectionList.TryGetValue(db.ConnectionString.ToString(), out connection);
if (connection != null)
return connection;
}
else
{ // We don't have a list for this transaction, so create a new one
connectionList = new Dictionary<string, DbConnection>();
lock (transactionConnections)
transactionConnections.Add(currentTransaction, connectionList);
}
//
// Next we'll see if there is already a connection. If not, we'll create a new connection and add it
// to the transaction's list of connections.
//
if (connectionList.ContainsKey(db.ConnectionString))
connection = connectionList[db.ConnectionString];
else
{
connection = db.GetNewOpenConnection();
currentTransaction.TransactionCompleted += new TransactionCompletedEventHandler(OnTransactionCompleted);
connectionList.Add(db.ConnectionString, connection);
}
return connection;
}