Save Big on Cyber Monday! Up to 40% Off
ends in   {{days}}
Days
{{timeFormat.hours}}
:
{{timeFormat.minutes}}
:
{{timeFormat.seconds}}

Snowflake is a cloud data platform that stores and processes large volumes of data and lets you query it with SQL. To access this data from a .NET application, developers need a reliable way to authenticate in Snowflake, send queries, and read the results with familiar data access tools.

In this article, we'll explore how to connect to Snowflake and retrieve its data in a C# application using dotConnect for Snowflake.

Why dotConnect for Snowflake?

Easy integration

dotConnect enables straightforward connectivity between .NET applications and Snowflake, allowing developers to query Snowflake data with SQL without working directly with the Snowflake REST API.

Familiar ADO.NET classes

Using familiar ADO.NET components, dotConnect for Snowflake allows developers to work with data through standard .NET data access patterns such as connections, commands, and data readers.

Advanced ORM support

dotConnect for Snowflake supports Entity Framework Core 3.1 and 8-11, Dapper, and NHibernate, enabling object-relational mapping and efficient interaction with Snowflake data in .NET applications.

Flexible authentication

Connect with a user name and a password or a programmatic access token, an RSA key pair, or OAuth 2.0 through your own Snowflake security integration. All requests to Snowflake are sent over HTTPS.

ADO.NET compatibility

Devart implements the standard ADO.NET provider model in dotConnect solutions, ensuring consistent behavior and compatibility with existing .NET frameworks and development tools.

Priority support and frequent updates

Devart provides professional technical assistance, comprehensive documentation, and ongoing product enhancements, helping developers maintain stable and reliable integrations with Snowflake.

Download and activate dotConnect for Snowflake

You can try dotConnect for Snowflake free for 30 days. Choose one of the following installation options:

30-day free trial version

dotnet add package Devart.Data.Snowflake
Install-Package Devart.Data.Snowflake

You can install the provider by using the Windows installer.

After you receive the license key, add it to your connection strings to connect to the data source.

Start using dotConnect for Snowflake in your project today with a free trial

Connect to Snowflake using C#

Before you connect, find the host name of your Snowflake account. It looks like orgname-accountname.snowflakecomputing.com. In Snowsight, you can copy it from the account details: select your user name in the lower-left corner, point to your account, and select View account details. Specify the host name in the Domain parameter without the https:// prefix. Do not use the Snowsight address app.snowflake.com: it is the address of the web interface, not of your account.

You also need a virtual warehouse to run queries and a database to work with. For the supported .NET platforms and Visual Studio versions, see Features & compatibility.

Create a console application, then use the code below to connect to Snowflake with a user name and password. The Basic authentication type is used by default.

using Devart.Data.Snowflake;

class Program
{
  static void Main()
  {
    using SnowflakeConnection conn = new(
      "Authentication Type=Basic;" +
      "Domain=myaccount.snowflakecomputing.com;" +
      "User=your_username;" +
      "Password=your_password;" +
      "Database=your_database;" +
      "Warehouse=your_warehouse;" +
      "License Key=your_license_key");
    conn.Open();
  }
}

If multi-factor authentication (MFA) is required for the Snowflake user, Snowflake rejects the password sign-in. In this case, specify a programmatic access token (PAT) of the user in the Password parameter, or use key pair or OAuth 2.0 authentication described below.

Main connection properties

Name Description
Authentication Type The authentication method: Basic (the default value), KeyPair, OAuth2, or OAuth2Interactive
Domain The host name of your Snowflake account, for example, myaccount.snowflakecomputing.com
User The name of the Snowflake user for the Basic and KeyPair authentication types
Password The password or a programmatic access token (PAT) of the Snowflake user for the Basic authentication type
Private Key The RSA private key for the KeyPair authentication type: the path to the key file or the key in the PEM format
Passphrase The passphrase of an encrypted private key
Client Id, Client Secret The client ID and client secret of your Snowflake security integration for the OAuth2 authentication type
Refresh Token The OAuth 2.0 refresh token for the OAuth2 authentication type
Database The database to use by default
Schema The schema to use by default
Warehouse The virtual warehouse that runs your queries
Role The Snowflake role to use for the session
License Key The license key for dotConnect for Snowflake

