Exemplo n.º 1
0
		[ResourceExposure(ResourceScope.Machine)] // connection parameter may refer to filenames.
		void IProvider.Initialize(IDataServices dataServices, object connection)
		{
			if(dataServices == null)
			{
				throw Error.ArgumentNull("dataServices");
			}
			_services = dataServices;

			DbConnection con;
			DbTransaction tx = null;

			string fileOrServerOrConnectionString = connection as string;
			if(fileOrServerOrConnectionString != null)
			{
				string connectionString = this.GetConnectionString(fileOrServerOrConnectionString);
				_dbName = this.GetDatabaseName(connectionString);
				if(_dbName.EndsWith(".sdf", StringComparison.OrdinalIgnoreCase))
				{
					_mode = SqlServerProviderMode.SqlCE;
				}
				if(_mode == SqlServerProviderMode.SqlCE)
				{
					DbProviderFactory factory = SqlProvider.GetProvider(SqlCeProviderInvariantName);
					if(factory == null)
					{
						throw Error.ProviderNotInstalled(_dbName, SqlCeProviderInvariantName);
					}
					con = factory.CreateConnection();
				}
				else
				{
					con = new SqlConnection();
				}
				con.ConnectionString = connectionString;
			}
			else
			{
				// We only support SqlTransaction and SqlCeTransaction
				tx = connection as SqlTransaction;
				if(tx == null)
				{
					// See if it's a SqlCeTransaction
					if(connection.GetType().FullName == SqlCeTransactionTypeName)
					{
						tx = connection as DbTransaction;
					}
				}
				if(tx != null)
				{
					connection = tx.Connection;
				}
				con = connection as DbConnection;
				if(con == null)
				{
					throw Error.InvalidConnectionArgument("connection");
				}
				if(con.GetType().FullName == SqlCeConnectionTypeName)
				{
					_mode = SqlServerProviderMode.SqlCE;
				}
				_dbName = this.GetDatabaseName(con.ConnectionString);
			}

			// initialize to the default command timeout
			using(DbCommand c = con.CreateCommand())
			{
				_commandTimeout = c.CommandTimeout;
			}

			int maxUsersPerConnection = 1;
			if(con.ConnectionString.IndexOf("MultipleActiveResultSets", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
				builder.ConnectionString = con.ConnectionString;
				if(string.Compare((string)builder["MultipleActiveResultSets"], "true", StringComparison.OrdinalIgnoreCase) == 0)
				{
					maxUsersPerConnection = 10;
				}
			}

			// If fileOrServerOrConnectionString != null, that means we just created the connection instance and we have to tell
			// the SqlConnectionManager that it should dispose the connection when the context is disposed. Otherwise the user owns
			// the connection and should dispose of it themselves.
			_conManager = new ConnectionManager(this, con, maxUsersPerConnection, fileOrServerOrConnectionString != null /*disposeConnection*/);
			if(tx != null)
			{
				_conManager.Transaction = tx;
			}

#if DEBUG
			SqlNode.Formatter = new SqlFormatter();
#endif


			Type readerType;
			if(_mode == SqlServerProviderMode.SqlCE)
			{
				readerType = con.GetType().Module.GetType(SqlCeDataReaderTypeName);
			}
			else if(con is SqlConnection)
			{
				readerType = typeof(SqlDataReader);
			}
			else
			{
				readerType = typeof(DbDataReader);
			}
			_readerCompiler = new ObjectReaderCompiler(readerType, _services);
		}
Exemplo n.º 2
0
		// Not implementing finalizer here because there are no unmanaged resources
		// to release. See http://msdnwiki.microsoft.com/en-us/mtpswiki/12afb1ea-3a17-4a3f-a1f0-fcdb853e2359.aspx

		// The bulk of the clean-up code is implemented in Dispose(bool)
		protected virtual void Dispose(bool disposing)
		{
			// Implemented but empty so that derived contexts can implement
			// a finalizer that potentially cleans up unmanaged resources.
			if(disposing)
			{
				_services = null;
				if(_conManager != null)
				{
					_conManager.DisposeConnection();
				}
				_conManager = null;
				_typeProvider = null;
				_sqlFactory = null;
				_translator = null;
				_readerCompiler = null;
				_log = null;
			}
		}