This topic describes how to use the MySQL.Data library to develop C# applications.
Prerequisites
The LindormTable version is 2.6.2 or later. For more information about how to view or upgrade the version of LindormTable, see Release notes of LindormTable and Upgrade the minor engine version of a Lindorm instance.
The MySQL compatibility feature is enabled for the instance. For more information, see Enable the MySQL compatibility feature.
The IP address of your client is added to the whitelist of your Lindorm instance. For more information, see Configure a whitelist.
Procedure
Install Dotnet. You can download the installation package of Dotnet from the Dotnet official website.
Run the following code to create a Dotnet project:
dotnet new console --framework net7.0Run the following code to add the dependency of the MySQL.Data library:
dotnet add package MySql.Data -v 8.0.11Configure connection parameters.
string connStr = "server=ld-uf6k8yqb741t3****-proxy-sql-lindorm.lindorm.rds.aliyuncs.com;UID=user;database=default;port=33060;password=test";Parameters
Parameter
Description
server
The MySQL-compatible endpoint for LindormTable. For more information about how to obtain the endpoint, see View endpoints.
ImportantIf your application is deployed on an ECS instance, we recommend that you use a VPC to connect to the Lindorm instance to ensure higher security and lower network latency.
If your application is deployed locally, enable a public endpoint in the console before you connect to the Lindorm instance over the public network. To enable the endpoint, choose in the console. On the Wide Table Engine tab, click Enable Public Endpoint.
When you access the Lindorm instance over a VPC, set the server parameter to the VPC address for MySQL compatibility. When you access the Lindorm instance over the public network, set the server parameter to the Internet address for MySQL compatibility.
UID
If you forget your user password, you can reset it in the LindormTable cluster management system. For more information, see Change the user password.
password
database
The name of the database to which you want to connect. By default, your client is connected to a database named default.
port
The port used to access LindormTable using MySQL. The value of this parameter is fixed to 33060.
Create a connection to use LindormTable with wide table SQL syntax. The following example shows how to query all databases.
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
using System;
using MySql.Data.MySqlClient;
namespace connectLindorm
{
class Program
{
static void Main(string[] args)
{
// Set server to the MySQL-compatible endpoint for LindormTable.
// Set user to the username for LindormTable.
// Set database to the name of the database to connect to.
// Set password to the password for LindormTable.
// Set port to the port for the MySQL protocol of LindormTable. The value is fixed to 33060.
string connStr = "server=ld-uf6k8yqb741t3****-proxy-sql-lindorm.lindorm.rds.aliyuncs.com;UID=user;database=default;port=33060;password=test";
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 current instance contains only a database named default, the following result is returned:
Connecting to MySQL...
default
information_schema
Done.