How to connect Google Drive with C# a step by step guide

Introduction

Connecting Google Drive to your C# application via ODBC enables SQL-like access to your cloud storage. With this approach, you can list files, read metadata, and even automate data handling without writing direct REST API calls.

This guide walks you through the process using the ZappySys ODBC API Driver and a fully optimized C# console app.

Prerequisites

  • Download and install the ZappySys ODBC PowerPack.
  • A Google account with access to Google Drive.
  • Visual Studio (.NET Framework or .NET Core).
  • Basic knowledge of C#.

Steps

Step 1: Create/Select a Project in the Google API Console

  1. Navigate to the Google API Console.

  2. Click on the Project Dropdown at the top bar and either select an existing project or create a new one by clicking CREATE PROJECT.

  3. Once the project is set, click ENABLE APIS AND SERVICES.

  4. Enable both Sheets API and Drive API by searching for them and clicking ENABLE.

  5. Return to the main screen and click on the OAuth Consent Screen tab. Provide the necessary details and save.

  6. Move to the Credentials tab.

  7. Click CREATE CREDENTIALS in the top bar, choose OAuth Client ID, select Desktop App as the Application Type, and click Create to obtain your Client ID and Secret.

Step 2: Create a New Driver

  1. Open the ODBC Data Source by typing “ODBC” in the search box and launching the ODBC Data Source.

  2. To gain access for yourself or other users, go to User DSN or System DSN. Go to the System tab for SQL Server Integration and add a new System DSN. Click the Add button.

  3. From the driver list, select ZappySys API Driver, then select Google Drive in the connector list and press Continue.

Step 3: Google Drive Connector Configuration

  1. Fill in the connector fields, including Client ID, Client Secret, and Scopes.

  2. Generate the token and test the connection.

  3. Go to the Preview Tab, select any table, and preview the result. Press OK to save the configuration.

Step 4: Create and configure your C# project

  1. Open Visual Studio.
  2. Create a new Console App (.NET or .NET Core).
  3. No extra packages are needed if using System.Data.Odbc.

Step 4: Add the optimized C# code

Replace the Program.cs content with the following optimized version:

using System;
using System.Data.Odbc;

namespace ConsoleAppZappysys
{
    internal class Program
    {
        // ===================== Generic Query Executor =====================
        // This function connects to the ODBC data source, runs a SQL query,
        // and prints the results in the console.
        static void ExecuteQuery(string dsnName, string sql, string label)
        {
            // Create the ODBC connection string using the DSN name you set up earlier
            string connectionString = $"DSN={dsnName}";

            try
            {
                // Create a connection to the ODBC driver
                using (var connection = new OdbcConnection(connectionString))
                {
                    connection.Open(); // Open the connection

                    // Create a command object to run the SQL query
                    using (var command = new OdbcCommand(sql, connection))
                    {
                        command.CommandTimeout = 600; // Set timeout to 10 minutes (600 seconds)

                        // Run the query and get the results
                        using (var reader = command.ExecuteReader())
                        {
                            int rowNumber = 1; // Keep track of which row we're printing
                            Console.WriteLine($"\n------------ {label} ---------------");

                            // Read each row from the result
                            while (reader.Read())
                            {
                                Console.WriteLine($"\n--- Record #{rowNumber} ---");

                                // Loop through each column in the current row
                                for (int i = 0; i < reader.FieldCount; i++)
                                {
                                    string columnName = reader.GetName(i); // Get column name
                                    string value = reader[i]?.ToString() ?? "(null)"; // Get value or show (null) if empty
                                    Console.WriteLine($"{columnName}: {value}"); // Print column name and value
                                }

                                rowNumber++; // Move to next row
                            }

                            // If no rows were found
                            if (rowNumber == 1)
                            {
                                Console.WriteLine("No records found.");
                            }
                        }
                    }
                }
            }
            // Catch errors related to ODBC connections (e.g., invalid DSN, credentials, etc.)
            catch (OdbcException ex)
            {
                Console.WriteLine("ODBC error occurred:");
                Console.WriteLine(ex.Message);
            }
            // Catch any other general errors
            catch (Exception ex)
            {
                Console.WriteLine("An unexpected error occurred:");
                Console.WriteLine(ex.Message);
            }
        }

        // =========================== Google Drive ============================
        // This function is specific to reading data from your Google Drive
        static void GetGoogleDrive()
        {
            // Name of the DSN you configured in the ODBC Data Source Administrator
            string dsnName = "Google Drive - API Drive";

            // SQL query to get specific file information from Google Drive
            string query = "SELECT Id,Name,MimeType,CreatedTime,ModifiedTime FROM Files";

            // A label used when displaying results in the console
            string title = "Google Drive Files";

            // Run the generic query function with these parameters
            ExecuteQuery(dsnName, query, title);
        }

        // =========================== Program Entry ============================
        // This is the main entry point of your console application
        static void Main(string[] args)
        {
            // Call the function that connects to Google Drive and retrieves file data
            GetGoogleDrive();
        }
    }
}

Step 5: Run and validate the output

  1. Press F5 or run the app from the terminal.
  2. You’ll see a structured list of files retrieved from your Google Drive.

Conclusion

This tutorial demonstrated how to use the ZappySys ODBC API Driver to connect your C# application with Google Drive, enabling SQL-style queries over cloud storage. The reusable query executor makes it easy to connect with other APIs too—just change the DSN and query.

Visit our official page to discover more connectors, powerful automation features, and real-time data integration options. Start building smarter, faster, and more scalable solutions today with ZappySys ODBC PowerPack.

References

Contact us

If you encounter any issues or have specific questions, reach out to our support team via live chat or support ticket using our email support@zappysys.com.