This topic describes how to use Visual Studio for Mac to connect to an AnalyticDB for MySQL cluster through C#.
Prerequisites
Visual Studio for Mac is downloaded and installed. For more information about the download method, visit Visual Studio for Mac. 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 Quick start of AnalyticDB for MySQL.
create table t1 (a int, s1 varchar)DISTRIBUTED BY HASH(`a`) ENGINE='CSTORE'; 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
. On 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 result.
Modify the preceding sample code and add related code used to connect to AnalyticDB for MySQL and return the execution result of 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 will be returned. 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 AnalyticDB for MySQL.
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 Execution icon. The system returns the correct result.