本文整理汇总了C#中IDbFactory.CreateConnection方法的典型用法代码示例。如果您正苦于以下问题:C# IDbFactory.CreateConnection方法的具体用法?C# IDbFactory.CreateConnection怎么用?C# IDbFactory.CreateConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDbFactory
的用法示例。
在下文中一共展示了IDbFactory.CreateConnection方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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(string connectionString, IDbFactory dbFactory)
{
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(connectionString, 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(connectionString))
{
connection = connectionList[connectionString];
}
else
{
connection = dbFactory.CreateConnection(connectionString);
connection.Open();
currentTransaction.TransactionCompleted += new TransactionCompletedEventHandler(OnTransactionCompleted);
connectionList.Add(connectionString, connection);
}
return connection;
}
示例2: GetOpenConnection
private static ConnectionWrapper<DbConnection> GetOpenConnection(string connectionString, IDbFactory factory,
bool disposeInnerConnection)
{
return TransactionScopeConnections.GetOpenConnection(connectionString, () => factory.CreateConnection(), disposeInnerConnection);
}
示例3: Context
public Transaction Transaction; // used to keep track of transaction participation.
#endregion Fields
#region Constructors
// to restore lock timeout on ConnectionScope.Dispose().
public Context(IDbFactory factory, string connectionString)
{
this.Factory = factory;
this.Connection = Factory.CreateConnection();
this.Connection.ConnectionString = connectionString;
}