.NET C# 操作Neo4j图数据库

.NET C# 操作Neo4j图数据库

目录

  • [.NET C# 操作Neo4j图数据库](# 操作Neo4j图数据库)

环境

VisualStudio2022 + .NET 6 + Neo4j.Driver 5.21

Code

csharp 复制代码
// 连接设置
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();
}
相关推荐
写bug的小屁孩2 分钟前
前后端交互接口(三)
运维·服务器·数据库·windows·用户界面·qt6.3
恒辉信达15 分钟前
hhdb数据库介绍(8-4)
服务器·数据库·mysql
Envyᥫᩣ1 小时前
C#语言:从入门到精通
开发语言·c#
齐 飞1 小时前
MongoDB笔记01-概念与安装
前端·数据库·笔记·后端·mongodb
云空1 小时前
《Python 与 SQLite:强大的数据库组合》
数据库·python·sqlite
暮毅1 小时前
10.Node.js连接MongoDb
数据库·mongodb·node.js
wowocpp1 小时前
ubuntu 22.04 server 格式化 磁盘 为 ext4 并 自动挂载 LTS
服务器·数据库·ubuntu
成富2 小时前
文本转SQL(Text-to-SQL),场景介绍与 Spring AI 实现
数据库·人工智能·sql·spring·oracle
songqq272 小时前
SQL题:使用hive查询各类型专利top 10申请人,以及对应的专利申请数
数据库·sql
计算机学长felix2 小时前
基于SpringBoot的“校园交友网站”的设计与实现(源码+数据库+文档+PPT)
数据库·spring boot·毕业设计·交友