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.
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.
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.
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.
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.
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.
Devart implements the standard ADO.NET provider model in dotConnect solutions, ensuring consistent behavior and compatibility with existing .NET frameworks and development tools.
Devart provides professional technical assistance, comprehensive documentation, and ongoing product enhancements, helping developers maintain stable and reliable integrations with Snowflake.
You can try dotConnect for Snowflake free for 30 days. Choose one of the following installation options:
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.
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.
| 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 |
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:
For more information, see Using programmatic access tokens for authentication in the Snowflake documentation.
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.
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.
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:
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"]);
}
}
}
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.
Click OK and expand the new data connection in Server Explorer to browse the Snowflake objects.
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.
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}");
}
}
}
}
}
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:
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.
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.
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.