Aspnetcore using MySQL

Source: Internet
Author: User

Since Netcore Open source, then also use the open source MySQL database? Of course Netcore not only with the MSSQL database. Today I'll explain how Netcore uses MySQL for development.

Create a new Netcore project first

Then write two classes and put them in the model.

public class Lexan    {        private lexancontext lexancontext;        public string Name {get; set;}        public int Sex {get; set;}        public int Age {get; set;}    }

public class Lexancontext {public string ConnectionString {get; set;}        Public Lexancontext (String connectionString) {connectionString = connectionString;        } private Mysqlconnection getconnection () {return new mysqlconnection (ConnectionString);            } public list<lexan> Getlexan () {list<lexan> List = new list<lexan> (); using (Mysqlconnection connection=getconnection ()) {connection.                Open ();                Mysqlcommand command = new Mysqlcommand ("SELECT * from Lexan", connection); using (Mysqldatareader Reader=command. ExecuteReader ()) {while (reader. Read ()) {list. ADD (New Lexan () {Name=reader. GetString ("Name"), Sex=reader. GetInt32 ("Sex"), Age=reaDer.                    GetInt32 ("Age")});        }}} return list; }    }

Then install a plugin in the NuGet library to complete the connection to MySQL

Then add a controller

Public Iactionresult Index ()        {            Lexancontext wordcontext = HttpContext.RequestServices.GetService (typeof ( AspNetCoreUseMySQL.Model.LexanContext) as Lexancontext;            Return View (Wordcontext. Getlexan ());            return View ();        }

Then add an MVC view

Then add the following code, you can also make changes according to your own situation

@model ienumerable<aspnetcoreusemysql.model.lexan>@{    viewbag.title = "Lexan";} 

Then modify the Startup class and add the following code

Services. ADD (New Servicedescriptor (typeof (Lexancontext), New Lexancontext (Configuration.getconnectionstring (" DefaultConnection ")));

Then write the connection string to the Appsettings.json, notice here, everyone's password is different, you have to make a slight change, otherwise there will be a connection error

Let's create a database, create a table, and write a piece of data inside

  Create database Aspnetcoreusemysql;  Use Aspnetcoreusemysql;   Show tables;  CREATE TABLE Lexan (name varchar), sex char (1), age char (5));  Describe Lexan;  INSERT into Lexan values (' Lexan ', ' 0 ', ' n ');  SELECT * from Lexan;

All the work is done, let's run a look at the effect

Finally Bo Master here to say, any blogger blog, the trouble to add the original address, thank you! All blog posts come from Lexan's blog, and you can also focus on Blogger's post address

Aspnetcore using MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.