基于Linux系统的Neo4j 4.1社区版本的 .Net 如何调用 Result API(三)

  上一篇文章我们主要对Neo4j的CQL语法做了简单介绍,这一章主要是讲解Neo4j如何在.Net中进行调用。

※ 前言
  一般有两种方法,一种是使用官方提供的Neo4j.Driver.dll,另外一种就是调用Result API。本文就是利用API进行调用,后续.Net Core调用才能无缝链接。

※ Neo4j帮助类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

/*
 * 封装Neo4j api 帮助类
 */
namespace WebNeo4j.Helper
{
     
    public class Neo4jApiHelper
    {
     
        #region 属性

        private const string url = "http://localhost:7474/db/data/transaction/commit";
        private const string userName = "neo4j";
        private const string pwd = "neo4j"; 
        #endregion

        #region 方法
        /// 
        /// 
        /// 
        /// cypher语法
        /// 
        public string OperationByCypher(string cypherStr)
        {
     
            try
            {
     
                StringBuilder RequestContent = new StringBuilder();
                RequestContent.AppendLine("{");
                RequestContent.AppendLine("\"statements\": [");
                RequestContent.AppendLine("{");
                RequestContent.AppendLine("\"statement\": \"" + cypherStr + "\",");
                RequestContent.AppendLine("\"parameters\": null,");
                RequestContent.AppendLine("\"resultDataContents\": [");
                RequestContent.AppendLine("  \"graph\"");
                RequestContent.AppendLine("],");
                RequestContent.AppendLine(" \"includeStats\": true");
                RequestContent.AppendLine("}");
                RequestContent.AppendLine("]");
                RequestContent.AppendLine(" }");
                string param = RequestContent.ToString();
                byte[] bs = Encoding.UTF8.GetBytes(param);
                System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
                req.Method = "POST";
                req.Accept = "application/json; charset=UTF-8";
                req.ContentType = "application/json";
                req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(userName + ":" + pwd));
                req.KeepAlive = true;
                System.IO.Stream reqStream = req.GetRequestStream();

                reqStream.Write(bs, 0, bs.Length);
                string responseData = "";
                using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)req.GetResponse())
                {
     
                    using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
     
                        responseData = reader.ReadToEnd().ToString();
                        LogHelper.Write("成功:" + responseData);
                        return responseData;
                    }

                }
            }
            catch (Exception ee)
            {
     
                LogHelper.Write("失败:" + ee.Message);
                return "失败:" + ee.Message;
            }
        } 
        #endregion

    }
}

※ 调用方法


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebNeo4j.Helper;

namespace WebNeo4j.Controllers
{
     
    public class Neo4jController : Controller
    {
     
        public ActionResult Index()
        {
     
            Neo4jApiHelper n = new Neo4jApiHelper();

            //创建节点
            //string create = n.OperationByCypher("CREATE (a:Person {name: '纪王'})");

            //创建节点和关系
            // string creates = n.OperationByCypher("CREATE(p:Country{name:'大梁',cjsj:'20200401'})-[r:仇敌]->(p1:Country{name:'大渝'})");

            //删除节点
            //string delete = n.OperationByCypher("match (n:Person{name:'纪王'}) detach delete n");

            //修改属性
            //string update = n.OperationByCypher("MATCH (n:Person {name:'林溪'})-[r:父子]->(m:Person {name:'萧平旌'}) CREATE (n)-[r2:夫妻]->(m) SET r2 = r WITH r DELETE r");

            //查询
            string select = n.OperationByCypher("Match (n:Person)  return  n ");
            
            



            return View();
        }



    }
}

※后话

  之前搜遍整个百度,发现没有人封装基于.Net调用的Neo4j帮助类,遂将项目的部分功能剔除,提供一个简单的帮助类,供各位小伙伴们参考。

你可能感兴趣的:(Neo4j,neo4j,.net,大数据)