.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();
}
相关推荐
O***Z6167 分钟前
三分钟内快速完成MySQL到达梦数据库的迁移
数据库·mysql
友友马1 小时前
『QT』窗口 (一)
开发语言·数据库·qt
q***78371 小时前
SQL实现md5加密方法
数据库·sql
q***61411 小时前
Spring中Aware的用法以及实现
java·数据库·spring
红树林072 小时前
渗透测试之sql注入--报错注入
数据库·sql·安全·web安全
菜鸟小九2 小时前
mysql(锁)
数据库·mysql·oracle
c***42102 小时前
【Sql Server】随机查询一条表记录,并重重温回顾下自定义函数的封装和使用
数据库·性能优化
q***44813 小时前
PostgreSQL的备份方式
数据库·postgresql
v***59833 小时前
【SQL Server】超详细SQLServer日期转换、字符串、数学、聚合等常用函数大全(最新版)
数据库·sqlserver