Connect with a programmatic access token

If Snowflake requires multi-factor authentication (MFA) for the user, a password alone may be rejected. In this case, connect with a Snowflake programmatic access token (PAT) instead of the password. You can also use key pair or OAuth 2.0 authentication described below.

To generate a token, use Snowsight or run the following statement. Copy the token right away: Snowflake shows it only once.

ALTER USER your_username ADD PROGRAMMATIC ACCESS TOKEN dotconnect_token;

In the connection dialog of Visual Studio Server Explorer, select Programmatic Access Token as the authentication type and enter the token in the Password (PAT) field. In the connection string, use the Basic authentication type and pass the token in the Password parameter. In the User parameter, specify the login name of the Snowflake user.

Authentication Type=Basic;Domain=myaccount.snowflakecomputing.com;User=your_username;Password=your_token;Database=your_database;Warehouse=your_warehouse;License Key=your_license_key

The Password (PAT) field accepts either a token or the user's password. If your account allows password sign-in, you can enter the password there.

Before you connect with a token, note the following:

  • By default, a token expires in 15 days. An administrator can allow up to 365 days. When the token expires, generate a new one and replace it in your saved connections and connection strings.
  • To sign in with a token, the user must be subject to a network policy, unless an administrator has turned off this requirement in the authentication policy.
  • For a service user (TYPE = SERVICE), a token is restricted to one role by default, and the session uses this role.
  • If an authentication policy limits the authentication methods, it must allow PROGRAMMATIC_ACCESS_TOKEN.

For more information, see Using programmatic access tokens for authentication in the Snowflake documentation.

Set up key pair authentication

Key pair authentication uses an RSA key instead of a password. It does not require a second authentication factor, so it suits applications and services that connect to Snowflake without user interaction.

First, generate an encrypted private key and its public key with OpenSSL. You will be prompted for a passphrase for the private key.

openssl genrsa 2048 | openssl pkcs8 -topk8 -v2 des3 -inform PEM -out rsa_key.p8
openssl rsa -in rsa_key.p8 -pubout -out rsa_key.pub

Then assign the public key to the Snowflake user. Copy the content of the rsa_key.pub file without the BEGIN PUBLIC KEY and END PUBLIC KEY lines and without line breaks, and run the following statement in Snowsight. To modify a user, you need the SECURITYADMIN role or the ownership of the user.

ALTER USER your_username SET RSA_PUBLIC_KEY = 'MIIBIjANBgkqh...';

In the connection string, set the authentication type to KeyPair, specify the path to the private key file in the Private Key parameter and, for an encrypted key, its passphrase in the Passphrase parameter. Store the private key securely and do not commit it to source control: anyone who has the key and its passphrase can access your data.

Authentication Type=KeyPair;Domain=myaccount.snowflakecomputing.com;User=your_username;Private Key=rsa_key.p8;Passphrase=your_passphrase;Database=your_database;Warehouse=your_warehouse;License Key=your_license_key

Set up OAuth 2.0 authentication

With OAuth 2.0, users sign in to Snowflake in the browser, and the provider uses a refresh token to connect. To use OAuth 2.0, create a security integration in your Snowflake account. The redirect URI must match the address used by the connection dialog of Visual Studio Server Explorer, http://localhost:58088. Creating an integration requires the ACCOUNTADMIN role or the CREATE INTEGRATION privilege.

CREATE SECURITY INTEGRATION dotconnect_oauth
  TYPE = OAUTH
  ENABLED = TRUE
  OAUTH_CLIENT = CUSTOM
  OAUTH_CLIENT_TYPE = 'CONFIDENTIAL'
  OAUTH_REDIRECT_URI = 'http://localhost:58088'
  OAUTH_ALLOW_NON_TLS_REDIRECT_URI = TRUE
  OAUTH_ISSUE_REFRESH_TOKENS = TRUE;

To get the client ID and client secret of the integration, run the following query and copy the OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET values from the result.

SELECT SYSTEM$SHOW_OAUTH_CLIENT_SECRETS('DOTCONNECT_OAUTH');

You can connect with OAuth 2.0 in one of two ways: get the refresh token interactively with Authentication Type=OAuth2Interactive, or specify the refresh token manually with Authentication Type=OAuth2.

