Search code examples
c#snowflake-cloud-data-platform

How to prevent multiple browser pop up for snowflake connection when using externalbrowser authentication?


I'm using Snowflake.Data.Client; to connect to snowflake in C#.

sfConnString = @"account=MY_ACCOUNT;authenticator=externalbrowser;database=MY_DB;warehouse=MY_WH;role=My_ROLE;

This is my method to run queries:

    public static void SnowflakeQuery(string query)
    {
        using (SnowflakeDbConnection conn = new(sfConnString))
        {
            conn.Open();
            SnowflakeDbCommand cmd = new();
            cmd.Connection = conn;
            cmd.CommandText = query;
            cmd.ExecuteNonQuery();
        }
    }

This works but every time I call SnowflakeQuery method, a browser pop-up happens before the query runs. How do I make it so I only need to authenticate once per session? Kinda like the Snowflake extension on VS Code.

VS Code snowflake extension


Solution

  • I figured it out. I just had to move the SnowflakeDbConnection object out of the method so it doesn't create a new connection each time.

    readonly private static SnowflakeDbConnection conn = new(sfConnString)
    
    public static void SnowflakeQuery(string query)
    {
        if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken) conn.Open();
        SnowflakeDbCommand cmd = new();
        cmd.Connection = conn;
        cmd.CommandText = query;
        cmd.ExecuteNonQuery();
    }