protected override bool DbDatabaseExists(DbConnection connection, int?commandTimeout, StoreItemCollection storeItemCollection)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection must not be null");
            }

            if (storeItemCollection == null)
            {
                throw new ArgumentNullException("storeItemCollection must not be null");
            }

            SampleConnection sampleConnection = connection as SampleConnection;

            if (sampleConnection == null)
            {
                throw new ArgumentException("connection must be a valid SampleConnection");
            }

            string databaseName = GetDatabaseName(sampleConnection);

            bool exists = false;

            UsingMasterConnection(sampleConnection, conn =>
            {
                StoreVersion storeVersion   = StoreVersionUtils.GetStoreVersion(conn);
                string databaseExistsScript = DdlBuilder.CreateDatabaseExistsScript(databaseName);

                int result = (int)CreateCommand(conn, databaseExistsScript, commandTimeout).ExecuteScalar();
                exists     = (result == 1);
            });

            return(exists);
        }
        protected override void DbDeleteDatabase(DbConnection connection, int?commandTimeout, StoreItemCollection storeItemCollection)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection must not be null");
            }

            if (storeItemCollection == null)
            {
                throw new ArgumentNullException("storeItemCollection must not be null");
            }

            SampleConnection sampleConnection = connection as SampleConnection;

            if (sampleConnection == null)
            {
                throw new ArgumentException("connection must be a valid SampleConnection");
            }

            string databaseName       = GetDatabaseName(sampleConnection);
            string dropDatabaseScript = DdlBuilder.DropDatabaseScript(databaseName);

            // clear the connection pool in case someone is holding on to the database
            sampleConnection.ClearPool();

            UsingMasterConnection(sampleConnection, (conn) =>
            {
                CreateCommand(conn, dropDatabaseScript, commandTimeout).ExecuteNonQuery();
            });
        }
        private static void UsingConnection(SampleConnection connection, Action <SampleConnection> act)
        {
            // remember the connection string so that we can reset it if credentials are wiped
            string holdConnectionString = connection.ConnectionString;
            bool   openingConnection    = connection.State == ConnectionState.Closed;

            if (openingConnection)
            {
                connection.Open();
            }
            try
            {
                act(connection);
            }
            finally
            {
                if (openingConnection && connection.State == ConnectionState.Open)
                {
                    // if we opened the connection, we should close it
                    connection.Close();
                }
                if (connection.ConnectionString != holdConnectionString)
                {
                    connection.ConnectionString = holdConnectionString;
                }
            }
        }
        private static void UsingMasterConnection(SampleConnection connection, Action <SampleConnection> act)
        {
            var connectionBuilder = new SqlConnectionStringBuilder(connection.ConnectionString)
            {
                InitialCatalog   = "master",
                AttachDBFilename = string.Empty, // any AttachDB path specified is not relevant to master
            };

            try
            {
                using (var masterConnection = new SampleConnection(connectionBuilder.ConnectionString))
                {
                    UsingConnection(masterConnection, act);
                }
            }
            catch (SqlException)
            {
                // if it appears that the credentials have been removed from the connection string, use an alternate explanation
                if (!connectionBuilder.IntegratedSecurity &&
                    (string.IsNullOrEmpty(connectionBuilder.UserID) || string.IsNullOrEmpty(connectionBuilder.Password)))
                {
                    throw new InvalidOperationException("Credentials are missing from the connection string");
                }
                throw;
            }
        }
