Link Search Menu Expand Document
Start for Free

Accessing Stardog from .NET

Learn how to access Stardog data using dotNetRDF.

Page Contents
  1. Overview
  2. Setup
  3. Connecting to Stardog from .NET
  4. Adding Data to a Database
  5. Conclusion

Overview

In this tutorial, we will explore how to connect to and query Stardog from a C# program.

Setup

For this tutorial, we will be using C# and .NET version 7.0. We’ll create our project with the following command:

dotnet new console --framework net7.0 --no-restore

We will use the dotNetRDF library to connect to Stardog, which we will install by running the following command in our project directory:

dotnet add package dotNetRDF

Instructions for installing Stardog in your environment can be found in the Install Stardog section of the documentation. We will be using some sample data for this project, which can be found here on our GitHub.

Connecting to Stardog from .NET

To begin, we will define some constants that we will use to connect to our server. These are based around Stardog’s default settings for a local installation. Add the following to your Program.cs file:

const string SERVER_URL = "http://localhost:5820";
const string STARDOG_USERNAME = "admin";
const string STARDOG_PASSWORD = "admin";
const string DATABASE_NAME = "MyDB";

Make sure to change these as needed for your use case.

We can now use these values to establish a connection to our Stardog server, which we will use to create the database for this project. Note that in this code we check to see if a database with our chosen name already exists, and delete it if so. You most likely will not want to do this in your actual use case.

// using will automatically call Dispose() for us
using (StardogServer stardog = new StardogServer(SERVER_URL, STARDOG_USERNAME, STARDOG_PASSWORD))
{
    // if the database already exists, delete it
    if (stardog.ListStores().Contains(DATABASE_NAME))
    {
        stardog.DeleteStore(DATABASE_NAME);
    }

    IStoreTemplate template = stardog.GetDefaultTemplate(DATABASE_NAME);
    stardog.CreateStore(template);
}

Adding Data to a Database

First, we will connect to the database on our Stardog server.

using (StardogConnector stardogConn = new StardogConnector(SERVER_URL, DATABASE_NAME, STARDOG_USERNAME, STARDOG_PASSWORD))
{

}

We will now load our sample data from the file linked earlier in this tutorial. In our case, we have named the file beatles.ttl and stored it in the resources directory within our project. Be sure to change the file path accordingly based on your file’s name and location. Within the above using block, we can add the data.

Graph g = new Graph();
g.LoadFromFile("resources/beatles.ttl");

// write it to the store
if (!stardogConn.IsReadOnly)
{
    stardogConn.SaveGraph(g);
}

Also within the using block, we can query the data we just added.

// Run a SPARQL query
object results = stardogConn.Query("SELECT * WHERE { ?s ?p ?o } LIMIT 100");
if (results is SparqlResultSet)
{
    SparqlResultSet rset = (SparqlResultSet)results;
    foreach (SparqlResult r in rset)
    {
        Console.WriteLine("RESULT: {0}", r.ToString());
    }
}
else
{
    throw new Exception("Result was not a SPARQL result set");
}

We can also update our database using the update function. In the following example, we will delete any triples where the artist is John Lennon.

stardogConn.Update("DELETE WHERE { ?s :artist :John_Lennon }");

Conclusion

We have now learned how to connect a C# application to Stardog using dotNetRDF and used it to perform basic operations on Stardog. You can find the complete code sample for this tutorial and other examples in the stardog-examples GitHub Repository. The documentation for dotNetRDF can be found on their website.