如何用C#写代码,向数据库中添加新数据

How to write code in C #, to add new data to the database

向 GraphDB 添加三元组

SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://localhost:7200/sparql"), "http://localhost:7200/");
SparqlResultSet results = endpoint.QueryWithResultSet("PREFIX : <http://www.example.org/> INSERT DATA {:test :test :hhrh }");

为什么不起作用?

StardogConnector stardog = new StardogConnector("http://localhost:7200", "test", "admin", "posw");
stardog.Begin();
string query = "PREFIX : <http://www.example.org/>SELECT * WHERE {:" + line[0] + " ?k :" + line[1] + "}";
stardog.Query(query);
stardog.Commit();

另一种方式,同样的问题。在 lokalka

上创建了一个数据库

是的,我也得出了这个结论,我是第一次使用GraphDB。那么,我怎样才能用一个文件来实现它呢?我写了这样的代码。

IGraph g = new Graph();

string sql = "PREFIX : <http://www.example.org/> INSERT DATA {:test :test :hhrh }";
g.LoadFromFile("t.n3"); 

Object results = g.ExecuteQuery(sql);

出现这样的错误

VDS.RDF.Parsing.RdfParseException
HResult = 0x80131500
Message = [InsertKeywordToken at Line 1 Column 36 to Line 1 Column 42] Unexpected Token encountered - expected a BASE / PREFIX directive or a Query Keyword to start a Query
Source = dotNetRDF
Stack trace:
in VDS.RDF.Parsing.SparqlQueryParser.ParseInternal (SparqlQueryParserContext context)
in VDS.RDF.Parsing.SparqlQueryParser.ParseInternal (TextReader input)
in VDS.RDF.Parsing.SparqlQueryParser.ParseFromString (String queryString)
in VDS.RDF.GraphExtensions.ExecuteQuery (IGraph g, String sparqlQuery)
in algorAutoText.Program.Main (String [] args) in C: \ Users \ Denis \ source \ repos \ algorAutoText \ algorAutoText \ Program.cs: line 43

误判,我应该没有加BASE / PREFIX。但是他在请求中

更新和删除查询来自 /statements 端点,

/repositories/{repository_id}/statements

您可以在此处查看 RDF4J 服务器 REST API:

http://docs.rdf4j.org/rest-api/#_the_rdf4j_server_rest_api

当您使用 DELETE 或 INSERT 关键字时,您正在执行 SPARQL 更新,而不是查询。 SPARQL 将查询和更新分为两个单独的规范,大多数三元组存储将它们实现为两个单独的端点(例如出于安全原因)。

要从 dotNetRDF 更新到三元组存储,您有两个选择。

  1. 您可以直接使用 SPARQL 更新端点,在这种情况下,您需要查看三元组存储的文档以了解如何为此创建 URL - 请参阅 https://github.com/dotnetrdf/dotnetrdf/wiki/UserGuide-Updating-With-SPARQL#remote-updates 了解详情。

  2. 或者,如果您的三重存储是 dotNetRDF 支持的存储之一(Stardog 和 Sesame/GraphDB 都是),那么有方便的包装器可以使这更容易一些 - 了解更多信息关于这个请参考https://github.com/dotnetrdf/dotnetrdf/wiki/UserGuide-Triple-Store-Integration#update