C#
|
Connection options for C# / .NET ArcadeDB exposes multiple wire protocols, so you can pick the one your stack already speaks:
This page covers the HTTP/JSON approach. Per-protocol C# examples for BOLT and PostgreSQL are coming soon. |
Sample Code
In C#/.NET 7.0/8.0, HTTP/JSON requests can be made using the HTTPClient class inside an async function.
In real situations, the HTTPClient object should not be created or discarded on each request. HTTPClient should be typically created once in the lifespan of an application, stored in a Singleton Instance or static reference/class, and reused for each request. Here it is created in the function just for simplicity.
The following example demonstrates a simple function which will add a record of the type Profile to a database named mydb with the name Alexander.
public async void addProfileName(){
HttpClient httpClient = new(); //typically instantiate this only once over course of application, then reuse
HttpRequestMessage msg = new();
msg.Method = HttpMethod.Post;
string authString = "root:arcadedb-password"; //add your password here
string base64AuthString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authString));
msg.Headers.Authorization = new("Basic", base64AuthString);
msg.RequestUri = new Uri("http://serveraddress:2480/api/v1/command/mydb"); //add your server address (or localhost) and db name
HttpContent httpContent = new StringContent("{ \"language\": \"sql\", \"command\": \"INSERT into Profile set name = \'Alexander\'\" }", Encoding.UTF8, "application/json"); //customize command here
msg.Content = httpContent;
HttpResponseMessage response = await httpClient.SendAsync(msg);
string responseString = await response.Content.ReadAsStringAsync();
Debug.WriteLine("SENT REQUEST, RESPONSE: " + responseString);
}