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; } }
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(); } } }
/// <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."); } }
/// <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."); } }
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; }
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; }
object ICloneable.Clone() { SampleConnection clone = new SampleConnection(); clone._WrappedConnection = (DbConnection) ((ICloneable) this._WrappedConnection).Clone(); return clone; }
private void InitializeMe(string commandText, SampleConnection connection, DbTransaction transaction) { this.CommandText = commandText; this.Connection = connection; this.Transaction = transaction; }
public SampleCommand(string commandText, SampleConnection connection) { this.InitializeMe(commandText, connection, null); }
public SampleCommand(string commandText, SampleConnection connection, DbTransaction transaction) { this.InitializeMe(commandText, connection, transaction); }
private void InitializeMe(string commandText, SampleConnection connection, DbTransaction transaction) { this.CommandText = commandText; this.Connection = connection; this.Transaction = transaction; }
public SampleCommand(string commandText, SampleConnection connection, DbTransaction transaction) { this.InitializeMe(commandText, connection, transaction); }
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; } }
public SampleCommand(string commandText, SampleConnection connection) { this.InitializeMe(commandText, connection, null); }