Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Abhishek YadavRank #217 · 11.3K reputationPosted Dec 1, 2025, 6:28 AM EST
To connect ASP.NET with MySQL, you just need the MySQL .NET Connector (
MySql.Data) and a proper connection string.1. Install MySQL Connector (NuGet)
2. Add Connection String
Web.config (ASP.NET MVC / WebForms):
ASP.NET Core (appsettings.json):
3. Basic C# Code Example
4. ASP.NET Core DI Example
?? That's all!
ASP.NET + MySQL works smoothly with MySql.Data NuGet.
Rajeesh MenothRank #118 · 18.4K reputationPosted Oct 7, 2025, 1:06 AM EDT
Please provide a brief explanation of your question so we can suggest the most suitable solution for your query.
Cynthia SathuragiriRank #937 · 2.2K reputationPosted Oct 8, 2025, 1:07 AM EDT
1. Install-Package MySql.Data section:
connectionString="server=localhost;user id=root;password=yourpassword;database=testdb;"
2. In your Web.config file, add a
providerName="MySql.Data.MySqlClient" />
3. Write C# Code to Connect and Query
using MySql.Data.MySqlClient;
using System;
using System.Web.UI;
public partial class Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlConn"].ConnectionString;
using (MySqlConnection conn = new MySqlConnection(connStr))
{
conn.Open();
Response.Write("Connection successful!");
}
}
}
Reading Data
using MySql.Data.MySqlClient;
using System;
using System.Web.UI;
public partial class ReadData : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlConn"].ConnectionString;
using (MySqlConnection conn = new MySqlConnection(connStr))
{
conn.Open();
string query = "SELECT id, name, email FROM users";
MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.Write($"ID: {reader["id"]}, Name: {reader["name"]}, Email: {reader["email"]}
");
}
}
}
}
Inserting Data
using MySql.Data.MySqlClient;
protected void btnSave_Click(object sender, EventArgs e)
{
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlConn"].ConnectionString;
using (MySqlConnection conn = new MySqlConnection(connStr))
{
conn.Open();
string sql = "INSERT INTO users (name, email) VALUES (@name, @email)";
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@email", txtEmail.Text);
cmd.ExecuteNonQuery();
Response.Write("Record inserted successfully!");
}
}
Sandhiya PriyaRank #26 · 43.2K reputationPosted Oct 13, 2025, 2:51 AM EDT
How to connect an ASP.NET application to MySQL.
I’ll cover the steps for both classic ASP.NET and ASP.NET Core.
1. Install MySQL Connector
You need MySQL.Data library (Connector/NET) to connect ASP.NET to MySQL.
Using NuGet:
Or for ASP.NET Core:
2. Create a Connection String
Example:
Server– MySQL server name (localhost if local).Database– Name of your database.User IDandPassword– MySQL credentials.SslMode=none– Optional for local development.In ASP.NET Core, you usually put this in
appsettings.json:3. Using MySqlConnection (Classic ASP.NET)
using MySql.Data.MySqlClient; string connString = "Server=localhost;Database=mydb;User ID=root;Password=yourpassword;SslMode=none;"; using (MySqlConnection conn = new MySqlConnection(connString)) { conn.Open(); string query = "SELECT * FROM Employees"; MySqlCommand cmd = new MySqlCommand(query, conn); using (MySqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { string name = reader["Name"].ToString(); int age = Convert.ToInt32(reader["Age"]); // Do something with data } } }4. Using Dependency Injection (ASP.NET Core)
Program.cs:
Controller Example:
public class EmployeeController : Controller { private readonly MySqlConnection _conn; public EmployeeController(MySqlConnection conn) { _conn = conn; } public IActionResult Index() { _conn.Open(); var cmd = new MySqlCommand("SELECT * FROM Employees", _conn); var reader = cmd.ExecuteReader(); var list = new List(); while (reader.Read()) { list.Add(reader["Name"].ToString()); } _conn.Close(); return View(list); } }Tips:
Always use
usingto dispose the connection.For security, never hard-code passwords in code; use
appsettings.jsonor environment variables.For production, consider connection pooling and
SslMode=Required.Maurice Norris5 reputationPosted Nov 30, 2025, 10:39 PM EST
Thanks for putting all these answers together — this thread is actually a really solid reference for anyone trying to hook ASP.NET up to MySQL. A lot of developers underestimate how picky MySQL can be with connections, especially when mixing classic ASP.NET and ASP.NET Core, so having these step-by-step examples is super valuable.
One thing I always remind newcomers is to verify that:
MySQL is running with the correct port (default 3306, but sometimes changed)
The MySQL user has permission for the host (e.g.,
root@localhostvsroot@%)SSL requirements match the server settings, otherwise you’ll hit random connection failures
Here’s a small extra example I use often — a clean, reusable helper for ASP.NET (classic) that keeps the connection logic in one place:
Then in your page or code-behind:
This structure keeps things clean, avoids repeated boilerplate, and works nicely with dependency injection. Anyway, thanks again for the detailed answers — they make this thread a super helpful reference for anyone wrestling with ASP.NET ? MySQL integration. And now that my database finally connects without drama, I’m rewarding myself with a break and a few rounds of mr flip — because debugging connection strings really feels like a platformer level sometimes.