Skip to content

ali2kan/supabase-csharp-integration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Supabase C# Integration

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.

📚 Table of Contents

🔍 Overview

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.

🛠 Prerequisites

Before you begin, ensure you have the following installed:

📥 Installation

Option 1: Use this repository as a template

  1. Clone the repository:

    git clone https://github.com/ali2kan/supabase-csharp-integration.git
    cd supabase-csharp-integration
  2. Restore the project dependencies:

    dotnet restore

Option 2: Integrate into an existing project

  1. Navigate to your existing project directory.

  2. Install required NuGet packages:

    dotnet add package Supabase
    dotnet add package DotNetEnv

Option 3: Start a new project

  1. Create a new console application:

    dotnet new console -n SupabaseCSharpIntegration
    cd SupabaseCSharpIntegration
  2. Install required NuGet packages:

    dotnet add package Supabase
    dotnet add package DotNetEnv

⚙️ Setup

  1. Create a .env file in the project root with your Supabase credentials:

    SUPABASE_URL=your_supabase_url
    SUPABASE_KEY=your_supabase_key
    
  2. 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.

  3. 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.

  4. Build the project to generate the obj and bin directories:

    dotnet build

🚀 Usage

To run the application:

dotnet run

This will execute the sample queries and display the results in the console.

🔗 Integration with Existing Projects

To integrate Supabase into an existing C# project:

  1. Install the required NuGet packages (Supabase, DotNetEnv).

  2. Add the model classes for your Supabase tables (see Program.cs for examples).

  3. Initialize the Supabase client:

    var options = new SupabaseOptions
    {
        AutoRefreshToken = true,
        AutoConnectRealtime = true
    };
    var client = new Supabase.Client(supabaseUrl, supabaseKey, options);
    await client.InitializeAsync();
  4. Use the client to query your data:

    var response = await client
        .From<YourModel>()
        .Select()
        .Get();

🖥 WinForms Integration Example

To integrate Supabase querying into a WinForms application:

  1. Create a new WinForms project in Visual Studio.

  2. Install the Supabase NuGet package.

  3. 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();
    }
  4. Call this method in your form's constructor or load event:

    public Form1()
    {
        InitializeComponent();
        InitializeSupabaseClient().Wait();
    }
  5. 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;
    }
  6. Call these methods from button click events or other appropriate UI triggers.

📊 Data Export

To export data to CSV:

  1. Install the CsvHelper NuGet package:

    dotnet add package CsvHelper
  2. 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.

🌟 Best Practices

  • 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.

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

About

integrate Supabase with Csharp applications

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages