# Creating Connection

## Content



To establish a connection to a Snowflake data source, the ADO.NET Provider can be used through the **C1SnowflakeConnection** class. This class requires a connection string to be provided as an argument, which can be created in either of the following ways:

*   Generated using the **C1SnowflakeConnectionStringBuilder** class by specifying individual statements.

*   Constructed manually by writing the connection string directly.

The following code shows how to use the **C1SnowflakeConnectionStringBuilder** class to configure the connection string for Snowflake, which can then be consumed by the **C1SnowflakeConnection** class to establish a connection to Snowflake server. Through that connection, the data source can be queried.

The ADO.NET provider for Snowflake allows you to use either the Key-Pair or the OAuth 2.0 authentication.

### Key Pair Authentication

### Connection string

All the connection string properties below are mandatory for key pair authentication:

**_private key_**: is the generated file together with a public key that contains the private key, in this property should be specified the path of the file

**_fingerprint_**: the fingerprint can be retrieved in the Snowflake console using the query ‘DESC USER username’;

**username**: the username used to log in to the Snowflake console

### Connection string generation

The following code shows how the **C1SnowflakeConnectionStringBuilder** class can be used to configure the connection string for Snowflake and consumed by the **C1SnowflakeConnection** class to create a connection to the server. Here, you can query the data.

```csharp
//Create connection string using C1SnowflakeConnectionStringBuilder
C1SnowflakeConnectionStringBuilder builder = new C1SnowflakeConnectionStringBuilder();
builder.Account = "*****";
builder.Url = "https://****.eu-west-2.aws.snowflakecomputing.com";
builder.Warehouse = "****";
builder.Database = "****";
builder.Schema = "****";
builder.Role = "****";
builder.PrivateKey = "****";
builder.PrivateKeyPassword = "****";
builder.Fingerprint = "****";
builder.Username = "****";
//Create and establish a connection
C1SnowflakeConnection conn = new C1SnowflakeConnection(builder);
```

### Connection string literal

Alternatively, the connection string can be written directly as a string literal like in the code example below.

```csharp
static string connectionString = $"account={Account};warehouse={Warehouse};schema={Schema};role={Role};database={Database};url={Url};" +
            $"private key={KeyPath};private key password ={PrivateKeyPassword};fingerprint={Fingerprint};username={User};";
C1SnowflakeConnection conn = new C1SnowflakeConnection(connectionString);
```

### OAuth-based authentication

Snowflake OAuth authentication is based on OAuth 2.0.

### Connection string generation

The following code shows how the **C1SnowflakeConnectionStringBuilder** class can be used to configure the connection string for Snowflake and consumed by the **C1SnowflakeConnection** class to create a connection to the server. Here, you can query the data.

```csharp
//Create a connection string using C1SnowflakeConnectionStringBuilder
C1SnowflakeConnectionStringBuilder builder = new C1SnowflakeConnectionStringBuilder();
builder.Account = "****.eu-west-2.aws";
builder.Url = "https://****.eu-west-2.aws.snowflakecomputing.com";
builder.Warehouse = "****";
builder.Database = "****";
builder.Schema = "****";
builder.Role = "****";
builder.OAuthTokenEndpoint = "****.snowflakecomputing.com/oauth/token-request";
builder.OAuthClientId = "****";
builder.OAuthClientSecret = "****";
builder.OAuthRefreshToken = "****";
builder.OAuthAccessToken = "****";
//Create and establish the connection
C1SnowflakeConnection conn = new C1SnowflakeConnection(builder);
```

### Connection string literal

Alternatively, the connection string can be written directly as a string literal like in the code example below.

```csharp
static string connectionString = $"account={Account};warehouse={Warehouse};schema={Schema};role={Role};database={Database};url={Url};" +
            $"OAuth Client Secret={OAuthClientSecret};OAuth Client Id={OAuthClientId};OAuth Access Token={OAuthAccessToken};OAuth Refresh Token={OAuthRefreshToken};Oauth Token Endpoint={OAuthTokenEndpoint};";
C1SnowflakeConnection conn = new C1SnowflakeConnection(builder);
```