All Products
Search
Document Center

Lindorm:Use C# to develop applications

Last Updated:Feb 29, 2024

This topic describes how to use the MySQL.Data library to develop C# applications.

Prerequisites

Procedure

  1. Install Dotnet. You can download the installation package of Dotnet from the Dotnet official website.

  2. Run the following code to create a Dotnet project:

    dotnet new console --framework net7.0
  3. Run the following code to add the dependency of the MySQL.Data library:

    dotnet add package MySql.Data -v 8.0.11
  4. 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.

    Important
    • If 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 Database Connections > Wide Table Engine. 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.

  5. 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.