To get the refresh token interactively, set the authentication type to OAuth2Interactive and specify the client ID and client secret in the connection string:

Authentication Type=OAuth2Interactive;Domain=myaccount.snowflakecomputing.com;Client Id=your_client_id;Client Secret=your_client_secret;Database=your_database;Warehouse=your_warehouse;License Key=your_license_key

To specify the refresh token manually, get it first: open the connection dialog in Visual Studio Server Explorer, select OAuth 2.0 as the authentication type, enter the client ID and client secret, and click Sign in to Snowflake. After you sign in, the dialog fills in the Refresh Token field. Then specify the values in the connection string:

Authentication Type=OAuth2;Domain=myaccount.snowflakecomputing.com;Client Id=your_client_id;Client Secret=your_client_secret;Refresh Token=your_refresh_token;Database=your_database;Warehouse=your_warehouse;License Key=your_license_key

Retrieve Snowflake data

Once the connection is open, you can run Snowflake SQL queries with SnowflakeCommand and read the results with SnowflakeDataReader. The example below reads rows from the orders table.

using Devart.Data.Snowflake;
using System;

class Program
{
  static void Main()
  {
    using SnowflakeConnection conn = new(
      "Authentication Type=Basic;" +
      "Domain=myaccount.snowflakecomputing.com;" +
      "User=your_username;" +
      "Password=your_password;" +
      "Database=your_database;" +
      "Warehouse=your_warehouse;" +
      "License Key=your_license_key");
    conn.Open();

    using SnowflakeCommand cmd = new("SELECT id, name FROM orders", conn);
    using SnowflakeDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
      Console.WriteLine(reader["name"]);
    }
  }
}

Connect using Server Explorer

You can also connect to Snowflake using Server Explorer in Visual Studio. Right-click Data Connections and select Add Connection from the context menu. In the Add Connection dialog, click Change and select Snowflake Data Source from the list of data sources.

Specify the domain, select the authentication type, and fill in the connection properties. For Programmatic Access Token, specify the user name and the token in the Password (PAT) field. For Key-pair, specify the user name, the path to the private key file, and its passphrase. For OAuth 2.0, enter the client ID and client secret and click Sign in to Snowflake to get the refresh token. Specify the warehouse and the database: the database is required to save the connection. Then click Test Connection to verify the settings.

Snowflake connection dialog in Visual Studio Server Explorer with dotConnect for Snowflake

Click OK and expand the new data connection in Server Explorer to browse the Snowflake objects.

Connect with EF Core using Entity Developer

Entity Developer allows you to visually design and generate Entity Framework Core (EF Core) models, simplifying database application development. If Entity Developer is not installed, close your Visual Studio instance, download Entity Developer, and install it following the on-screen instructions.

Then follow the detailed illustrated guide to create your model using Entity Developer. When connecting, select dotConnect for Snowflake as the provider and specify the same connection properties as in Server Explorer. On the Select database objects page, expand the schema that contains your tables and select the tables to include in the model.

Snowflake supports primary and foreign key constraints but does not enforce them. If the tables of your database have no foreign key constraints declared, the model is generated without associations between entities. You can add associations manually in the model designer to use navigation properties in your code.

EF Core model for Snowflake in Entity Developer

You can see the generated DbContext and entity classes in Visual Studio under the model. Snowflake stores unquoted identifiers in uppercase, so the generated classes and properties have uppercase names, for example, CUSTOMER and FIRSTNAME. To read Snowflake data using EF Core, execute the code below. Replace SnowflakeModel with the name of the context class generated for your model.

using System;
using System.Linq;

namespace SnowflakeEFCore
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new SnowflakeModel())
            {
                var customers = context.CUSTOMERs
                    .Where(c => c.ACTIVE)
                    .OrderBy(c => c.LASTNAME)
                    .Take(10)
                    .ToList();

                foreach (var customer in customers)
                {
                    Console.WriteLine($"{customer.FIRSTNAME} {customer.LASTNAME}, {customer.EMAIL}");
                }
            }
        }
    }
}

Connect with EF Core using Scaffold-DbContext

Another way to connect to your Snowflake data is the Scaffold-DbContext command of EF Core. This command generates the entity classes and a DbContext class based on the tables of your database.

