本文整理汇总了C#中IDatabaseProvider.CreateConnection方法的典型用法代码示例。如果您正苦于以下问题:C# IDatabaseProvider.CreateConnection方法的具体用法?C# IDatabaseProvider.CreateConnection怎么用?C# IDatabaseProvider.CreateConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDatabaseProvider
的用法示例。
在下文中一共展示了IDatabaseProvider.CreateConnection方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetConnectionProperty
/// <summary>
/// Takes a property and database provider, then determines how to populate that property
/// based on type.
/// </summary>
/// <param name="sender">The object in which the connection property resides.</param>
/// <param name="activeConnectionProperties">
/// A collection of properties that have already been populated.</param>
/// <param name="provider">The database provider that will be used for fetching connections.</param>
/// <param name="databaseName">The name of the database that a connection should be made to.</param>
/// <param name="connectionProperty">The property that needs to be populated on the sender object.</param>
private static void SetConnectionProperty(
object sender,
List<PropertyInfo> activeConnectionProperties,
IDatabaseProvider<IDbConnection> provider,
string databaseName,
PropertyInfo connectionProperty)
{
if (typeof(IDbConnection).IsAssignableFrom(connectionProperty.PropertyType))
{
// Property should be populated with an opened connection instance to the target database.
IDbConnection newConnection = provider.CreateConnection(databaseName);
Connections.Add(newConnection);
connectionProperty.SetValue(sender, newConnection);
activeConnectionProperties.Add(connectionProperty);
}
else if (typeof(Func<IDbConnection>).IsAssignableFrom(connectionProperty.PropertyType))
{
// Property should be populated with a delegate that can provide a connection instance when requested.
connectionProperty.SetValue(
sender,
(Func<IDbConnection>)(() => provider.CreateConnection(databaseName)));
activeConnectionProperties.Add(connectionProperty);
}
else if (connectionProperty.PropertyType == typeof(string))
{
// Property should be populated with a connection string that can be used to connect to the target database.
using (var connection = provider.CreateConnection(databaseName))
{
connectionProperty.SetValue(
sender,
connection.ConnectionString);
}
}
else
{
throw new InvalidOperationException(
"A connection was decorated with the DbTestMonkey.Contracts.ConnectionAttribute " +
"attribute but was not a type of System.Data.IDbConnection or System.Func<System.Data.IDbConnection>.");
}
}