How to connect MySql with MVC and Work bench

Many time developer facing this how to connect with my SQL with MVC. This article demonstrates how to connect with MySQL.With help of Mysql.Data library we can easily connect to SQL Server Database.

 

 

1. Install MySQL workbench

2.Install  Wampserver (without this application can not be connect to Database).

3. Install mys sql connector from nuget pakage manager as given in picture.

How to connect MySql with MVC and Work bench How to connect MySql with MVC and Work bench

customerModel.cs
public class customerModel</div>
    {
        public string email { get; set; }</div>
        public string password { get; set; }</div>
    }
HomeController.cs
MySqlConnection con = new MySqlConnection();
public ActionResult Login()
{

  return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model)
{
try
{
   if (ModelState.IsValid)
{
      string strCon = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (con = new MySqlConnection(strCon))
{
    using (MySqlCommand cmd = new MySqlCommand("select * from customer where email=@email and pass=@pass"))
    {
      cmd.Connection = con;
      cmd.Parameters.AddWithValue("@email", model.Email);
      cmd.Parameters.AddWithValue("@pass", model.Password);
      con.Open();
      MySqlDataReader sdr = cmd.ExecuteReader();
      while (sdr.Read())
      {
          Session["user"] = sdr["email"].ToString();
      }
      if (Session["user"] == null)
      {
          ViewBag.Message = "Id Password not matched";
      }
      else
      {   
          return RedirectToAction("Index");
      }
      con.Close();
}
}
}
}
catch (Exception ex){}
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Index()
{
    ViewBag.Message = "WELCOME";

    return View();
}

login.cshml


@model WebApplication1.Models.LoginViewModel

@{
ViewBag.Title = "Login";
}

<h2>Login</h2>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>LoginViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.RememberMe, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.RememberMe)
@Html.ValidationMessageFor(model => model.RememberMe, "", new { @class = "text-danger" })
</div>
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")

Index.cshtm

How to connect MySql with MVC and Work bench


@{
ViewBag.Title = "Index";
}

<h2>Index</h2>
welcome <b>@Session["user"]</b>

How to connect MySql with MVC and Work bench