I'm working with Neo4j Driver and trying to read and write from the DB locally.
Writing to the DB works with:
try
{
List<Person> people = CollectPeopleData();
_driver = GraphDatabase.Driver(uri, AuthTokens.Basic(userName, password));
using var session = _driver.AsyncSession();
foreach (Person person in people)
{
var data = session.ExecuteWriteAsync(tx =>
{
var result = tx.RunAsync(
"CREATE (a:Person) " +
"SET a.id = $person.Id " +
"SET a.name = $person.Name " +
"RETURN a.Id",
new { person });
return result;
});
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
The above code works. I can write to the DB.
If I try to do the same with session.ExecuteReadAsync() to read instead of write it doesn't work (the DB contains a bunch of nodes already).
I can't seem to process the Task<IResultCursor>, even if I convert it to a list of IRecords by doing data.Result.ToListAsync(), it seems to tell me that the records have already been consumed..? Can't really figure out the best or working minimal example to retrieve data from my DB.
Code attempted:
try
{
_driver = GraphDatabase.Driver(uri, AuthTokens.Basic(userName, password));
using var session = _driver.AsyncSession();
Task<IResultCursor> data = session.ExecuteReadAsync(tx =>
{
Task<IResultCursor> result = tx.RunAsync(
"MATCH (a) " +
"RETURN a"
);
return result;
}
data.Result.ForEachAsync(x => Console.WriteLine(x.Values.ToString()));
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
Perhaps someone can suggest a simple example to just get all data as objects with "MATCH (n) RETURN (n)"? i.e. I don't need to do any complex filtering, I just want the records and then do stuff with them later.
This is UNTESTED. Kindly try the code below and add the step to print out the result in the console. Thanks.