Added list_code_example to csharp.md

pull/5722/head
meelahme 2024-12-26 18:30:39 -08:00
parent e28d3934f2
commit 560505d423
2 changed files with 74 additions and 1 deletions

View File

@ -11,6 +11,80 @@ menu:
identifier: influxdb3-csharp
influxdb/cloud-dedicated/tags: [C#, gRPC, SQL, Flight SQL, client libraries]
weight: 201
list_code_example: >
```csharp
// This example demonstrates how to use the InfluxDB v3 C# .NET client library
// to write sensor data to an InfluxDB database and query data from the last 90 days.
using System;
using System.Threading.Tasks;
using InfluxDB3.Client;
using InfluxDB3.Client.Api.Domain;
class Program
{
static async Task Main(string[] args)
{
// Initialize the InfluxDB client
var client = new InfluxDBClient("https://your-influxdb-url", "your-auth-token", "your-organization-id", "your-database-name");
try
{
// Write sensor data to the database
await WriteSensorData(client);
// Query data from the last 90 days
await QuerySensorData(client);
}
finally
{
// Dispose the client to release resources
client.Dispose();
}
}
private static async Task WriteSensorData(InfluxDBClient client)
{
// Create a point with tags, fields, and a timestamp
var point = new Point
{
Measurement = "home",
Tags = { { "room", "living_room" } },
Fields = { { "temperature", 23.5 } },
Timestamp = DateTime.UtcNow
};
// Write the point to the database
await client.WritePointAsync(point);
Console.WriteLine("Data successfully written to InfluxDB.");
}
private static async Task QuerySensorData(InfluxDBClient client)
{
// Define the Flux query
var query = @"
from(bucket: ""your-database-name"")
|> range(start: -90d)
|> filter(fn: (r) => r._measurement == ""home"")";
// Execute the query
var result = await client.QueryAsync(query);
// Print the results
Console.WriteLine("Queried data:");
foreach (var record in result)
{
Console.WriteLine($"{record.Timestamp}: {record.Values["_value"]}");
}
}
}
---
The InfluxDB v3 [`influxdb3-csharp` C# .NET client library](https://github.com/InfluxCommunity/influxdb3-csharp) integrates with C# .NET scripts and applications

View File

@ -15,7 +15,6 @@ list_code_example: >
// The following example demonstrates how to write sensor data into InfluxDB
// and retrieve data from the last 90 days for analysis.