Пример #5
0
        object ICloneable.Clone()
        {
            SampleConnection clone = new SampleConnection();

            clone._WrappedConnection = (DbConnection)((ICloneable)this._WrappedConnection).Clone();
            return(clone);
        }
        private static void GetDatabaseFileNames(SampleConnection connection, out string dataFileName, out string logFileName)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection must not be null");
            }


            var    connectionStringBuilder = new SqlConnectionStringBuilder(connection.ConnectionString);
            string attachDBFile            = connectionStringBuilder.AttachDBFilename;

            if (string.IsNullOrEmpty(attachDBFile))
            {
                dataFileName = null;
                logFileName  = null;
            }
            else
            {
                //Handle the case when attachDBFilename starts with |DataDirectory|
                dataFileName = ExpandDataDirectory(attachDBFile);

                //Handle the other cases
                dataFileName = dataFileName ?? attachDBFile;
                logFileName  = Path.ChangeExtension(dataFileName, "ldf");
            }
        }
        private static string GetDatabaseName(SampleConnection sampleConnection)
        {
            string databaseName = sampleConnection.Database;

            if (string.IsNullOrEmpty(databaseName))
            {
                throw new InvalidOperationException("Connection String did not specify an Initial Catalog");
            }

            return(databaseName);
        }
        protected override void DbCreateDatabase(DbConnection connection, int?commandTimeout, StoreItemCollection storeItemCollection)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection must not be null");
            }

            if (storeItemCollection == null)
            {
                throw new ArgumentNullException("storeItemCollection must not be null");
            }

            SampleConnection sampleConnection = connection as SampleConnection;

            if (sampleConnection == null)
            {
                throw new ArgumentException("The connection is not of type 'SampleConnection'.");
            }

            string databaseName = GetDatabaseName(sampleConnection);

            if (string.IsNullOrEmpty(databaseName))
            {
                throw new InvalidOperationException("Initial Catalog is missing from the connection string");
            }

            string dataFileName, logFileName;

            GetDatabaseFileNames(sampleConnection, out dataFileName, out logFileName);

            string createDatabaseScript = DdlBuilder.CreateDatabaseScript(databaseName, dataFileName, logFileName);
            string createObjectsScript  = DdlBuilder.CreateObjectsScript(storeItemCollection);

            UsingMasterConnection(sampleConnection, conn =>
            {
                // create database
                CreateCommand(conn, createDatabaseScript, commandTimeout).ExecuteNonQuery();
            });

            // Clear connection pool for the database connection since after the 'create database' call, a previously
            // invalid connection may now be valid.
            sampleConnection.ClearPool();

            UsingConnection(sampleConnection, conn =>
            {
                // create database objects
                CreateCommand(conn, createObjectsScript, commandTimeout).ExecuteNonQuery();
            });
        }
        private static SampleCommand CreateCommand(SampleConnection connection, string commandText, int?commandTimeout)
        {
            Debug.Assert(connection != null);
            if (string.IsNullOrEmpty(commandText))
            {
                // SqlCommand will complain if the command text is empty
                commandText = Environment.NewLine;
            }
            var command = new SampleCommand(commandText, connection);

            if (commandTimeout.HasValue)
            {
                command.CommandTimeout = commandTimeout.Value;
            }
            return(command);
        }
        protected override string GetDbProviderManifestToken(DbConnection connection)
        {
            if (connection == null)
            {
                throw new ArgumentException("connection");
            }

            SampleConnection sampleConnection = connection as SampleConnection;

            if (sampleConnection == null)
            {
                throw new ArgumentException("The connection is not of type 'SampleConnection'.");
            }

            if (string.IsNullOrEmpty(sampleConnection.ConnectionString))
            {
                throw new ArgumentException("Could not determine storage version; a valid storage connection or a version hint is required.");
            }

            bool closeConnection = false;

            try
            {
                if (sampleConnection.State != ConnectionState.Open)
                {
                    sampleConnection.Open();
                    closeConnection = true;
                }

                StoreVersion version = StoreVersionUtils.GetStoreVersion(sampleConnection);
                if (version == StoreVersion.Sql9)
                {
                    return(SampleProviderManifest.TokenSql9);
                }
                else
                {
                    return(StoreVersionUtils.GetVersionHint(version));
                }
            }
            finally
            {
                if (closeConnection)
                {
                    sampleConnection.Close();
                }
            }
        }
Пример #11
0
 /// <summary>
 /// Get the StoreVersion from the connection. Returns one of Sql9, Sql10
 /// </summary>
 /// <param name="connection">current sql connection</param>
 /// <returns>Sql Version for the current connection</returns>
 internal static StoreVersion GetStoreVersion(SampleConnection connection)
 {
     // We don't have anything unique for Sql
     if ((connection.ServerVersion.StartsWith("10.", StringComparison.Ordinal)) ||
         (connection.ServerVersion.StartsWith("11.", StringComparison.Ordinal)))
     {
         return(StoreVersion.Sql10);
     }
     else if (connection.ServerVersion.StartsWith("09.", StringComparison.Ordinal))
     {
         return(StoreVersion.Sql9);
     }
     else
     {
         throw new ArgumentException("The version of SQL Server is not supported via sample provider.");
     }
 }
Пример #12
0
 /// <summary>
 /// Get the StoreVersion from the connection. Returns one of Sql9, Sql10
 /// </summary>
 /// <param name="connection">current sql connection</param>
 /// <returns>Sql Version for the current connection</returns>
 internal static StoreVersion GetStoreVersion(SampleConnection connection)
 {
     // We don't have anything unique for Sql
     if ((connection.ServerVersion.StartsWith("10.", StringComparison.Ordinal)) || 
        (connection.ServerVersion.StartsWith("11.", StringComparison.Ordinal)))
     {
         return StoreVersion.Sql10;
     }
     else if (connection.ServerVersion.StartsWith("09.", StringComparison.Ordinal))
     {
         return StoreVersion.Sql9;
     }
     else
     {
         throw new ArgumentException("The version of SQL Server is not supported via sample provider.");
     }
 }
