dotConnect for Snowflake Documentation
On this page
    Connecting to Snowflake
    On this page

    This tutorial describes how to connect to Snowflake.

    Requirements

    To connect to Snowflake, you need to have the corresponding account, dotConnect for Snowflake installed, and the IDE running. You also have to know the required connection parameters described below.

    Note that you must activate the product on the computer where your application runs; otherwise, provide the activation key at run time with the License Key connection string parameter or the SnowflakeConnection.LoadLicenseKey method. For more information, see Activation.

    Preparing Snowflake for Connection

    Before you connect, find out the address of your Snowflake account and make sure that the Snowflake user has the privileges required to work with the data.

    Account address

    To find the address of your account in Snowsight, open the account selector, select View account details, and copy the Account/Server URL value, for example, myorg-myaccount.snowflakecomputing.com. Specify this value in the Domain connection string parameter.

    You can also specify only the account identifier. In the account address, the organization name and the account name are separated with a hyphen, for example, myorg-myaccount, not with a dot as in the myorg.myaccount form used in SQL. If your account is identified by a legacy account locator, specify it with the region and the cloud platform, for example, xy12345.us-east-2.aws. For more information, see Account identifiers in the Snowflake documentation.

    User privileges

    The role used in the connection needs the USAGE privilege on the warehouse, the database, and the schema, and the privileges on the tables that your application works with, such as SELECT, INSERT, UPDATE, and DELETE. The following statements create a role with these privileges, grant it to the user, and make it and the warehouse the defaults of the user:

    CREATE ROLE app_role;
    GRANT USAGE ON WAREHOUSE your_warehouse TO ROLE app_role;
    GRANT USAGE ON DATABASE your_database TO ROLE app_role;
    GRANT USAGE ON SCHEMA your_database.your_schema TO ROLE app_role;
    GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA your_database.your_schema TO ROLE app_role;
    GRANT ROLE app_role TO USER your_user;
    ALTER USER your_user SET DEFAULT_ROLE = app_role, DEFAULT_WAREHOUSE = your_warehouse;

    Run these statements with a role that can create roles and manage grants, for example, SECURITYADMIN. The GRANT ... ON ALL TABLES statement applies to the existing tables only. To grant the privileges on the tables created later, also run the same statement with ON FUTURE TABLES IN SCHEMA.

    Instead of setting the defaults of the user, you can specify the role and the warehouse in the Role and Warehouse connection string parameters. If neither the Warehouse parameter nor the default warehouse of the user is set, the connection opens, but queries fail because the session has no active warehouse. For more information on the privileges, see Overview of Access Control in the Snowflake documentation.

    For the privileges required to bulk load data with loaders, see Bulk Data Loading.

    Network access

    Besides the account address, dotConnect for Snowflake connects to the cloud storage of the account (Amazon S3, Azure Blob Storage, or Google Cloud Storage): large query results are downloaded from it, and SnowflakeInternalLoader uploads files to it. If a firewall restricts outgoing connections of your application, allow these hosts too. To get the list of hosts, run SELECT SYSTEM$ALLOWLIST() in Snowflake. If you connect through a proxy server, the proxy settings of the connection apply to these connections as well.

    Required Connection Parameters

    To establish a connection to the server, you have to provide the required connection parameters to dotConnect for Snowflake. This information is used by SnowflakeConnection component to connect to Snowflake. The parameters are represented as a connection string. You can compose the connection string manually or have dotConnect for Snowflake construct it for you.

    The following connection string parameters are required:

    You can also specify the following optional parameters:

    For more information on authentication methods and on how to get the private key or the refresh token, see Authentication.

    Create a Connection to Snowflake

    Create a Connection at Design Time

    Before you begin, open your project in Visual Studio and switch to the form designer.

    1. In Toolbox, locate the SnowflakeConnection component in the Snowflake Data category.
    2. Double-click the component to add it to the form. The component appears in the component tray below the form. By default, the first instance is named snowflakeConnection1.
    3. Select snowflakeConnection1 and press F4 to open the Properties window.
    4. Specify the required connection string parameters described above in the corresponding property fields.

      As you configure the properties, the ConnectionString property updates automatically to reflect your settings. The resulting connection string might look like this: "Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********".

    5. To configure additional connection string parameters, select Advanced.

    For a complete list of supported connection string parameters and their descriptions, see SnowflakeConnection.ConnectionString.

    Create a Connection at Run Time

    You can also configure a connection at run-time by setting its ConnectionString property (note that you have to add references to Devart.Data.Snowflake.dll, Devart.Data.SqlShim.dll, and Devart.Data.dll assemblies):

    snowflakeConnection1.ConnectionString = "Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********";
    
    snowflakeConnection1.ConnectionString = "Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********"
    

    Use a Connection String Builder

    If you decide to set up the connection by assigning values to several properties, consider using SnowflakeConnectionStringBuilder class. It has all of the possible connection settings exposed as properties, thus allowing you to customize the connection to the full extent.

    SnowflakeConnectionStringBuilder connectionStringBuilder = new SnowflakeConnectionStringBuilder();
    
    connectionStringBuilder.Domain = "myorg-myaccount";
    connectionStringBuilder.Database = "your_database";
    connectionStringBuilder.Warehouse = "your_warehouse";
    connectionStringBuilder.User = "your_user";
    connectionStringBuilder.Password = "your_password";
    // For KeyPair authentication, use the following instead of the password:
    // connectionStringBuilder.AuthenticationType = SnowflakeAuthenticationType.KeyPair;
    // connectionStringBuilder.PrivateKey = System.IO.File.ReadAllText(@"C:\keys\rsa_key.p8");
    
    SnowflakeConnection myConnection = new SnowflakeConnection(connectionStringBuilder.ConnectionString);
    
    Dim connectionStringBuilder As SnowflakeConnectionStringBuilder = New SnowflakeConnectionStringBuilder
    
    connectionStringBuilder.Domain = "myorg-myaccount"
    connectionStringBuilder.Database = "your_database"
    connectionStringBuilder.Warehouse = "your_warehouse"
    connectionStringBuilder.User = "your_user"
    connectionStringBuilder.Password = "your_password"
    ' For KeyPair authentication, use the following instead of the password:
    ' connectionStringBuilder.AuthenticationType = SnowflakeAuthenticationType.KeyPair
    ' connectionStringBuilder.PrivateKey = System.IO.File.ReadAllText("C:\keys\rsa_key.p8")
    
    Dim myConnection As SnowflakeConnection = New SnowflakeConnection(connectionStringBuilder.ConnectionString)

    Notice that in this example we used the SnowflakeConnection constructor that accepts a connection string as an argument.

    For information on the arguments allowed in the connection string, refer to the description of the SnowflakeConnection.ConnectionString property.

    Create a Connection in Server Explorer

    To create a Server Explorer connection:

    1. On the Server Explorer toolbar, click Connect to Database.
    2. Under Data source, select Snowflake Data Source.
    3. Under Data provider, select dotConnect for Snowflake, then click Continue.
    4. Specify the Domain and the Database, select the authentication type, and specify the parameters required for it. For more information, see Authentication. The provider connects without a database, but Server Explorer needs one to show tables and views: if Database is empty, Test Connection checks the credentials and then reports that the database is missing, and the OK button stays unavailable.
    5. Click OK.
    Snowflake connection editor

    After this, you can browse Snowflake objects in Server Explorer.

    Snowflake connection in Server Explorer

    Opening a Connection

    To open a connection, call the Open method.

    myConnection1.Open();
    
    myConnection1.Open()
    

    The myConnection1 object must have a valid connection string assigned earlier. When you call Open, dotConnect for Snowflake connects to Snowflake and signs in. If the connection isn't established within the Connection Timeout interval (60 seconds by default) or another problem occurs, Open throws an exception with a brief explanation of what is wrong. When the connection is established, the Open method returns, and the State property changes to Open.

    To connect to the server at design time:

    1. Right-click the connection object in designer and select Connect.
    2. In the dialog window, enter the required connection parameters.
    3. If necessary, click Advanced and configure other connection string parameters. You can find the description of these connection string parameters in the SnowflakeConnection.ConnectionString topic.
    4. Click Connect.

    Closing a Connection

    To close a connection, call its Close method.

    The following example summarizes the aforementioned information and shows how to create, set up, open, use, and then close the connection.

    SnowflakeConnection myConn = new SnowflakeConnection();
    myConn.ConnectionString = "Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********";
    myConn.Open();
    MessageBox.Show(myConn.State.ToString());
    myConn.Close();
    
    Dim myConn As SnowflakeConnection = New SnowflakeConnection()
    myConn.ConnectionString = "Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********"
    myConn.Open()
    MessageBox.Show(myConn.State.ToString())
    myConn.Close()
    

    The sample code connects to Snowflake, shows the connection state, and then closes the connection. This is actually a rare usage, because in real applications connections are used by other objects like SnowflakeCommand, SnowflakeDataReader and others. For more information on this, see the corresponding tutorials or reference information.

    Modifying a Connection

    You can modify a connection by changing properties of the SnowflakeConnection object. Keep in mind that while some of the properties can be altered freely, most of them close the connection when a new value is assigned.

    Connection String Examples

    The following examples demonstrate common connection configurations for dotConnect for Snowflake. Replace the placeholder values with your own connection details.

    For more information about the supported authentication methods, see Authentication.

    Basic authentication

    To connect using a username and a programmatic access token, specify the token in the Password parameter. A password also works if the user isn't required to use multi-factor authentication.

    Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=your_token;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********

    Key-pair authentication

    To authenticate using a private key file, set the Authentication Type parameter to KeyPair and specify the path to the private key file. If the private key is encrypted, also specify the Passphrase parameter.

    Authentication Type=KeyPair;Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Private Key=C:\keys\rsa_key.p8;Passphrase=your_passphrase;Database=your_database;Warehouse=your_warehouse;License Key=**********

    OAuth 2.0 authentication

    To authenticate using OAuth 2.0, set the Authentication Type parameter to OAuth2 and specify the OAuth credentials.

    Authentication Type=OAuth2;Domain=myorg-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=**********

    To sign in through a browser and get the refresh token when the connection is opened, set Authentication Type to OAuth2Interactive and omit Refresh Token. This works only in an application that can show a browser; otherwise, an empty Refresh Token causes an error. For more information, see Interactive Sign-In.

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

    Role selection

    To use a specific role instead of the user's default role, add the Role parameter to the connection string.

    ...;Role=app_role

    Proxy server configuration

    To connect through a proxy server, specify the proxy host and port. If the proxy server requires authentication, also specify the proxy credentials.

    ...;Proxy Host=proxy.example.com;Proxy Port=3128;Proxy User=proxy_user;Proxy Password=proxy_password

    Session settings, connection pooling, and timeouts

    To customize session settings, configure connection pooling, and specify connection and command timeouts, add the corresponding parameters to the connection string.

    ...;Initialization Command="ALTER SESSION SET TIMEZONE = 'UTC'";Min Pool Size=2;Max Pool Size=20;Connection Timeout=30;Default Command Timeout=600
    Warning Don't store credentials directly in your application source code. For recommendations on securing connection credentials, see Security Best Practices.

    For a complete list of connection string parameters, see SnowflakeConnection.ConnectionString.

    For help with connection issues, see Troubleshooting.

    Sessions and Connection Pooling

    Each open connection works in its own Snowflake session. Connection pooling is enabled by default: when you close a connection, it goes back to the pool together with its session, and the next connection with the same connection string gets this session instead of signing in again.

    When a connection is closed, dotConnect for Snowflake rolls back its open transaction and restores the database and the schema of the connection string if you changed them with the ChangeDatabase method. Other session state stays with the pooled session, and the next connection that gets it from the pool works with this state:

    If your application changes the session state with SQL, restore it before closing the connection, use the ChangeDatabase method instead of USE DATABASE, or disable pooling with the Pooling=false connection string parameter.

    To initialize session settings, use one of the following connection string parameters:

    For example, the following connection string parameter sets a query tag each time a connection opens:

    ...;Initialization Command="ALTER SESSION SET QUERY_TAG = 'billing-service'"

    Use the following connection string parameters to configure connection pooling:

    Parameter

    Description

    Default Value

    PoolingEnables or disables connection pooling.true
    Min Pool SizeThe minimum number of sessions maintained in the pool. The provider creates these sessions when the first connection with this connection string opens.0
    Max Pool SizeThe maximum number of sessions in the pool.100
    Connection LifetimeThe maximum lifetime of a session, in seconds. When a connection returns to the pool, the provider closes its session if its age exceeds the specified lifetime. A value of 0 disables the lifetime limit.0

    Reusing a pooled session eliminates the need to authenticate with Snowflake each time a connection opens, reducing connection opening time.

    To clear connection pools, use the following methods:

    After clearing a pool, the next connection to that pool requires authentication and session creation.

    The ChangeDatabase method runs USE DATABASE and moves the session to the PUBLIC schema of that database. If the database has no PUBLIC schema, the session has no current schema, and unqualified object names can't be resolved. The SnowflakeConnection.Schema property returns the Schema value of the connection string. If it is not set or ChangeDatabase was called, the property returns the current schema of the session.

    The Ping method signs out and signs in again, so the connection gets a new session: temporary tables and session variables are lost. Don't call Ping while a transaction is open: the transaction is lost too.

    If a session expires, dotConnect for Snowflake signs in again automatically. If a transaction was open in the expired session, the following exception is thrown instead: The Snowflake session expired while a transaction was open, and a new login would start a new session. The transaction is lost and its changes are not committed; roll it back and run it again.

    If a request to Snowflake fails because of a network error, it is sent again as many times as the Failover Retries connection string parameter specifies. While a transaction is open, requests are not sent again, because the statement could already be executed: the error is thrown, and you need to roll the transaction back and run it again.

    You can execute a command while a data reader of the same connection is open.

    Command Timeouts and Cancellation

    The default command timeout of dotConnect for Snowflake is 3600 seconds. You can change it with the SnowflakeCommand.CommandTimeout property or the Default Command Timeout connection string parameter. When the timeout elapses, the statement is aborted on the Snowflake side, and an exception is thrown. The BeginTransaction method of the connection and the Commit and Rollback methods of SnowflakeTransaction are never aborted by a timeout. BEGIN, COMMIT, and ROLLBACK statements that you execute with a command are subject to the command timeout like any other statement.

    The SnowflakeCommand.Cancel method aborts the running statement on the Snowflake side: the execution fails with the "SQL execution canceled" error of Snowflake. A canceled CancellationToken of an asynchronous method has the same effect.

    The ExecuteReader method returns after Snowflake has executed the query. If you close a data reader before reading all rows, the query isn't aborted, because it is already complete: the provider only stops downloading the rest of the result.

    The Connection Timeout connection string parameter specifies the time limit, in seconds, for signing in to Snowflake when opening a connection. The default value is 60 seconds.

    For information about connection and command execution errors, see Troubleshooting.