create a database test in mysql
#Schema
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
CREATE TABLE IF NOT EXISTS `employees` ( `Id` int(11) NOT NULL, `first_name` text NOT NULL, `last_name` text NOT NULL, PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `employees` -- INSERT INTO `employees` (`Id`, `first_name`, `last_name`) VALUES (1, 'aaaa', 'cccc'), (2, 'ssss', 'cccc'); |
#project.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true }, "dependencies": {}, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0" }, "MySql.Data.Core": "7.0.4-IR-191" }, "imports": "dnxcore50" } } } |
#.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using MySql.Data.MySqlClient; namespace ConsoleApplication { public class Program { public static void Main(string[] args) { Console.WriteLine("Hello World!"); MySqlConnection connection = new MySqlConnection { ConnectionString = "server=localhost;user id=root;password=;port=3306;database=test;SslMode=None" }; connection.Open(); MySqlCommand command = new MySqlCommand("SELECT * FROM employees;", connection); using (MySqlDataReader reader = command.ExecuteReader()) { System.Console.WriteLine("Category Id\t\tName\t\tLast Update"); while (reader.Read()) { string row = $"{reader["Id"]}\t\t{reader["first_name"]}\t\t{reader["last_name"]}"; System.Console.WriteLine(row); } } connection.Close(); System.Console.ReadKey(); } } } |