Hi,
I am facing a database connection problem. I am not sure what happens, but hope for experts' answers. :)
Here is my situation. I am developing an ASP .NET MVC web application using VS Community for MAC version 7.4.3 (build 10), and MAMP version 4.4.1.
I have do the following code and setup the database in phpMyAdmin provided by MAMP. Here are the details for the settings.
connection string in web.config:
<connectionStrings>
<add name="SchoolSystem" connectionString="Data Source=localhost;port=3306;Initial Catalog=SchoolSystem;User ID=root;Password=root" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
Source code:
public ActionResult Add()
{
string mainConn = ConfigurationManager.ConnectionStrings["SchoolSystem"].ConnectionString;
MySqlConnection mysql = new MySqlConnection(mainConn);
string query = "SELECT * from UserRole;";
MySqlCommand command = new MySqlCommand(query);
command.Connection = mysql;
mysql.Open();
MySqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
Console.WriteLine(dr["UserRoleID"].ToString());
Console.WriteLine(dr["UserRoleName"].ToString());
Console.WriteLine(dr["IsAll"].ToString());
Console.WriteLine(dr["IsAddStudent"].ToString());
Console.WriteLine(dr["IsDeleteStudent"].ToString());
Console.WriteLine(dr["IsAddTeacher"].ToString());
Console.WriteLine(dr["IsDeleteTeacher"].ToString());
}
mysql.Close();
return View();
}
phpMyAdmin configuration:
PHP
phpinfo shows the current configuration of PHP.
MySQL
MySQL can be administered with phpMyAdmin.
To connect to the MySQL server from your own scripts use the following connection parameters:
Host | localhost |
---|---|
Port | 3306 |
User | root |
Password | root |
Socket | /Applications/MAMP/tmp/mysql/mysql.sock |
phpMyAdmin URL:
Error message:
<div id="header"> <div id="header-text">Application Exception</div> </div> <div id="main">System.Net.Sockets.SocketException
Connection refused
Description: HTTP 500.Error processing request.
Details: Non-web exception. Exception origin (name of application or object): System.
<div>Exception stack trace:</div> <div class="details"></div> <div id="footer"> <div>Version Information:5.8.1.0 (2017-10/6bf3922f3fd Thu Mar 8 17:00:31 EST 2018); ASP.NET Version:4.0.30319.42000</div> </div> </div>Appreciate any help provided.