A connection string consists of key-value pairs separated by semicolons. It specifies the Google Cloud project, the authentication settings, and other options that the provider uses to connect. This guide explains how to build connection strings for .NET applications that connect to Google BigQuery with dotConnect for Google BigQuery.
Connection string parameter names are case-insensitive. Most parameters also accept names without spaces, for example, ProjectId for Project Id.
BigQuery connection strings
dotConnect for Google BigQuery connects to BigQuery over HTTPS, so a connection string does not contain a server address or a port. It identifies the Google Cloud project, optionally the default dataset, and the credentials. The following example opens a connection using service account authentication:
using Devart.Data.BigQuery;
class Program
{
static void Main()
{
using BigQueryConnection conn = new(
"Authentication Type=ServiceAccount;" +
"Project Id=your_project_id;" +
"DataSet Id=your_dataset_id;" +
"ServiceAccount Json=your_service_account_json;" +
"License Key=your_license_key");
conn.Open();
}
}
The required authentication parameters depend on the selected authentication type. The values in the examples are placeholders. In real applications, store the service account key, the client secret, and the refresh token in secure configuration instead of hard-coding them, and do not commit them to source control.
Authentication types
The Authentication Type parameter specifies how the provider authenticates with Google Cloud. dotConnect for Google BigQuery supports three authentication types.
| Authentication Type | When to use |
|---|---|
| ServiceAccount | Server applications, services, and scheduled jobs that sign in with a Google Cloud service account key. Required parameters: Project Id, ServiceAccount Json |
| UserAccount | Applications that already have an OAuth 2.0 refresh token of a Google user account. Required parameters: Project Id, Client Id, Client Secret, Refresh Token |
| UserAccountInteractive | Desktop applications and tools where the user signs in with a Google account in the browser. Required parameter: Project Id |
Service account (ServiceAccount)
Create a service account in the Google Cloud console, generate a JSON key for it, and grant the account the roles it needs, for example, BigQuery Data Viewer and BigQuery Job User to read data and run queries. Pass the key in the ServiceAccount Json parameter. The value can be either the content of the key file or the path to that file.
using Devart.Data.BigQuery;
class Program
{
static void Main()
{
using BigQueryConnection conn = new(
"Authentication Type=ServiceAccount;" +
"Project Id=your_project_id;" +
"ServiceAccount Json=your_service_account_json;" +
"License Key=your_license_key");
conn.Open();
}
}
User account (UserAccount)
Use this authentication type when you have an OAuth 2.0 refresh token obtained through the authorization code flow. Client Id and Client Secret are the OAuth client credentials obtained in the Google Cloud console under APIs & Services > Credentials.
using Devart.Data.BigQuery;
class Program
{
static void Main()
{
using BigQueryConnection conn = new(
"Authentication Type=UserAccount;" +
"Project Id=your_project_id;" +
"Client Id=your_client_id;" +
"Client Secret=your_client_secret;" +
"Refresh Token=your_refresh_token;" +
"License Key=your_license_key");
conn.Open();
}
}
Interactive sign-in (UserAccountInteractive)
With UserAccountInteractive, the provider opens the default browser on the first connection so that the user can sign in to Google and grant access. The provider obtains the refresh token automatically and keeps it between sessions, so you do not need to specify it. If Client Id and Client Secret are not specified, the provider uses its own OAuth application. Specify your own Client Id and Client Secret to sign in through your OAuth application.
using Devart.Data.BigQuery;
class Program
{
static void Main()
{
using BigQueryConnection conn = new(
"Authentication Type=UserAccountInteractive;" +
"Project Id=your_project_id;" +
"License Key=your_license_key");
conn.Open();
}
}
In Visual Studio, the Add Connection dialog offers the User Account and Service Account options. Select User Account and click Sign in to Google BigQuery to sign in through the browser.
Main connection properties
The following table lists the main parameters used in Google BigQuery connection strings.
| Name | Description |
|---|---|
| Authentication Type | The authentication type: ServiceAccount, UserAccount, or UserAccountInteractive |
| Project Id | The unique ID of the Google Cloud project associated with the connection |
| DataSet Id | The default dataset. If this parameter is not specified, you can query all datasets of the project that the account has access to, referencing tables as dataset.table |
| ServiceAccount Json | The service account key obtained from the Google Cloud console: the content of the JSON key file or the path to that file. Used with ServiceAccount |
| Client Id | The OAuth 2.0 client ID obtained from the Google Cloud console. Used with UserAccount and, optionally, with UserAccountInteractive |
| Client Secret | The OAuth 2.0 client secret obtained from the Google Cloud console. Used with UserAccount and, optionally, with UserAccountInteractive |
| Refresh Token | The refresh token returned by the OAuth 2.0 authorization code exchange. Used with UserAccount |
| License Key | The dotConnect for Google BigQuery license key |
BigQueryConnectionStringBuilder
The BigQueryConnectionStringBuilder class builds a connection string from typed properties and helps prevent errors in parameter names and in values that contain special characters.
using Devart.Data.BigQuery;
class Program
{
static void Main()
{
BigQueryConnectionStringBuilder builder = new();
builder.AuthenticationType = AuthenticationType.ServiceAccount;
builder.ProjectId = "your_project_id";
builder.DataSetId = "your_dataset_id";
builder.ServiceAccountJson = "your_service_account_json";
builder.LicenseKey = "your_license_key";
using BigQueryConnection conn = new(builder.ConnectionString);
conn.Open();
}
}
BigQueryConnection constructor
You can pass a connection string to the BigQueryConnection constructor or assign it to the ConnectionString property of a connection created with the default constructor.
using Devart.Data.BigQuery;
class Program
{
static void Main()
{
using BigQueryConnection conn = new();
conn.ConnectionString =
"Authentication Type=ServiceAccount;" +
"Project Id=your_project_id;" +
"ServiceAccount Json=your_service_account_json;" +
"License Key=your_license_key";
conn.Open();
}
}
Proxy connection strings
If your network reaches the internet through a proxy server, specify the proxy parameters in the connection string. The provider sends all requests to BigQuery and to Google sign-in through this proxy.
using Devart.Data.BigQuery;
class Program
{
static void Main()
{
using BigQueryConnection conn = new(
"Authentication Type=ServiceAccount;" +
"Project Id=your_project_id;" +
"ServiceAccount Json=your_service_account_json;" +
"Proxy Host=your_proxy_host;" +
"Proxy Port=3128;" +
"Proxy User=your_proxy_user;" +
"Proxy Password=your_proxy_password;" +
"License Key=your_license_key");
conn.Open();
}
}
| Name | Description |
|---|---|
| Proxy Host | The name or IP address of the proxy server |
| Proxy Port | The port of the proxy server. The default value is 3128 |
| Proxy User | The proxy server account |
| Proxy Password | The password for the proxy server account |
BigQuery connections always use HTTPS. SSL/TLS certificate settings, SSH tunneling, and HTTP tunneling are not available for this provider.
Connection pooling options
Connection pooling is enabled by default. A pool is created for each distinct set of authentication data, and a connection returned to the pool can be reused by the next Open() call with the same connection string.
using Devart.Data.BigQuery;
class Program
{
static void Main()
{
using BigQueryConnection conn = new(
"Authentication Type=ServiceAccount;" +
"Project Id=your_project_id;" +
"ServiceAccount Json=your_service_account_json;" +
"Pooling=true;" +
"Min Pool Size=2;" +
"Max Pool Size=50;" +
"Connection Lifetime=300;" +
"License Key=your_license_key");
conn.Open();
}
}
| Name | Description |
|---|---|
| Pooling | If true, the connection is drawn from the appropriate pool or is created and added to the pool. The default value is true |
| Min Pool Size | The minimum number of connections allowed in the pool. The default value is 0 |
| Max Pool Size | The maximum number of connections allowed in the pool. The default value is 100 |
| Connection Lifetime | When a connection is returned to the pool, its creation time is compared with the current time. If the difference (in seconds) exceeds this value, the connection is closed. The default value is 0 (the connection always returns to the pool) |
Advanced connection string options
| Name | Description |
|---|---|
| Use Legacy SQL | Specifies whether to use the legacy SQL dialect of BigQuery for queries. The default value is false |
| Default Cloud Storage Bucket | The name of the Google Cloud Storage bucket used to temporarily place the data when it is loaded to BigQuery with BigQueryLoader |
| Primary Key Column | The name of a column that is considered the primary key column of a table if the table has such a column |
| Connection Timeout | The time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error. The default value is 60 |
| Default Command Timeout | The default command execution timeout, in seconds, unless overridden for an individual command. The default value is 60 |
| Failover Retries | The number of retries for commands that fail due to potentially transient errors. The default value is 7 |
| UTC Dates | Determines whether to treat date values as UTC dates. The default value is false |
| Initialization Command | A command that is executed immediately after the connection is opened |
| Run Once Command | A command that is executed when the connection is opened the first time. It is not executed when the connection is taken from the pool |
| Persist Security Info | When false, security-sensitive values, such as the service account key and the client secret, are removed from the ConnectionString property after the connection is opened. The parameter does not encrypt credentials. The default value is false |
| Token Server | The path to the file used to persist the refresh token between sessions. The parameter is set automatically during the web sign-in and typically does not require modification |
| UserAgent | The value of the User-Agent HTTP header |
| Enlist | Specifies whether the connection enlists in the current transaction scope. The default value is true |
| NormalizeDbNames | Performs normalization of database names. The default value is false |
For example, the following connection string sets the legacy SQL dialect, the Cloud Storage bucket for data loading, and the command timeout:
using Devart.Data.BigQuery;
class Program
{
static void Main()
{
using BigQueryConnection conn = new(
"Authentication Type=ServiceAccount;" +
"Project Id=your_project_id;" +
"DataSet Id=your_dataset_id;" +
"ServiceAccount Json=your_service_account_json;" +
"Use Legacy SQL=true;" +
"Default Cloud Storage Bucket=your_bucket_name;" +
"Default Command Timeout=120;" +
"License Key=your_license_key");
conn.Open();
}
}
Entity Framework Core connection strings
Pass the same connection string to the UseBigQuery method of DbContextOptionsBuilder. The method is provided by the Devart.Data.BigQuery.EFCore package.
using Devart.Data.BigQuery;
using Microsoft.EntityFrameworkCore;
class AppDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseBigQuery(
"Authentication Type=ServiceAccount;" +
"Project Id=your_project_id;" +
"DataSet Id=your_dataset_id;" +
"ServiceAccount Json=your_service_account_json;" +
"License Key=your_license_key");
}
}
You can also pass an existing BigQueryConnection object to UseBigQuery instead of a string.
Bulk loading connection strings
BigQueryLoader uploads the rows to a Google Cloud Storage bucket as compressed CSV files and then starts a BigQuery load job. The authenticated account must have permissions to access the bucket, so set Default Cloud Storage Bucket in the connection string or assign the CloudStorageBucketName property of the loader.
using Devart.Data.BigQuery;
class Program
{
static void Main()
{
using BigQueryConnection conn = new(
"Authentication Type=ServiceAccount;" +
"Project Id=your_project_id;" +
"DataSet Id=your_dataset_id;" +
"Default Cloud Storage Bucket=your_bucket_name;" +
"ServiceAccount Json=your_service_account_json;" +
"License Key=your_license_key");
conn.Open();
BigQueryLoader loader = new("orders", conn);
loader.CreateColumns();
loader.Open();
loader.SetValue("id", 1L);
loader.SetValue("name", "Laptop");
loader.NextRow();
loader.Close();
}
}
Conclusion
Choose the authentication type that fits your application: ServiceAccount for server applications and scheduled jobs, UserAccount when the application already has an OAuth 2.0 refresh token, and UserAccountInteractive for desktop tools where users sign in through the browser. Add the default dataset, proxy, pooling, and Cloud Storage bucket parameters only when your scenario requires them.
You can download the fully functional free trial of dotConnect for Google BigQuery and test it with your actual workload.