我正面临数据库连接问题。我不确定会发生什么,但希望专家'答案。 :)
这是我的情况。我正在使用VS Community for MAC 7.4.3版(版本10)和MAMP版本4.4.1开发ASP .NET MVC Web应用程序。
我已经在MAMP提供的phpMyAdmin中执行以下代码并设置数据库。以下是设置的详细信息。
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>
源代码:
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配置:
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:
http://localhost:8888/phpMyAdmin/sql.php?db=SchoolSystem&table=UserRole&token=1cd40620cbc2325e1f15a1c443f327af&pos=0
错误讯息:
<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>
感谢所提供的任何帮助。