当前位置:   article > 正文

.NET C# 操作Neo4j图数据库_neo4j c#

neo4j c#

.NET C# 操作Neo4j图数据库

环境

VisualStudio2022 + .NET 6 + Neo4j.Driver 5.21

在这里插入图片描述

Code

// 连接设置
var uri = "bolt://localhost:7687";
var user = "neo4j";
var password = "password"; // 请替换为你的实际密码

var driver = GraphDatabase.Driver(uri, AuthTokens.Basic(user, password));
var session = driver.AsyncSession();

try
{
    // 创建节点
    var createResult = await session.RunAsync("CREATE (n:Person {name: $name, age: $age}) RETURN n", 
                                              new { name = "Alice", age = 30 });

    var createdNode = await createResult.SingleAsync();
    Console.WriteLine($"Created Node: {createdNode["n"].As<INode>().Properties["name"]}");

    // 查询节点
    var readResult = await session.RunAsync("MATCH (n:Person) WHERE n.name = $name RETURN n", 
                                            new { name = "Alice" });

    var readNode = await readResult.SingleAsync();
    Console.WriteLine($"Read Node: {readNode["n"].As<INode>().Properties["name"]}");

    // 更新节点
    var updateResult = await session.RunAsync("MATCH (n:Person) WHERE n.name = $name SET n.age = $age RETURN n", 
                                              new { name = "Alice", age = 35 });

    var updatedNode = await updateResult.SingleAsync();
    Console.WriteLine($"Updated Node Age: {updatedNode["n"].As<INode>().Properties["age"]}");

    // 删除节点
    var deleteResult = await session.RunAsync("MATCH (n:Person) WHERE n.name = $name DELETE n", 
                                              new { name = "Alice" });

    Console.WriteLine("Deleted Node: Alice");

    // 创建节点 Alice
    var createAlice = await session.RunAsync("CREATE (a:Person {name: $name, age: $age}) RETURN a",
                                             new { name = "Alice", age = 30 });

    var aliceNode = await createAlice.SingleAsync();
    Console.WriteLine($"Created Node: {aliceNode["a"].As<INode>().Properties["name"]}");

    // 创建节点 Bob
    var createBob = await session.RunAsync("CREATE (b:Person {name: $name, age: $age}) RETURN b",
                                           new { name = "Bob", age = 25 });

    var bobNode = await createBob.SingleAsync();
    Console.WriteLine($"Created Node: {bobNode["b"].As<INode>().Properties["name"]}");

    // 创建关系 KNOWS
    var createRelationship = await session.RunAsync(@"
                    MATCH (a:Person {name: $nameA}), (b:Person {name: $nameB})
                    CREATE (a)-[r:KNOWS]->(b)
                    RETURN r",
                                                    new { nameA = "Alice", nameB = "Bob" });

    var relationship = await createRelationship.SingleAsync();
    Console.WriteLine($"Created Relationship: {relationship["r"].As<IRelationship>().Type}");
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}
finally
{
    await session.CloseAsync();
    await driver.CloseAsync();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/1008653
推荐阅读
相关标签
  

闽ICP备14008679号