WebSql 0.0.2

dotnet add package WebSql --version 0.0.2
                    
NuGet\Install-Package WebSql -Version 0.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WebSql" Version="0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WebSql" Version="0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="WebSql" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WebSql --version 0.0.2
                    
#r "nuget: WebSql, 0.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package WebSql@0.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=WebSql&version=0.0.2
                    
Install as a Cake Addin
#tool nuget:?package=WebSql&version=0.0.2
                    
Install as a Cake Tool

WebSql

The in-browser SQL engine powered by excellent <img src="https://user-images.githubusercontent.com/552629/76405509-87025300-6388-11ea-86c9-af882abb00bd.png" width="20px" alt="sql.js" /> sql.js

GitHub

Installation

dotnet add package WebSql --version 0.0.1

Usage

Creaton of new database.

var database = await WebSqlInterop.CreateDatabase();

Then you can execute SQL inside. Create schema, populate data

string CreateDatabaseSql = @"
CREATE TABLE IF NOT EXISTS WeatherForecast (
	Id INTEGER PRIMARY KEY AUTOINCREMENT, 
	Date TEXT, 
	TemperatureC INTEGER, 
	Summary TEXT
);

INSERT INTO WeatherForecast(Date, TemperatureC, Summary)
VALUES ('2022-01-06', 1, 'Feezing'),
    ('2022-01-07', 14, 'Bracing'),
    ('2022-01-08', -13, 'Feezing'),
    ('2022-01-09', -16, 'Balmy'),
    ('2022-01-10', -2, 'Chilly'),
    ('2022-01-11', -3, 'Chilly');
";

await database.ExecuteNonQueryAsync(CreateDatabaseSql);

If you want to get results out of the database, you can use ExecuteQueryAsync method.

WebSqlResultset[] result = await database.ExecuteAsync("SELECT * FROM WeatherForecast");
var firstResultset = result[0];
foreach (var row in result.Values)
{
    // Access the values in the row using column indexes
    Console.WriteLine($"Id: {row[0]}, Date: {row[1]}, TemperatureC: {row[2]}, Summary: {row[3]}");
}

Data in the row is stored as type System.Text.Json.JsonElement, so you should perform conversion yourself for now. No marshalling done on my side.

Using parameters

You can use dictionaries to specify parameters. Sqlite supports ?, ?NNN, :VVV, @VVV and $VVV as parameter names, where NNN is a number and VVV a string. You can use any of them in your SQL statement, but you have to use the same name in the dictionary.

string ParametersSampleSql = @"
DROP TABLE IF EXISTS test;

CREATE TABLE test (id INTEGER, age INTEGER, name TEXT);
INSERT INTO test VALUES ($id1, :age1, @name1);
INSERT INTO test VALUES ($id2, :age2, @name2);

SELECT id FROM test;
SELECT age,name FROM test WHERE id=$id1";

result = await database.ExecuteAsync(ParametersSampleSql, new Dictionary<string, object>()
        {
            {"$id1", 1 },
            { ":age1", 10 },
            { "@name1", "Ling" },
            { "$id2", 2 },
            { ":age2", 18 },
            { "@name2", "Paul"  }
        });

Using cursor

If you don't want load whole dataset into memory, you can use PrepareAsync method. It will create prepared WebSqlStatement class which can be used to bind parameters and step through the result set.

string AnonymousParameterPrepareSql = "SELECT * FROM hello WHERE a=? AND b=?";

await using WebSqlStatement anonymousParameterStatement = await database.PrepareAsync(AnonymousParameterPrepareSql);
await anonymousParameterStatement.BindAsync(0, "hello");
while (await anonymousParameterStatement.StepAsync())
{
    var rowResult = await anonymousParameterStatement.GetAsync();
    // Do something with results
}

Remote database

You can load database from remote location. Use CreateDatabaseFromFile method and specify URL.

WebSqlDatabase database = await WebSqlInterop.CreateDatabaseFromFile("Chinook_Sqlite.sqlite");
WebSqlResultset[] result = await database.ExecuteAsync("SELECT * FROM Artist LIMIT 10;");
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.2 184 4/15/2025
0.0.1 171 4/15/2025