.NET
Installation
-
Use the following command to create a new project:
dotnet new console -o Neo4jAuraExample
-
Navigate into the newly created directory:
cd Neo4jAuraExample
-
Install the .NET Driver using Package Manager:
dotnet add package Neo4j.Driver
Code example
using Neo4j.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
public class DriverIntroductionExample : IDisposable
{
private bool _disposed = false;
private readonly IDriver _driver;
~DriverIntroductionExample() => Dispose(false);
public DriverIntroductionExample(string uri, string user, string password)
{
_driver = GraphDatabase.Driver(uri, AuthTokens.Basic(user, password));
}
public async Task CreateFriendship(string person1Name, string person2Name)
{
// To learn more about the Cypher syntax, see https://neo4j.com/docs/cypher-manual/current/
// The Reference Card is also a good resource for keywords https://neo4j.com/docs/cypher-refcard/current/
var query = @"
MERGE (p1:Person { name: $person1Name })
MERGE (p2:Person { name: $person2Name })
MERGE (p1)-[:KNOWS]->(p2)
RETURN p1, p2";
await using var session = _driver.AsyncSession(configBuilder => configBuilder.WithDatabase("neo4j"));
try
{
// Write transactions allow the driver to handle retries and transient error
var writeResults = await session.ExecuteWriteAsync(async tx =>
{
var result = await tx.RunAsync(query, new { person1Name, person2Name });
return await result.ToListAsync();
});
foreach (var result in writeResults)
{
var person1 = result["p1"].As<INode>().Properties["name"];
var person2 = result["p2"].As<INode>().Properties["name"];
Console.WriteLine($"Created friendship between: {person1}, {person2}");
}
}
// Capture any errors along with the query and data for traceability
catch (Neo4jException ex)
{
Console.WriteLine($"{query} - {ex}");
throw;
}
}
public async Task FindPerson(string personName)
{
var query = @"
MATCH (p:Person)
WHERE p.name = $name
RETURN p.name";
await using var session = _driver.AsyncSession(configBuilder => configBuilder.WithDatabase("neo4j"));
try
{
var readResults = await session.ExecuteReadAsync(async tx =>
{
var result = await tx.RunAsync(query, new { name = personName });
return await result.ToListAsync();
});
foreach (var result in readResults)
{
Console.WriteLine($"Found person: {result["p.name"].As<String>()}");
}
}
// Capture any errors along with the query and data for traceability
catch (Neo4jException ex)
{
Console.WriteLine($"{query} - {ex}");
throw;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
_driver?.Dispose();
}
_disposed = true;
}
public static async Task Main(string[] args)
{
// Aura queries use an encrypted connection using the "neo4j+s" protocol
var uri = "neo4j+s://<Bolt url for Neo4j Aura instance>";
var user = "<Username for Neo4j Aura instance>";
var password = "<Password for Neo4j Aura instance>";
using var example = new DriverIntroductionExample(uri, user, password);
await example.CreateFriendship("Alice", "David");
await example.FindPerson("Alice");
}
}
Running the example
Follow the steps below to run the example code:
-
Copy and paste the code above into a file named
Program.cs
. -
Enter the information for the AuraDB instance you want to connect to by replacing:
-
<Bolt url for Neo4j Aura instance>
with the URI. -
<Username for Neo4j Aura instance>
with the username. -
<Password for Neo4j Aura instance>
with the password.
-
-
Use the following command to generate an executable:
dotnet build
-
Use the following command to run the example code:
dotnet run
Example walkthrough
The example imports Neo4j.Driver
to connect to the Neo4j AuraDB instance.
The Main
function calls the following two functions:
-
CreateFriendship
creates two 'Person' nodes, Alice and David, and a 'KNOWS' relationship between them using a write transaction. -
FindPerson
finds Alice using a read transaction.
Developing with Neo4j Aura requires the use of Transaction Functions. Transaction Functions enable automatic recovery from transient network errors and enable load balancing. |
Make sure to log queries and data sent from your application as it is useful when you encounter errors and can help with debugging. This example catches a Neo4jException
.
Was this page helpful?