Stay organized with collectionsSave and categorize content based on your preferences.
Query a public dataset with the BigQuery C# client library
Query a public dataset with the BigQuery C# client library
Learn how to:
Activate Cloud Shell in a Google Cloud project.
Open the Cloud Shell Editor.
Prepare files for queries.
Query a public dataset in BigQuery.
Clean up.
Estimated time to complete:
ClickStartto begin.
Activate Cloud Shell in a Google Cloud project
If you do not enable billing for a project, you automatically work in the
BigQuery sandbox. The BigQuery sandbox lets you learn
BigQuery with a limited set of BigQuery
features at no charge. If you do not plan to use your project beyond this
document, we recommend that you use the BigQuery sandbox.
ClickActivate
Cloud Shell.Show
me
To learn how to open the Cloud Shell Editor, clickNext.
Open the Cloud Shell Editor
In Cloud Shell, create a new C# project and file:
dotnetnewconsole-nBigQueryCsharpDemo
The output is similar to the following. Several lines are omitted to
simplify the output.
Welcome to .NET 6.0!
---------------------
SDK Version: 6.0.407
...
The template "Console App" was created successfully.
...
This command creates a C# project that's namedBigQueryCsharpDemoand a file that's namedProgram.cs.
Open the Cloud Shell Editor:
cloudshellworkspaceBigQueryCsharpDemo
To learn how to prepare files for queries, clickNext.
Prepare files for queries
To open a terminal in the Cloud Shell Editor, clickOpen Terminal.
Open your project directory:
cdBigQueryCsharpDemo
Install the BigQuery client library for C#:
dotnetaddpackageGoogle.Cloud.BigQuery.V2
The output is similar to the following. Several lines are omitted to
simplify the output.
Determining projects to restore...
Writing /tmp/tmpF7EKSd.tmp
...
info : Writing assets file to disk.
...
To learn how to query a public dataset in BigQuery, clickNext.
Query a public dataset in BigQuery
Set the variableGOOGLE_PROJECT_IDto the valueGOOGLE_CLOUD_PROJECTand export the variable:
exportGOOGLE_PROJECT_ID=$GOOGLE_CLOUD_PROJECT
ClickOpen Editor.
In theExplorerpane, locate yourBIGQUERYCSHARPDEMOproject.
Click theProgram.csfile to open it.
To create a query against thebigquery-public-data.stackoverflowdataset that returns the
top 10 most viewed Stack Overflow pages and their view counts, replace
the contents of the file with the following code:
usingSystem;usingGoogle.Cloud.BigQuery.V2;namespaceGoogleCloudSamples{publicclassProgram{publicstaticvoidMain(string[]args){stringprojectId=Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");varclient=BigQueryClient.Create(projectId);stringquery=@"SELECTCONCAT('https://stackoverflow.com/questions/',CAST(id as STRING)) as url, view_countFROM `bigquery-public-data.stackoverflow.posts_questions`WHERE tags like '%google-bigquery%'ORDER BY view_count DESCLIMIT 10";varresult=client.ExecuteQuery(query,parameters:null);Console.Write("\nQuery Results:\n------------\n");foreach(varrowinresult){Console.WriteLine($"{row["url"]}: {row["view_count"]} views");}}}}
ClickOpen Terminal.
In the terminal, run theProgram.csscript. If you are prompted to
authorize Cloud Shell and agree to the terms, clickAuthorize.
To avoid incurring charges to your Google Cloud account, either delete
your Google Cloud project, or delete the resources that you created in
this walkthrough.
Delete the project
If you created a new project to learn about BigQuery and you no
longer need the project,delete it. Be aware
that deleting a project deletes everything in the project and custom project IDs
are lost.
Delete the resources
If you used an existing project, delete theBigQueryCsharpDemofolder
that you created:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["Query a public dataset with the BigQuery C# client library\n\n\nQuery a public dataset with the BigQuery C# client library\n==========================================================\n\nLearn how to:\n\n\u003cbr /\u003e\n\n1. Activate Cloud Shell in a Google Cloud project.\n2. Open the Cloud Shell Editor.\n3. Prepare files for queries.\n4. Query a public dataset in BigQuery.\n5. Clean up.\n\nEstimated time to complete:\n\nClick **Start** to begin.\n\nActivate Cloud Shell in a Google Cloud project\n----------------------------------------------\n\n1.\n\n If you do not enable billing for a project, you automatically work in the\n BigQuery sandbox. The BigQuery sandbox lets you learn\n BigQuery with a limited set of BigQuery\n features at no charge. If you do not plan to use your project beyond this\n document, we recommend that you use the BigQuery sandbox.\n2. \n3. Click **Activate\n Cloud Shell** . Show me\n\nTo learn how to open the Cloud Shell Editor, click **Next**.\n\nOpen the Cloud Shell Editor\n---------------------------\n\n1. In Cloud Shell, create a new C# project and file:\n\n dotnet new console -n BigQueryCsharpDemo\n\n The output is similar to the following. Several lines are omitted to\n simplify the output. \n\n Welcome to .NET 6.0!\n ---------------------\n SDK Version: 6.0.407\n ...\n The template \"Console App\" was created successfully.\n ...\n\n This command creates a C# project that's named\n `BigQueryCsharpDemo` and a file that's named `Program.cs`.\n2. Open the Cloud Shell Editor:\n\n cloudshell workspace BigQueryCsharpDemo\n\nTo learn how to prepare files for queries, click **Next**.\n\nPrepare files for queries\n-------------------------\n\n1. To open a terminal in the Cloud Shell Editor, click\n **Open Terminal**.\n\n2. Open your project directory:\n\n cd BigQueryCsharpDemo\n\n3. Install the BigQuery client library for C#:\n\n dotnet add package Google.Cloud.BigQuery.V2\n\n The output is similar to the following. Several lines are omitted to\n simplify the output. \n\n Determining projects to restore...\n Writing /tmp/tmpF7EKSd.tmp\n ...\n info : Writing assets file to disk.\n ...\n\nTo learn how to query a public dataset in BigQuery, click\n**Next**.\n\nQuery a public dataset in BigQuery\n----------------------------------\n\n1. Set the variable `GOOGLE_PROJECT_ID` to the value `GOOGLE_CLOUD_PROJECT`\n and export the variable:\n\n export GOOGLE_PROJECT_ID=$GOOGLE_CLOUD_PROJECT\n\n2. Click **Open Editor**.\n\n3. In the **Explorer** pane, locate your `BIGQUERYCSHARPDEMO`\n project.\n\n4. Click the `Program.cs` file to open it.\n\n5. To create a query against the\n `bigquery-public-data.stackoverflow` dataset that returns the\n top 10 most viewed Stack Overflow pages and their view counts, replace\n the contents of the file with the following code:\n\n\n\n using System;\n using https://cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.html;\n\n namespace GoogleCloudSamples\n {\n public class Program\n {\n public static void Main(string[] args)\n {\n string projectId = Environment.GetEnvironmentVariable(\"GOOGLE_PROJECT_ID\");\n var client = https://cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.BigQueryClient.html.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.BigQueryClient.html#Google_Cloud_BigQuery_V2_BigQueryClient_Create_System_String_Google_Apis_Auth_OAuth2_GoogleCredential_(projectId);\n string query = @\"SELECT\n CONCAT(\n 'https://stackoverflow.com/questions/',\n CAST(id as STRING)) as url, view_count\n FROM `bigquery-public-data.stackoverflow.posts_questions`\n WHERE tags like '%google-bigquery%'\n ORDER BY view_count DESC\n LIMIT 10\";\n var result = client.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.BigQueryClient.html#Google_Cloud_BigQuery_V2_BigQueryClient_ExecuteQuery_System_String_System_Collections_Generic_IEnumerable_Google_Cloud_BigQuery_V2_BigQueryParameter__Google_Cloud_BigQuery_V2_QueryOptions_Google_Cloud_BigQuery_V2_GetQueryResultsOptions_(query, parameters: null);\n Console.Write(\"\\nQuery Results:\\n------------\\n\");\n foreach (var row in result)\n {\n Console.WriteLine($\"{row[\"url\"]}: {row[\"view_count\"]} views\");\n }\n }\n }\n }\n\n \u003cbr /\u003e\n\n6. Click **Open Terminal**.\n\n7. In the terminal, run the `Program.cs` script. If you are prompted to\n authorize Cloud Shell and agree to the terms, click **Authorize**.\n\n dotnet run\n\n The result is similar to the following: \n\n Query Results:\n ------------\n https://stackoverflow.com/questions/35159967: 170023 views\n https://stackoverflow.com/questions/22879669: 142581 views\n https://stackoverflow.com/questions/10604135: 132406 views\n https://stackoverflow.com/questions/44564887: 128781 views\n https://stackoverflow.com/questions/27060396: 127008 views\n https://stackoverflow.com/questions/12482637: 120766 views\n https://stackoverflow.com/questions/20673986: 115720 views\n https://stackoverflow.com/questions/39109817: 108368 views\n https://stackoverflow.com/questions/11057219: 105175 views\n https://stackoverflow.com/questions/43195143: 101878 views\n\nYou have successfully queried a public dataset with the\nBigQuery C# client library.\n\nTo avoid incurring charges to your account and learn about next steps, click\n**Next**.\n\nNext steps\n----------\n\n\nKeep the resources that you created and do more with BigQuery, or clean up to avoid\nbilling charges.\n\n### Do more with BigQuery\n\n- Learn more about using the [BigQuery C# client library](/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest).\n- Learn more about [BigQuery public datasets](/bigquery/public-data).\n- Learn how to [load data into BigQuery](/bigquery/docs/loading-data).\n- Learn more about [querying data in BigQuery](/bigquery/docs/query-overview).\n- Get [updates about BigQuery](/bigquery/docs/release-notes).\n- Learn about [BigQuery pricing](/bigquery/pricing).\n- Learn about [BigQuery quotas and limits](/bigquery/quotas).\n\n### Clean up\n\nTo avoid incurring charges to your Google Cloud account, either delete\nyour Google Cloud project, or delete the resources that you created in\nthis walkthrough.\n\n### Delete the project\n\nIf you created a new project to learn about BigQuery and you no\nlonger need the project,\n[delete it](https://console.cloud.google.com/cloud-resource-manager). Be aware\nthat deleting a project deletes everything in the project and custom project IDs\nare lost.\n\n### Delete the resources\n\nIf you used an existing project, delete the `BigQueryCsharpDemo` folder\nthat you created:\n\n1. In Cloud Shell, move up a directory:\n\n cd ..\n\n2. Delete the resources that you created:\n\n rm -R BigQueryCsharpDemo\n\n The `-R` flag deletes all assets in a folder."]]