Пример #13
0
 private static SampleCommand CreateCommand(SampleConnection connection, string commandText, int? commandTimeout)
 {
     Debug.Assert(connection != null);
     if (string.IsNullOrEmpty(commandText))
     {
         // SqlCommand will complain if the command text is empty
         commandText = Environment.NewLine;
     }
     var command = new SampleCommand(commandText, connection);
     if (commandTimeout.HasValue)
     {
         command.CommandTimeout = commandTimeout.Value;
     }
     return command;
 }
Пример #14
0
        private static void GetDatabaseFileNames(SampleConnection connection, out string dataFileName, out string logFileName)
        {
            if (connection == null)
                throw new ArgumentNullException("connection must not be null");


            var connectionStringBuilder = new SqlConnectionStringBuilder(connection.ConnectionString);
            string attachDBFile = connectionStringBuilder.AttachDBFilename;
            if (string.IsNullOrEmpty(attachDBFile))
            {
                dataFileName = null;
                logFileName = null;
            }
            else
            {                
                //Handle the case when attachDBFilename starts with |DataDirectory|                
                dataFileName = ExpandDataDirectory(attachDBFile);

                //Handle the other cases
                dataFileName = dataFileName ?? attachDBFile;
                logFileName = Path.ChangeExtension(dataFileName, "ldf");
            }
        }        
Пример #15
0
        private static string GetDatabaseName(SampleConnection sampleConnection)
        {
            string databaseName = sampleConnection.Database;
            if (string.IsNullOrEmpty(databaseName))
                throw new InvalidOperationException("Connection String did not specify an Initial Catalog");

            return databaseName;
        }
Пример #16
0
 object ICloneable.Clone()
 {
     SampleConnection clone = new SampleConnection();
     clone._WrappedConnection = (DbConnection) ((ICloneable) this._WrappedConnection).Clone();
     return clone;
 }
Пример #17
0
 private void InitializeMe(string commandText, SampleConnection connection, DbTransaction transaction)
 {
     this.CommandText = commandText;
     this.Connection  = connection;
     this.Transaction = transaction;
 }
Пример #18
0
 public SampleCommand(string commandText, SampleConnection connection)
 {
     this.InitializeMe(commandText, connection, null);
 }
Пример #19
0
 public SampleCommand(string commandText, SampleConnection connection, DbTransaction transaction)
 {
     this.InitializeMe(commandText, connection, transaction);
 }
Пример #20
0
 private void InitializeMe(string commandText, SampleConnection connection, DbTransaction transaction)
 {
     this.CommandText = commandText;
     this.Connection = connection;
     this.Transaction = transaction;
 }
Пример #21
0
 public SampleCommand(string commandText, SampleConnection connection, DbTransaction transaction)
 {
     this.InitializeMe(commandText, connection, transaction);
 }
Пример #22
0
 private static void UsingConnection(SampleConnection connection, Action<SampleConnection> act)
 {
     // remember the connection string so that we can reset it if credentials are wiped
     string holdConnectionString = connection.ConnectionString;
     bool openingConnection = connection.State == ConnectionState.Closed;
     if (openingConnection)
     {
         connection.Open();
     }
     try
     {
         act(connection);
     }
     finally
     {
         if (openingConnection && connection.State == ConnectionState.Open)
         {
             // if we opened the connection, we should close it
             connection.Close();
         }
         if (connection.ConnectionString != holdConnectionString)
         {
             connection.ConnectionString = holdConnectionString;
         }
     }
 }
Пример #23
0
        private static void UsingMasterConnection(SampleConnection connection, Action<SampleConnection> act)
        {
            var connectionBuilder = new SqlConnectionStringBuilder(connection.ConnectionString)
            {
                InitialCatalog = "master",
                AttachDBFilename = string.Empty, // any AttachDB path specified is not relevant to master
            };

            try
            {
                using (var masterConnection = new SampleConnection(connectionBuilder.ConnectionString))
                {
                    UsingConnection(masterConnection, act);
                }
            }
            catch (SqlException)
            {
                // if it appears that the credentials have been removed from the connection string, use an alternate explanation
                if (!connectionBuilder.IntegratedSecurity &&
                    (string.IsNullOrEmpty(connectionBuilder.UserID) || string.IsNullOrEmpty(connectionBuilder.Password)))
                {
                    throw new InvalidOperationException("Credentials are missing from the connection string");
                }
                throw;
            }
        }
Пример #24
0
 public SampleCommand(string commandText, SampleConnection connection)
 {
     this.InitializeMe(commandText, connection, null);
 }