Operations: How to Add a Connection String

  • You can add a connection string by using the PutConnectionStringOperation method.

  • This article demonstrates how to connect to an external database.

In this page:

PutConnectionStringOperation

public PutConnectionStringOperation(T connectionString)
Parameters Connection String Type Description
connectionString Raven Connection string to create: RavenConnectionString
connectionString Sql Connection string to create: SqlConnectionString
connectionString Olap Connection string to create: OlapConnectionString
connectionString ElasticSearch Connection string to create: ElasticSearchConnectionString

ConnectionString

public class ConnectionString
{
    public string Name { get; set; } // name of connection string        
}

Add a Raven Connection String

Secure servers

To connect secure RavenDB servers you need to

  1. Export the server certificate from the source server.
  2. Install it as a client certificate on the destination server.

This can be done in the RavenDB Studio -> Server Management -> Certificates view.

PutConnectionStringOperation<RavenConnectionString> operation
    = new PutConnectionStringOperation<RavenConnectionString>(
        new RavenConnectionString
        {
            Name = "raven2",
            Database = "Northwind2",
            TopologyDiscoveryUrls = new[]
            {
                "https://rvn2:8080"
            }
        });

PutConnectionStringResult connectionStringResult
    = store.Maintenance.Send(operation);
  • RavenConnectionString
    public class RavenConnectionString : ConnectionString
    {
        public string Database { get; set; } // target database name
        public string[] TopologyDiscoveryUrls; // list of server urls
    
        public ConnectionStringType Type => ConnectionStringType.Raven;
    
    }

Add an Sql Connection String

PutConnectionStringOperation<SqlConnectionString> operation
    = new PutConnectionStringOperation<SqlConnectionString>(
        new SqlConnectionString
        {
            Name = "local_mysql",
            FactoryName = "MySql.Data.MySqlClient",
            ConnectionString = "host=127.0.0.1;user=root;database=Northwind"
        });

PutConnectionStringResult connectionStringResult
    = store.Maintenance.Send(operation);
  • SqlConnectionString
    public class SqlConnectionString : ConnectionString
    {
        public string ConnectionString { get; set; }
    
        public string FactoryName { get; set; }
    
        public ConnectionStringType Type => ConnectionStringType.Sql;
    
    }

Add an Olap Connection String

To a Local Machine

OlapConnectionString olapConnectionString = new OlapConnectionString
{
    Name = (string)connectionStringName,
    LocalSettings = new LocalSettings
    {
        FolderPath = path
    }
};

var resultOlapString = store.Maintenance.Send
    (new PutConnectionStringOperation<OlapConnectionString>(olapConnectionString));

To a Cloud-Based Server

To learn how to connect to a cloud instance, see the Olap ETL article.

The following code sample is for a connection string to Amazon AWS.
If you use the following cloud-based servers, change the parameters accordingly:

var myOlapConnectionString = new OlapConnectionString
{
    Name = "myConnectionStringName",
    S3Settings = new S3Settings
    {
        BucketName = "myBucket",
        RemoteFolderName = "my/folder/name",
        AwsAccessKey = "myAccessKey",
        AwsSecretKey = "myPassword",
        AwsRegionName = "us-east-1"
    }
};

var resultOlapString = store.Maintenance.Send(new PutConnectionStringOperation<OlapConnectionString>(myOlapConnectionString));
  • OlapConnectionString
    public class OlapConnectionString : ConnectionString
    {
        public string Name { get; set; }
        public LocalSettings LocalSettings { get; set; }
        public S3Settings S3Settings { get; set; }
        public AzureSettings AzureSettings { get; set; }
        public GlacierSettings GlacierSettings { get; set; }
        public GoogleCloudSettings GoogleCloudSettings { get; set; }
        public FtpSettings FtpSettings { get; set; }
    
        public ConnectionStringType Type => ConnectionStringType.Olap;
    }

Add an Elasticsearch Connection String

// Create a Connection String to Elasticsearch
var elasticSearchConnectionString = new ElasticSearchConnectionString
{
    // Connection String Name
    Name = "ElasticConStr",
    // Elasticsearch Nodes URLs
    Nodes = new[] { "http://localhost:9200" },
    // Authentication Method
    Authentication = new Raven.Client.Documents.Operations.ETL.ElasticSearch.Authentication
    {
        Basic = new BasicAuthentication
        {
            Username = "John",
            Password = "32n4j5kp8"
        }
    }
};

store.Maintenance.Send(new PutConnectionStringOperation<ElasticSearchConnectionString>(elasticSearchConnectionString));
  • ElasticSearchConnectionString
    public class ElasticsearchConnectionString : ConnectionString
    {
        public string Name { get; set; }
        public string Nodes { get; set; }
        public string Authentication { get; set; }
        public string Basic { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
    
        public ConnectionStringType Type => ConnectionStringType.ElasticSearch;
    }