DataConnector | ComponentOne
ADO.NET provider for Snowflake / Getting Started
In This Topic
    Getting Started
    In This Topic

    The ADO.NET provider for Snowflake provides a wide range of features that enable connectivity to Snowflake from .Net applications. The documentation will help you understand the C1.AdoNet.Snowflake namespace, which includes all the available classes that can be used to connect and retrieve data from Snowflake.

    DataConnectors are mostly used in combination with other ComponentOne components, such as DataEngine and FlexPivot. The procedure below describes the first steps to use the DataConnector in a console application within Visual Studio.

    How to create a new Console Application

    The ADO.NET provider for Snowflake can be used in any application. In this guide, a console application is created:

    1. Open Visual Studio.
    2. Select Create a new project from the Get Started pane.
    3. In the Create a new project window, select Console Application and click Next, as in the screenshot below: Create new project window
    4. In the Configure your new project window, write your project name, choose a location to save your project and click Create.

    How to add the NuGet packages

    To use the ADO.NET provider for Snowflake in an application, the respective NuGet package should be added:

    1. From the Project menu, select Manage NuGet Packages.
    2. In the NuGet Package Manager, click the Package source drop-down and select nugget.org.
    3. In the left pane of the Browse tab, select C1.AdoNet.Snowflake.
    4. In the right pane of the Browse tab, click Install to add the reference to the package.

    How to use ADO.NET provider for Snowflake to retrieve data

    1. The first step is to add the required connection parameters to your C# code. This includes your Snowflake account information, such as URL, Username, Password, Role, and Database details. In this example, we will use placeholders for these values.

    C#
    Copy Code
    const string Url = "https://xxxx.eu-west-2.aws.snowflakecomputing.com";
    const string Account = "xxxx.eu-west-2.aws";
    const string Warehouse = "****";
    const string Role = "****";
    const string Database = "****";
    const string Schema = "****";
    const string OAuthClientId = "****";
    const string OAuthClientSecret = "****";
    const string OAuthAccessToken = "****";
    const string OAuthRefreshToken = "****";
    static string connectionString => $"account={Account};warehouse={Warehouse};schema={Schema};role={Role};database={Database};url={Url};"

    2. Fetch the data using the C1SnowflakeConnection class. The connection string using corresponding attributes is passed as an argument. For more information on creating connections, see Creating Connection.

    C1SnowflakeConnection implements the ADO.NET DbConnection, similar to a standard Snowflake connection object that retrieves a single result set of all the data that matches a query. Once the connection is established, it retrieves the data from the source as shown in the following code example.

     

    Static void ReadData()

    C#
    Copy Code
    static void ReadData()
    {
        Console.WriteLine("Starting...");
        //Fetch data
        using (var conn = new C1SnowflakeConnection(connectionString))
        {
            con.Open();
            var cmd = con.CreateCommand();
            //Provide command
            cmd.CommandText = "Select * From CALL_CENTER";
            var reader = cmd.ExecuteReader();
            PrintStringContentFromReader(reader);
        }
    }
    static void PrintStringContentFromReader(DbDataReader reader)
            {
            NextResult:
                var cols = new List<string>();
                var fieldCount = reader.FieldCount;
                for (var i = 0; i < fieldCount; i++)
                {
                    cols.Add(reader.GetName(i));
                }
                Console.WriteLine(string.Join(",", cols));
                while (reader.Read())
                {
                    var values = new List<string>();
                    for (var i = 0; i < fieldCount; i++)
                    {
                        var objValue = reader.GetValue(i);
                        string strValue;
                        if (objValue == null || DBNull.Value == objValue)
                        {
                            strValue = "";
                        }
                        else
                        {
                            var dataType = reader.GetFieldType(i);
                            if (dataType == typeof(DateTime))
                            {
                                strValue = ((DateTime)objValue).ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                            }
                            else
                            {
                                strValue = Convert.ToString(Convert.ChangeType(objValue, dataType, CultureInfo.InvariantCulture), CultureInfo.InvariantCulture);
                            }
                        }
                        values.Add(strValue);
                    }
                    Console.WriteLine(string.Join(",", values));
                }
                if (reader.NextResult())
                    goto NextResult;
            }

    The following output is generated on the console application on executing the above steps.