First, install the following NuGet packages using the NuGet Package Manager in Visual Studio:

  • Microsoft.EntityFrameworkCore.Tools
  • Devart.Data.Snowflake.EFCore

The Devart.Data.Snowflake.EFCore package is released in several builds, one for each supported EF Core version. Package versions without a suffix (X.Y.Z) target EF Core 3.1, and package versions ending in .8, .9, .10, and .11 target EF Core 8, 9, 10, and 11, respectively. Select the build that matches the EF Core version of your project.

After that, open the Package Manager Console via Tools > NuGet Package Manager > Package Manager Console and run the following command to scaffold the DbContext and entity classes from your database.

Scaffold-DbContext "Authentication Type=Basic;Domain=myaccount.snowflakecomputing.com;User=your_username;Password=your_password;Database=your_database;Warehouse=your_warehouse;License Key=your_license_key" Devart.Data.Snowflake.EFCore -OutputDir Models

The -OutputDir Models parameter specifies the output directory to store the generated model classes.

After running the scaffold command, EF Core generates the DbContext class and entity classes in the specified output directory. Review the generated code to ensure it reflects your Snowflake database.

Conclusion

This tutorial has demonstrated several methods for connecting to Snowflake and accessing its data. You can choose the approach that best fits your project requirements: plain ADO.NET classes for SQL queries, Server Explorer for working with Snowflake in Visual Studio, or EF Core for working with Snowflake data as .NET objects.

dotConnect for Snowflake provides a fast and straightforward way to connect with a user name and password, an RSA key pair, or OAuth 2.0. Entity Developer allows you to visually design data models and work with Snowflake data through EF Core.

Both dotConnect for Snowflake and Entity Developer offer fully functional free trials, allowing you to integrate them into your workflow and evaluate their capabilities firsthand.

FAQ

How do I install and activate dotConnect for Snowflake in a .NET project?
Install dotConnect for Snowflake using the EXE installer or by adding the Devart.Data.Snowflake NuGet package to your project. Both the trial and the purchased versions require an activation key: get a trial key or the key of your license in your Devart Customer Portal and include it in the connection string via the License Key parameter.
Which authentication methods does dotConnect for Snowflake support?
You can authenticate with a user name and a password or a programmatic access token (PAT) by setting Authentication Type=Basic, with an RSA key pair by setting Authentication Type=KeyPair and specifying the Private Key parameter, or through OAuth 2.0 by setting Authentication Type=OAuth2 and specifying the Client Id, Client Secret, and Refresh Token parameters.
Which address should I specify in the Domain parameter?
Specify the host name of your Snowflake account, for example, orgname-accountname.snowflakecomputing.com, without the https:// prefix. You can copy it from the account details in Snowsight. The Snowsight address app.snowflake.com is not the address of your account.
Can I connect to a Snowflake user that requires multi-factor authentication?
If multi-factor authentication is required for the user, Snowflake rejects the password sign-in. Pass a programmatic access token (PAT) in the Password parameter instead of the password, use key pair authentication, which does not require a second factor, or use OAuth 2.0, which lets the user complete the sign-in in the browser.
Snowflake rejects my programmatic access token. What should I check?
Make sure that the token has not expired: by default, a token expires in 15 days. Check that the user is subject to a network policy, unless an administrator has turned off this requirement in the authentication policy. If an authentication policy limits the authentication methods, it must allow PROGRAMMATIC_ACCESS_TOKEN.
Can I connect to Snowflake using Entity Framework Core and dotConnect?
Yes, you can use Entity Developer to visually generate an EF Core model from your Snowflake database, or run Scaffold-DbContext with the Devart.Data.Snowflake.EFCore package and a dotConnect connection string (including Database and License Key) to create DbContext and entity classes.
Is it possible to work with Snowflake in the Visual Studio Server Explorer with dotConnect?
Yes. Open Server Explorer in Visual Studio, add a new Data Connection, select Snowflake Data Source as the data source, configure authentication, specify the database, and test the connection.

Elena Zemliakova LinkedIn

I'm a technical writer with a PhD in Linguistics and a focus on databases, data connectivity, and software development technologies. I enjoy turning complex technical topics into clear, practical, and easy-to-understand content.

Try the 30-day trial of the full product. No limits. No card required. Start free trial