全部产品
云市场

C#访问

更新时间:2019-04-28 15:46:06

本章节介绍使用MySQL的.NET connector访问分析型数据库MySQL版。

  1. using System;
  2. using System.Data;
  3. using MySql.Data;
  4. using MySql.Data.MySqlClient;
  5. namespace adbdemo
  6. {
  7. public class Tutorial2
  8. {
  9. public static void Main()
  10. {
  11. string connStr = "server=...;UID=...;database=...;port=...;password=...;SslMode=none;";
  12. MySqlConnection conn = new MySqlConnection(connStr);
  13. try
  14. {
  15. Console.WriteLine("Connecting to MySQL...");
  16. conn.Open();
  17. string sql = "select c_custkey, c_name from customer limit 1";
  18. MySqlCommand cmd = new MySqlCommand(sql, conn);
  19. MySqlDataReader rdr = cmd.ExecuteReader();
  20. while (rdr.Read())
  21. {
  22. Console.WriteLine(rdr[0] + " --- " + rdr[]);
  23. }
  24. rdr.Close();
  25. }
  26. catch (Exception ex)
  27. {
  28. Console.WriteLine(ex.ToString());
  29. }
  30. conn.Close();
  31. Console.WriteLine("Done.");
  32. }
  33. }
  34. }