Neo4j .NET客户端执行字符串Cypher查询

4

是否可以使用Neo4j .NET客户端或任何其他模块将CYPHER查询作为纯字符串执行?

例如,如果我想向我的图形数据库添加一些节点并已经组装好了语句,是否有一种执行字符串的方法:

CREATE (n:Edit {name:"L-1154LX"});

我想批量处理一系列已经创建的CREATE CYPHER查询。

2个回答

5

官方文档位于https://github.com/Readify/Neo4jClient/wiki/cypher#manual-queries-highly-discouraged

然而,这样做会影响性能,并且存在安全风险。

影响性能是因为需要重新解析每个查询。您应该使用参数,例如在https://github.com/Readify/Neo4jClient/wiki/cypher-examples#create-a-user的示例中。这样,查询文本保持一致,只有参数不同,因此您不必在每次调用时承担查询编译成本。

存在安全风险,因为很容易出现编码错误,从而暴露自己的注入风险。

所以,请不要运行手动查询,除非您真正了解自己在做什么。它们是有意隐藏的。


谢谢!我在代码实现上遇到了麻烦。我假设"query"是原始的Cypher查询字符串,但这只是标准Client对象的一种方法吗?我正在使用.NET客户端。 - smk081
我非常非常警告你不要这样做。有更好、更安全、更快的方法来实现你想要的功能。我提供的文档中给出了三行代码,你可以直接复制粘贴。请注意,这些方法是显式接口实现,因此需要先将其转换为 IRawGraphClient。但是,我仍然强烈反对你这样做。真的。非常非常。 - Tatham Oddie
请按照以下代码操作:https://github.com/Readify/Neo4jClient/wiki/cypher-examples#create-a-user - Tatham Oddie

1
我正在编写一个.NET应用程序,使得可以从Excel电子表格中提取节点和关系,以便可以动态生成并加载到neo4j(请参见下面的翻译/管理对象模型)。
当我通过neo4jclient将它们加载到neo4j时,我将不知道我的节点在运行时长什么样子;它们可以有任意数量的属性,这些属性可能具有任何名称和值。在https://github.com/Readify/Neo4jClient/wiki/cypher-examples的示例中,看起来我应该有本地类来引用属性名称和值;但这样做行不通。我是否错过了什么技巧?参数化查询?(即使这些示例也期望有本地的、硬编码的类)。
 public class Node
        {
            public string NodeType = "";
            public List<Attribute> Attributes;
            public List<Relationship> ParentRelationships;
            public List<Relationship> ChildRelationships;
            public Node(string nodeType)
            {
                NodeType = nodeType;
                Attributes = new List<Attribute>();
                ParentRelationships = new List<Relationship>();
                ChildRelationships = new List<Relationship>();
            }

            public void AddAttribute(Attribute attribute)
            {
                //We are not allowing empty attributes
                if(attribute.GetValue() != "")
                    this.Attributes.Add(attribute);
            }

            public string GetIdentifier()
            {
                foreach (Attribute a in Attributes)
                {
                    if (a.IsIdentifier)
                        return a.GetValue();
                }
                return null;
            }

            public void AddParentRelationship(Relationship pr)
            {
                ParentRelationships.Add(pr);
            }

            public void AddChildRelationship(Relationship cr)
            {
                ChildRelationships.Add(cr);
            }

  public class Attribute
        {
            private string Name;
            private string Value;
            public bool IsIdentifier;

            public Attribute(string name, string value, bool isIdentifier)
            {
                SetName(name);
                SetValue(value);
                IsIdentifier = isIdentifier;
            }

            public void SetName(string name)
            {
                Name = name.Trim();
            }
            public void SetValue(string value)
            {
                Value = value.Trim();
            }
            public string GetName()
            {
                return Name;
            }
            public string GetValue()
            {
                return Value;
            }

        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接