About the Driver

In the Cypher Fundamentals course, we cover how to query Neo4j using a language called Cypher. To execute a Cypher statement against a Neo4j database you will use an object called a Driver.

The Driver object is a thread-safe, application-wide fixture from which all Neo4j interaction derives.

The Driver API is topology independent, so you can run the same code against a Neo4j cluster or a single DBMS.

To connect to and query Neo4j from within a .NET application, you use the Neo4j .NET Driver.

The Neo4j .NET Driver is one of five officially supported drivers, the others are Java, JavaScript, Python, and Go. There are also a wide range of Community Drivers available for other languages including PHP and Ruby.

You should create a single instance of the Driver in your application per Neo4j cluster or DBMS, which can then be shared across your application.

Installing the Driver

The Neo4j .NET Driver is distributed via the NuGet Gallery. You can find the latest version of the driver by visiting https://www.nuget.org/packages/Neo4j.Driver/.

To install the latest version of the driver using NuGet in Visual Studio, you can run the following command

c#
Installing through Visual Studio
PM> Install-Package Neo4j.Driver

Alternatively, you can run the dotnet add command to add the dependency.

sh
Using the dotnet add Command
dotnet add Neoflix package Neo4j.Driver

This will add a new entry to the .csproj file for your project

xml
The newly added package reference
<PackageReference Include="Neo4j.Driver" Version="5.1.0" />

Creating a Driver Instance

Each driver instance will connect to one DBMS, or Neo4j cluster, depending on the value provided in the connection string.

You will then use the using keyword to include the Neo4j.Driver namespace into your C# classes.

The static GraphDatabase class provides a Driver method for creating a new driver implementation. The Driver() method requires two arguments:

  1. A connection string for the Neo4j cluster or DBMS - for example neo4j://localhost:7687 or neo4j+s://dbhash.databases.neo4j.io:7687

  2. An authentication token - Neo4j supports basic username and password authentication, kerberos tokens or custom authentication. You can create an authentication token by calling one of the static methods provided by AuthTokens.

Here is an example for how to create a driver instance:

c#
Creating a Driver Instance
// Import all relevant classes from neo4j-dotnet-driver
using Neo4j.Driver;
var username = "neo4j";
var password = "letmein!";
var driver = GraphDatabase.Driver("bolt://localhost:7687",
    AuthTokens.Basic(username, password));

The above example creates an unencrypted connection to the Neo4j server at localhost on the default port number of 7687. The driver then attemps to authenticate against the server using a basic authentication with the username neo4j and password letmein!.

Verifying Connectivity

You can verify that the connection details used during driver instantiation are correct by calling the verifyConnectivity() function. This function returns the Driver instance if the connection details are correct, or fails with a Neo.ClientError.Security.Unauthorized Exception if a connection could not be made.

c#
Verify Connectivity
// Verify the connection details
await driver.VerifyConnectivityAsync();

Console.WriteLine("Connection Verified!");

Check Your Understanding

1. Which of the following programming languages have officially supported drivers?

  • ✓ .NET

  • ✓ Go

  • ✓ Java

  • ✓ JavaScript

  • ✓ Python

  • ❏ PHP

  • ❏ Ruby

Hint

Five languages are officially supported by Neo4j.

Solution

The five supported languages are .NET, Go, Java, JavaScript and Python.

  • GraphDatabases.Neo4j

  • Neo4j

  • Neo4j.Driver

  • Neo4j.GraphDatabase

Hint

The package is registered under Neo4j.Driver.

Solution

The package is registered under Neo4j.Driver.

Lesson Summary

In this lesson, you learned about the Neo4j .NET Driver and how it can be used to connect to Neo4j from within a Java application.

In the next lesson, we will take a closer look at the first argument in the GraphDatabase.Driver() method, the connection string.