当前位置: 首页>>代码示例>>C#>>正文


C# IDatabaseProvider.CreateConnection方法代码示例

本文整理汇总了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>.");
             }
        }
开发者ID:DbTestMonkey,项目名称:DbTestMonkey,代码行数:52,代码来源:DbController.cs


注:本文中的IDatabaseProvider.CreateConnection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。