This topic describes how to use Visual Studio for Mac to connect to an AnalyticDB for MySQL cluster by using C#.
Prerequisites
Visual Studio for Mac is downloaded and installed. Visual Studio 2019 for Mac version 8.6.5 is used in this topic.
Test data is prepared in AnalyticDB for MySQL. For more information, see Getting started with Data Warehouse Edition.
create table t1 (a int, s1 varchar)DISTRIBUTED BY HASH(`a`) ENGINE='XUANWU'; insert into t1 values (11, 'test1'), (22, 'test2'), (33, 'test3'), (44, 'test4');
create user test identified by 'test_123456'; grant select on test.* to test;
Procedure
Start Visual Studio.
Choose
. In the New Project dialog box, click Console Application and click Next.Create a sample project named hello world. Click the Execute icon in the upper-left corner. The system returns the execution results.
Modify the preceding sample code to include the code used to connect to an AnalyticDB for MySQL cluster and return the execution results from the t1 table.
using System; using MySql.Data.MySqlClient; namespace connectADB { class Program { static void Main(string[] args) { string connStr = "server=127.0.0.1;UID=test;database=test;port=3306;password=test_123456;SslMode=none;"; MySqlConnection conn = new MySqlConnection(connStr); try { Console.WriteLine("Connecting to MySQL..."); conn.Open(); string sql = "select * from `t1`"; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { Console.WriteLine(rdr[0] + " --- " + rdr[1]); } rdr.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } conn.Close(); Console.WriteLine("Done."); } } }
After you modify the code, an error occurs to indicate that you must import the MySqlConnector package.
Right-click Solution and select Manage NuGet Package.
NoteThe MySqlConnector package is required when you use C# to connect to an AnalyticDB for MySQL cluster.
On the Manage NuGet Package page, enter MySqlConnector in the search box and click Add Package.
After you add the MySqlConnector package, the error message disappears. Click the Execute icon in the upper-left corner. The system returns the correct results.