Connect a C# application to LindormTable using the MySQL.Data library and wide table SQL syntax.
Prerequisites
Before you begin, make sure you have:
LindormTable version 2.6.2 or later. To check or upgrade your version, see Release notes of LindormTable and Upgrade the minor engine version of a Lindorm instance
The MySQL compatibility feature enabled for your Lindorm instance. See Enable the MySQL compatibility feature
Your client IP address added to the instance whitelist. See Configure a whitelist
Set up a .NET project
Run the following commands to create a project and add the MySQL.Data dependency:
dotnet new console --framework net7.0
dotnet add package MySql.Data -v 8.0.11If you haven't installed .NET 7.0, download it from the .NET official website.
Connect to LindormTable
Step 1: Configure the connection string
Build a connection string with the following parameters:
string connStr = "server=<endpoint>;UID=<username>;database=<database>;port=33060;password=<password>";Replace the placeholders with your actual values:
| Parameter | Description |
|---|---|
server | The MySQL-compatible endpoint for LindormTable. To get the endpoint, see View endpoints. Use the VPC address when connecting from an ECS instance, or the Internet address when connecting over the public network. |
UID | Your LindormTable username. To reset a forgotten password, see Change the user password. |
password | Your LindormTable password. |
database | The database to connect to. The default database name is default. |
port | Fixed value: 33060. |
For ECS-hosted applications, use a Virtual Private Cloud (VPC) connection to reduce network latency and improve security. To connect over the public network, enable the public endpoint first: in the console, go to Database Connections > Wide Table Engine, then click Enable Public Endpoint.
Step 2: Open a connection and run a query
The following example uses MySqlConnection to open a connection, MySqlCommand to define a SQL query, and MySqlDataReader to iterate over the results.
MySqlConnection conn = new MySqlConnection(connStr);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string sql = "show databases;";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
rdr.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Console.WriteLine("Done.");Complete example
The following is a complete, runnable example that connects to LindormTable and lists all databases.
Replace <endpoint>, <username>, and <password> with your actual values before running.
using System;
using MySql.Data.MySqlClient;
namespace connectLindorm
{
class Program
{
static void Main(string[] args)
{
// server: MySQL-compatible endpoint for LindormTable
// UID: your LindormTable username
// database: the database to connect to
// port: fixed to 33060 for the MySQL protocol
// password: your LindormTable password
string connStr = "server=<endpoint>;UID=<username>;database=default;port=33060;password=<password>";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string sql = "show databases";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
rdr.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Console.WriteLine("Done.");
}
}
}If the instance contains only the default database, the output is:
Connecting to MySQL...
default
information_schema
Done.What's next
To manage databases and tables, use wide table SQL syntax through the same connection.
In production, pass the connection string via environment variables instead of hardcoding credentials in source code.