예제 #1
0
        public override TestCluster CreateTestCluster()
        {
            var options = new TestClusterOptions();

            options.ClientConfiguration.AddSimpleMessageStreamProvider(StreamProviderName, false);
            options.ClusterConfiguration.AddSimpleMessageStreamProvider(StreamProviderName, false);


            foreach (var item in dbNames)
            {
                string actualDBName = item.Value;
                string dbName       = item.Key;
                using (SqlConnection connection = instance.CreateConnection())
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand($"create database [{actualDBName}]", connection))
                    {
                        command.ExecuteNonQuery();
                    }
                }
                var sbuilder = instance.CreateConnectionStringBuilder();
                sbuilder.SetInitialCatalogName(actualDBName);

                options.ClusterConfiguration.AddSimpleSQLStorageProvider(dbName, sbuilder.ConnectionString, "true");
            }


            return(new TestCluster(options));
        }
예제 #2
0
        private static string GetConnectionString(ISqlLocalDbInstance instance, string pathToModel)
        {
            var sqlConnectionStringBuilder = instance.CreateConnectionStringBuilder();

            sqlConnectionStringBuilder.InitialCatalog     = Path.GetFileNameWithoutExtension(pathToModel);
            sqlConnectionStringBuilder.IntegratedSecurity = true;
            sqlConnectionStringBuilder.AttachDBFilename   = pathToModel;
            return(sqlConnectionStringBuilder.ToString());
        }
예제 #3
0
        private void Initialize(string instanceName, string databaseName)
        {
            this.databaseName = databaseName;
            this.instanceName = instanceName;

            var existing = SqlLocalDbApi.GetInstanceInfo(instanceName);

            if (existing.Exists)
            {
                if (existing.IsRunning)
                {
                    SqlLocalDbApi.StopInstance(instanceName);
                }
                SqlLocalDbApi.DeleteInstance(instanceName);
            }


            ISqlLocalDbProvider provider = new SqlLocalDbProvider();

            this.instance = provider.GetOrCreateInstance(instanceName);

            instance.Start();


            var connectionStringBuilder = instance.CreateConnectionStringBuilder();

            using (var conn = new SqlConnection(connectionStringBuilder.ConnectionString))
            {
                var serverConnection = new ServerConnection(conn);

                // By default, LocalDB stores database files in the user's home folder. Messy for temporary test databases.
                // Store them in the user's temp folder instead.
                var testDatabasesDirectory = Path.Combine(Path.GetTempPath(), "TestDatabases");

                if (!Directory.Exists(testDatabasesDirectory))
                {
                    Directory.CreateDirectory(testDatabasesDirectory);
                }

                // Generate a unique name for our mdf file to avoid clashing with other ongoing test runs.
                var databaseFileNameRoot = string.Format("{0}_{1}_{2}", databaseName, DateTime.Now.ToString("yyyyMMdd_HHmmss"), Guid.NewGuid().ToString("N"));

                var mdfFileName = databaseFileNameRoot + ".mdf";
                var ldfFileName = databaseFileNameRoot + "_log.ldf";

                this.mdfFilePath = Path.Combine(testDatabasesDirectory, mdfFileName);
                this.ldfFilePath = Path.Combine(testDatabasesDirectory, ldfFileName);

                var sql = string.Format("CREATE DATABASE {0} ON (NAME = N'{0}', FILENAME = '{1}')", databaseName, this.mdfFilePath);

                Console.WriteLine(string.Format("Creating database {0} at {1}", databaseName, this.mdfFilePath));
                serverConnection.ExecuteNonQuery(sql);
            }

            connectionStringBuilder.InitialCatalog = this.databaseName;
            this.connectionString = connectionStringBuilder.ConnectionString;
        }
예제 #4
0
        private string CreateConnectionString()
        {
            var connectionStringBuilder = _localDbInstance.CreateConnectionStringBuilder();

            connectionStringBuilder.MultipleActiveResultSets = true;
            connectionStringBuilder.IntegratedSecurity       = true;
            connectionStringBuilder.InitialCatalog           = _databaseName;

            return(connectionStringBuilder.ToString());
        }
        private SqlConnectionStringBuilder CreateADatabase(Random rnd)
        {
            string actualDBName = rnd.Next().ToString();

            using (SqlConnection connection = instance.CreateConnection())
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand($"create database [{actualDBName}]", connection))
                {
                    command.ExecuteNonQuery();
                }
            }
            var sbuilder = instance.CreateConnectionStringBuilder();

            sbuilder.SetInitialCatalogName(actualDBName);
            return(sbuilder);
        }
 /// <summary>
 /// Creates an instance of <see cref="SqlConnectionStringBuilder" /> containing
 /// the default SQL connection string to connect to the LocalDB instance.
 /// </summary>
 /// <returns>
 /// An instance of <see cref="SqlConnectionStringBuilder" /> containing
 /// the default SQL connection string to connect to the LocalDB instance.
 /// </returns>
 /// <exception cref="ObjectDisposedException">
 /// The instance has been disposed.
 /// </exception>
 public SqlConnectionStringBuilder CreateConnectionStringBuilder()
 {
     EnsureNotDisposed();
     return(_instance.CreateConnectionStringBuilder());
 }