This repository demonstrates how to integrate Supabase with C# applications, focusing on querying data from multiple schemas and tables. It serves as a template for connecting to Supabase backends and performing database operations in C# environments.
- Overview
- Prerequisites
- Installation
- Setup
- Usage
- Integration with Existing Projects
- WinForms Integration Example
- Data Export
- Best Practices
- Contributing
- License
This project demonstrates how to:
- Connect to a Supabase backend using C#
- Query data from multiple tables (using ACLED, CIA Factbook, and World Bank data as examples)
- Structure a C# application for Supabase integration
- Export data to CSV or Excel formats
While the example uses specific datasets, the techniques can be applied to any Supabase project.
Before you begin, ensure you have the following installed:
- .NET SDK (version 6.0 or later)
- Visual Studio or Visual Studio Code
- Git (optional, for cloning the repository)
-
Clone the repository:
git clone https://github.com/ali2kan/supabase-csharp-integration.git cd supabase-csharp-integration
-
Restore the project dependencies:
dotnet restore
-
Navigate to your existing project directory.
-
Install required NuGet packages:
dotnet add package Supabase dotnet add package DotNetEnv
-
Create a new console application:
dotnet new console -n SupabaseCSharpIntegration cd SupabaseCSharpIntegration
-
Install required NuGet packages:
dotnet add package Supabase dotnet add package DotNetEnv
-
Create a
.env
file in the project root with your Supabase credentials:SUPABASE_URL=your_supabase_url SUPABASE_KEY=your_supabase_key
-
If you're using this repository as a template or starting a new project, update the
Program.cs
file with your specific table names and query requirements. -
If you're integrating into an existing project, copy the relevant code from the
Program.cs
in this repository and adapt it to your project structure. -
Build the project to generate the
obj
andbin
directories:dotnet build
To run the application:
dotnet run
This will execute the sample queries and display the results in the console.
To integrate Supabase into an existing C# project:
-
Install the required NuGet packages (Supabase, DotNetEnv).
-
Add the model classes for your Supabase tables (see
Program.cs
for examples). -
Initialize the Supabase client:
var options = new SupabaseOptions { AutoRefreshToken = true, AutoConnectRealtime = true }; var client = new Supabase.Client(supabaseUrl, supabaseKey, options); await client.InitializeAsync();
-
Use the client to query your data:
var response = await client .From<YourModel>() .Select() .Get();
To integrate Supabase querying into a WinForms application:
-
Create a new WinForms project in Visual Studio.
-
Install the Supabase NuGet package.
-
Add a method to initialize the Supabase client:
private Supabase.Client _supabaseClient; private async Task InitializeSupabaseClient() { string supabaseUrl = ConfigurationManager.AppSettings["SupabaseUrl"]; string supabaseKey = ConfigurationManager.AppSettings["SupabaseKey"]; var options = new SupabaseOptions { AutoRefreshToken = true, AutoConnectRealtime = true }; _supabaseClient = new Supabase.Client(supabaseUrl, supabaseKey, options); await _supabaseClient.InitializeAsync(); }
-
Call this method in your form's constructor or load event:
public Form1() { InitializeComponent(); InitializeSupabaseClient().Wait(); }
-
Create methods to query data and update UI controls:
private async Task LoadData() { var response = await _supabaseClient .From<YourModel>() .Select() .Get(); dataGridView1.DataSource = response.Models; }
-
Call these methods from button click events or other appropriate UI triggers.
To export data to CSV:
-
Install the CsvHelper NuGet package:
dotnet add package CsvHelper
-
Use the following code to export data:
using (var writer = new StreamWriter("output.csv")) using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) { csv.WriteRecords(response.Models); }
For Excel export, consider using libraries like EPPlus or ClosedXML.
- Keep sensitive information (like API keys) in environment variables or secure configuration files.
- Use asynchronous methods consistently for better performance.
- Implement proper error handling and logging.
- Use strongly-typed models to represent your database tables.
- Regularly update and maintain your Supabase client and other dependencies.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.