Beispiel #1
0
        /// <summary>
        ///     Creates a connection for SQL Server Compact Edition based on the given database name or connection string.
        ///     If the given string contains an '=' character then it is treated as a full connection string,
        ///     otherwise it is treated as a database name only.
        /// </summary>
        /// <param name="nameOrConnectionString"> The database name or connection string. </param>
        /// <returns> An initialized DbConnection. </returns>
        public DbConnection CreateConnection(string nameOrConnectionString)
        {
            Check.NotEmpty(nameOrConnectionString, "nameOrConnectionString");

            var factory = DbProviderFactories.GetFactory(ProviderInvariantName);

            Debug.Assert(factory != null, "Expected DbProviderFactories.GetFactory to throw if provider not found.");

            var connection = factory.CreateConnection();

            if (connection == null)
            {
                throw Error.DbContext_ProviderReturnedNullConnection();
            }

            if (DbHelpers.TreatAsConnectionString(nameOrConnectionString))
            {
                connection.ConnectionString = nameOrConnectionString;
            }
            else
            {
                if (!nameOrConnectionString.EndsWith(".sdf", ignoreCase: true, culture: null))
                {
                    nameOrConnectionString += ".sdf";
                }
                var dataPath = (DatabaseDirectory.StartsWith("|", StringComparison.Ordinal) &&
                                DatabaseDirectory.EndsWith("|", StringComparison.Ordinal))
                                   ? DatabaseDirectory + nameOrConnectionString
                                   : Path.Combine(DatabaseDirectory, nameOrConnectionString);
                connection.ConnectionString = String.Format(
                    CultureInfo.InvariantCulture, "Data Source={0}; {1}", dataPath, BaseConnectionString);
            }

            return(connection);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a connection for SQL Server Compact Edition based on the given database name or connection string.
        /// If the given string contains an '=' character then it is treated as a full connection string,
        /// otherwise it is treated as a database name only.
        /// </summary>
        /// <param name="nameOrConnectionString"> The database name or connection string. </param>
        /// <returns> An initialized DbConnection. </returns>
        public DbConnection CreateConnection(string nameOrConnectionString)
        {
            Check.NotEmpty(nameOrConnectionString, nameof(nameOrConnectionString));
            DbConnection connection = DbConfiguration.DependencyResolver.GetService <DbProviderFactory>((object)this.ProviderInvariantName).CreateConnection();

            if (connection == null)
            {
                throw Error.DbContext_ProviderReturnedNullConnection();
            }
            string str;

            if (DbHelpers.TreatAsConnectionString(nameOrConnectionString))
            {
                str = nameOrConnectionString;
            }
            else
            {
                if (!nameOrConnectionString.EndsWith(".sdf", true, (CultureInfo)null))
                {
                    nameOrConnectionString += ".sdf";
                }
                str = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Data Source={0}; {1}", (object)(!this.DatabaseDirectory.StartsWith("|", StringComparison.Ordinal) || !this.DatabaseDirectory.EndsWith("|", StringComparison.Ordinal) ? Path.Combine(this.DatabaseDirectory, nameOrConnectionString) : this.DatabaseDirectory + nameOrConnectionString), (object)this.BaseConnectionString);
            }
            DbInterception.Dispatch.Connection.SetConnectionString(connection, new DbConnectionPropertyInterceptionContext <string>().WithValue(str));
            return(connection);
        }
Beispiel #3
0
        public DbConnection CreateConnection(string nameOrConnectionString)
        {
            Check.NotEmpty(nameOrConnectionString, nameof(nameOrConnectionString));
            string str = nameOrConnectionString;

            if (!DbHelpers.TreatAsConnectionString(nameOrConnectionString))
            {
                if (nameOrConnectionString.EndsWith(".mdf", true, (CultureInfo)null))
                {
                    throw Error.SqlConnectionFactory_MdfNotSupported((object)nameOrConnectionString);
                }
                str = new SqlConnectionStringBuilder(this.BaseConnectionString)
                {
                    InitialCatalog = nameOrConnectionString
                }.ConnectionString;
            }
            DbConnection connection;

            try
            {
                connection = this.ProviderFactory("System.Data.SqlClient").CreateConnection();
                DbInterception.Dispatch.Connection.SetConnectionString(connection, new DbConnectionPropertyInterceptionContext <string>().WithValue(str));
            }
            catch
            {
                connection = (DbConnection) new SqlConnection();
                DbInterception.Dispatch.Connection.SetConnectionString(connection, new DbConnectionPropertyInterceptionContext <string>().WithValue(str));
            }
            return(connection);
        }
Beispiel #4
0
        public DbConnection CreateConnection(string nameOrConnectionString)
        {
            Check.NotEmpty(nameOrConnectionString, "nameOrConnectionString");

            // If the "name or connection string" contains an '=' character then it is treated as a connection string.
            var connectionString = nameOrConnectionString;

            if (!DbHelpers.TreatAsConnectionString(nameOrConnectionString))
            {
                if (nameOrConnectionString.EndsWith(".mdf", ignoreCase: true, culture: null))
                {
                    throw Error.SqlConnectionFactory_MdfNotSupported(nameOrConnectionString);
                }

                connectionString =
                    new SqlConnectionStringBuilder(BaseConnectionString)
                {
                    InitialCatalog = nameOrConnectionString
                }.ConnectionString;
            }

            DbConnection connection = null;

            try
            {
                connection = ProviderFactory("System.Data.SqlClient").CreateConnection();

                DbInterception.Dispatch.Connection.SetConnectionString(
                    connection,
                    new DbConnectionPropertyInterceptionContext <string>().WithValue(connectionString));
            }
            catch
            {
                // Fallback to hard-coded type if provider didn't work
                connection = new SqlConnection();

                DbInterception.Dispatch.Connection.SetConnectionString(
                    connection,
                    new DbConnectionPropertyInterceptionContext <string>().WithValue(connectionString));
            }

            return(connection);
        }