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 whitelists.
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.0
Run the following code to add the dependency of the MySQL.Data library:
dotnet add package MySql.Data -v 8.0.11
Configure 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 LindormTable endpoint for MySQL. 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 on a local server and needs to connect to the Lindorm instance over the Internet, you can perform the following steps to enable the Internet endpoint for the instance in the Lindorm console: In the left-side navigation pane, select
. On the Wide Table Engine tab, click Enable Public Endpoint.If you use a VPC to access the Lindorm instance, specify the LindormTable VPC endpoint for MySQL in the value of server. If you use the Internet to access the Lindorm instance, specify the LindormTable Internet endpoint for MySQL in the value of server.
UID
If you forget your password, you can change the password in the cluster management system of LindormTable. For more information, see Manage users.
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 by using MySQL. The value of this parameter is fixed to 33060.
Establish a connection and use LindormTable SQL to perform operations in LindormTable. The following code block provides an example on how to use LindormTable SQL 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.");
Sample code
using System;
using MySql.Data.MySqlClient;
namespace connectLindorm
{
class Program
{
static void Main(string[] args)
{
// Set server to the LindormTable endpoint for MySQL.
// Set user to the username used to connect to LindormTable.
// Set database to the database that you want to connect.
// Set password to the password used to connect to LindormTable.
// Set port to the port used to access LindormTable by using MySQL. The value of this parameter 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.