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.
The ADO.NET provider for Snowflake can be used in any application. In this guide, a console application is created:
To use the ADO.NET provider for Snowflake in an application, the respective NuGet package should be added:
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.