<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mysql connector tutorial &#8211; ASP.NET Hosting Reviews and Guides</title>
	<atom:link href="https://topreviewhostingasp.net/tag/mysql-connector-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://topreviewhostingasp.net</link>
	<description>ASP.NET Hosting &#124; Reviews &#124; Tips &#38; Tutorial</description>
	<lastBuildDate>Thu, 11 Feb 2021 03:53:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://topreviewhostingasp.net/wp-content/uploads/2017/01/cropped-trhaico-32x32.png</url>
	<title>mysql connector tutorial &#8211; ASP.NET Hosting Reviews and Guides</title>
	<link>https://topreviewhostingasp.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Run MySQL Connector in ASP.NET Core</title>
		<link>https://topreviewhostingasp.net/how-to-run-mysql-connector-in-asp-net-core/</link>
					<comments>https://topreviewhostingasp.net/how-to-run-mysql-connector-in-asp-net-core/#respond</comments>
		
		<dc:creator><![CDATA[Jacques Hunt]]></dc:creator>
		<pubDate>Thu, 11 Feb 2021 03:49:22 +0000</pubDate>
				<category><![CDATA[Hosting Tips]]></category>
		<category><![CDATA[asp net core mysql]]></category>
		<category><![CDATA[asp.net core]]></category>
		<category><![CDATA[asp.net core tutorial]]></category>
		<category><![CDATA[mysql connector]]></category>
		<category><![CDATA[mysql connector tips]]></category>
		<category><![CDATA[mysql connector tutorial]]></category>
		<guid isPermaLink="false">https://topreviewhostingasp.net/?p=2907</guid>

					<description><![CDATA[This tutorial shows how to use MySQL database with MySQL Connector in Asp.net Core. Before we start it, please make sure you have installed MySQL 5.7, .NET Core, Visual Studio, and also Sakila sample database. Let&#8217;s Get Started 1. The first step is to create ASP.NET Core Web Application and you can name it. For [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>This tutorial shows how to use MySQL database with MySQL Connector in Asp.net Core.</p>



<p>Before we start it, please make sure you have installed MySQL 5.7, <a href="https://dotnet.microsoft.com/download" target="_blank" rel="noreferrer noopener">.NET Core</a>, Visual Studio, and also <a href="https://dev.mysql.com/doc/sakila/en/sakila-installation.html" target="_blank" rel="noreferrer noopener">Sakila sample database</a>.</p>



<h2 class="wp-block-heading">Let&#8217;s Get Started</h2>



<p>1. The first step is to create ASP.NET Core Web Application and you can name it. For example, we use MvcSakilaCore.</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="955" height="582" class="wp-image-2908" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_010.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_010.png 955w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_010-300x183.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_010-768x468.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_010-50x30.png 50w" sizes="(max-width: 955px) 100vw, 955px" /></figure>



<p>2. Select Web Application template and No Authentication.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="800" height="620" class="wp-image-2909" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_020.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_020.png 800w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_020-300x233.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_020-768x595.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_020-50x39.png 50w" sizes="(max-width: 800px) 100vw, 800px" /></figure>



<p>3. You will see the project below:</p>



<figure class="wp-block-image size-large"><img decoding="async" width="768" height="416" class="wp-image-2910" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_030-768x416-1.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_030-768x416-1.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_030-768x416-1-300x163.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_030-768x416-1-50x27.png 50w" sizes="(max-width: 768px) 100vw, 768px" /></figure>



<p>4. Press F5 to run the application:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="768" height="409" class="wp-image-2911" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_040-768x409-1.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_040-768x409-1.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_040-768x409-1-300x160.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_040-768x409-1-50x27.png 50w" sizes="(max-width: 768px) 100vw, 768px" /></figure>



<p>5. Create a new folder named &#8220;Models&#8221; where it will store the database access:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="768" height="416" class="wp-image-2912" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_030-768x416-2.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_030-768x416-2.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_030-768x416-2-300x163.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_030-768x416-2-50x27.png 50w" sizes="(max-width: 768px) 100vw, 768px" /></figure>



<p>6. Installing MySQL Connector/NET Core Package</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="768" height="447" class="wp-image-2913" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/nuget-install.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/nuget-install.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/nuget-install-300x175.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/nuget-install-50x29.png 50w" sizes="(max-width: 768px) 100vw, 768px" /></figure>



<p>7. Add Connection string like below</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="678" height="98" class="wp-image-2914" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/connection-string.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/connection-string.png 678w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/connection-string-300x43.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/connection-string-50x7.png 50w" sizes="(max-width: 678px) 100vw, 678px" /></figure>



<p>8. Adding data modal classes.</p>



<p>For this example a Film class will be used. It contains the database fields as properties we want to show in our application.</p>



<pre class="wp-block-code"><code>using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MvcSakilaCore.Models
{
  public class Film
  {
    private SakilaContext context;

    public int FilmId { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public int ReleaseYear { get; set; }

    public int Length { get; set; }

    public string Rating { get; set; }
  }
}</code></pre>



<p>Create a new SakilaContext class that will contains the connections and Sakila database entities:</p>



<pre class="wp-block-code"><code>using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace MvcSakilaCore.Models
{
  public class SakilaContext
  {
    public string ConnectionString { get; set; }

    public SakilaContext(string connectionString)
    {
      this.ConnectionString = connectionString;
    }

    private MySqlConnection GetConnection()
    {
      return new MySqlConnection(ConnectionString);
    }

    public List&lt;Film&gt; GetAllFilms()
    {
      List&lt;Film&gt; list = new List&lt;Film&gt;();

      using (MySqlConnection conn = GetConnection())
      {
        conn.Open();
        MySqlCommand cmd = new MySqlCommand("SELECT * FROM film", conn);
        using (MySqlDataReader reader = cmd.ExecuteReader())
        {
          while (reader.Read())
          {
            list.Add(new Film()
            {
              FilmId = reader.GetInt32("film_id"),
              Title = reader.GetString("title"),
              Description = reader.GetString("description"),
              ReleaseYear = reader.GetInt32("release_year"),
              Length = reader.GetInt32("length"),
              Rating = reader.GetString("rating")
            });
          }
        }
      }

      return list;
    }
  }
}</code></pre>



<p>In order to be able to use our SakilaContext it’s required to register the instance as a service in our application. To do this add the code line in the Startup.cs file:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="998" height="630" class="wp-image-2915" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_3.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_3.png 998w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_3-300x189.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_3-768x485.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_3-50x32.png 50w" sizes="(max-width: 998px) 100vw, 998px" /></figure>



<p>9. Adding Film Controller</p>



<p>In Solution Explorer, right-click Controllers &gt; Add &gt; New Item… &gt; MVC Controller Class. Name the controller <strong>FilmsController</strong>:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="955" height="582" class="wp-image-2916" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-controller.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-controller.png 955w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-controller-300x183.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-controller-768x468.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-controller-50x30.png 50w" sizes="(max-width: 955px) 100vw, 955px" /></figure>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="768" height="416" class="wp-image-2917" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-controller-2.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-controller-2.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-controller-2-300x163.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-controller-2-50x27.png 50w" sizes="(max-width: 768px) 100vw, 768px" /></figure>



<p>Change the FilmsController code to this:</p>



<pre class="wp-block-code"><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MvcSakilaCore.Models;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace MvcSakilaCore.Controllers
{
  public class FilmsController : Controller
  {
    // GET: /&lt;controller&gt;/
    public IActionResult Index()
    {
      SakilaContext context = HttpContext.RequestServices.GetService(typeof(MvcSakilaCore.Models.SakilaContext)) as SakilaContext;

      return View(context.GetAllFilms());
    }
  }
}</code></pre>



<p>10. Creating the Films View</p>



<p>Start creating the Films folder under Views:</p>



<p>In Solution Explorer, right click Views &gt; Films &gt; Add &gt; New Item… &gt; ASP.NET &gt; MVC View Page</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="955" height="582" class="wp-image-2918" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-view.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-view.png 955w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-view-300x183.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-view-768x468.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/film-view-50x30.png 50w" sizes="(max-width: 955px) 100vw, 955px" /></figure>



<p>Add the following code into the new Index.cshtml view file:</p>



<pre class="wp-block-code"><code>@model IEnumerable&lt;MvcSakilaCore.Models.Film&gt;
@{
  ViewBag.Title = "Films";
}
&lt;h2&gt;Films&lt;/h2&gt;
&lt;table class="table"&gt;
  &lt;tr&gt;
    &lt;th&gt;Film ID&lt;/th&gt;
    &lt;th&gt;Title&lt;/th&gt;
    &lt;th&gt;Description&lt;/th&gt;
    &lt;th&gt;Release Year&lt;/th&gt;
    &lt;th&gt;Length&lt;/th&gt;
    &lt;th&gt;Rating&lt;/th&gt;
  &lt;/tr&gt;
  @foreach (var item in Model)
  {
    &lt;tr&gt;
      &lt;td&gt;
        @Html.DisplayFor(modelItem =&gt; item.FilmId)
      &lt;/td&gt;
      &lt;td&gt;
        @Html.DisplayFor(modelItem =&gt; item.Title)
      &lt;/td&gt;
      &lt;td&gt;
        @Html.DisplayFor(modelItem =&gt; item.Description)
      &lt;/td&gt;
      &lt;td&gt;
        @Html.DisplayFor(modelItem =&gt; item.ReleaseYear)
      &lt;/td&gt;
      &lt;td&gt;
        @Html.DisplayFor(modelItem =&gt; item.Length)
      &lt;/td&gt;
      &lt;td&gt;
        @Html.DisplayFor(modelItem =&gt; item.Rating)
      &lt;/td&gt;
    &lt;/tr&gt;
  }
&lt;/table&gt;</code></pre>



<p>Before run the application, add the Films path to the running url.</p>



<p>In Solution Explorer, right click MvcSakilaCore &gt; Properties &gt; Debug &gt; Launch URL &gt; Films:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="998" height="630" class="wp-image-2919" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_4.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_4.png 998w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_4-300x189.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_4-768x485.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_4-50x32.png 50w" sizes="(max-width: 998px) 100vw, 998px" /></figure>



<p>Run the application (press F5) and the Films list should be displayed:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="768" height="373" class="wp-image-2920" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_5.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_5.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_5-300x146.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2021/02/cnet_aspnetcore_5-50x24.png 50w" sizes="(max-width: 768px) 100vw, 768px" /></figure>



<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://topreviewhostingasp.net/how-to-run-mysql-connector-in-asp-net-core/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
