-
Notifications
You must be signed in to change notification settings - Fork 147
GettingStartedWithCSharp
This is a quick start guide that'll show you how to connect to Neo4j, it assumes you have Neo4j running either locally or on a URL you have access to.
- Create (or open) a project that you want to add Neo4jClient to.
- In the Package Manager run:
Install-Package Neo4jClient
NB. If you've downloaded a release and built it, add a reference
as you normally would.
From here on in, we're going with the assumption the database is at neo4j://localhost:7687/, obviously replace this with your own URL if you've setup the DB elsewhere.
All the operations made against the DB are made from the BoltGraphClient
class, so we need to first instantiate one of those:
var client = new BoltGraphClient("neo4j://localhost:7687");
You may want to pass authentication information along as well, this can be done via the overload:
var client = new BoltGraphClient("neo4j://localhost:7687", "username", "password");
Once the client is instantiated, you need to Connect
to the database:
await client.ConnectAsync();
At this point your client
is connected to the database and you're ready to start querying.
If you get an error (in the form of an Exception
) from the ConnectAsync
method, there are a few things to check:
- Is the Neo4j server definitely running?
- Open up the URL (http://localhost:7474/) and see if you can see the management page, if not - start Neo4j and try again.
All the queries are based around the Cypher language, and it is recommended that you get comfortable with Cypher via the Neo4j documentation.
We'll use the 'Movies' demo application, so go to your server (http://localhost:7474) and type :play movies
into the $
text box. Then click on the code and run it to insert the data.
Once the database is set up we can start to code against it.
Create a new C# console application and add this class to it:
public class Movie
{
[JsonProperty(PropertyName = "title")]
public string Title { get; set; }
[JsonProperty(PropertyName = "released")]
public int Released { get; set; }
[JsonProperty(PropertyName = "tagline")]
public string TagLine { get; set; }
}
The JsonProperty
attributes are there to allow Json.NET
to translate between the Java style of property naming that the Movies
data uses and the .NET style for the class. We could have just done something like:
public class Movie
{
public string title { get; set; }
/*etc*/
}
But the various C# style guides and helpers StyleCop/Resharper etc will tell you that's not on.
Now we have our class, let's get write out query:
var movies = client.Cypher
.Match("(m:Movie)")
.Return(m => m.As<Movie>())
.Limit(10)
.Results;
At this point, we have the query, but until we start to process the results
nothing has actually been sent to the server. So let's write out some movies:
foreach (var movie in movies)
Console.WriteLine("{0} ({1}) - {2}", movie.Title, movie.Released, movie.TagLine);
When you run the application you'll get a list of 10 of the movies from the database.
You have the client set up and have a query working - so now would be a good time to head over to the Cypher and Cypher Examples pages and look at the other things you can do.