我试图将MySQL服务器与 ASP.NET Web应用程序连接,并且正在使用以下连接字符串和gettign错误System.Data.SqlClient.SqlException: 'Login failed for user 'root'.'
请从 web.config 文件中找到我的连接字符串。
<connectionStrings>
<add name="EmployeeAppDB" connectionString="server=localhost;database=employeedb;user id=root;password=root;"/>
</connectionStrings>
请在下面找到我的控制器类。
namespace WebAPI.Controllers
{
public class DepartmentController : ApiController
{
public HttpResponseMessage Get()
{
DataTable table = new DataTable();
string query = @"
select employeedb.department.DepartmentID, employeedb.department.DepartmentName from employeedb.department";
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["EmployeeAppDB"].ConnectionString))
using (var cmd = new SqlCommand(query, con))
using (var da = new SqlDataAdapter(cmd))
{
cmd.CommandType = CommandType.Text;
da.Fill(table); // am getting error in the line `System.Data.SqlClient.SqlException: 'Login failed for user 'root'.'`
}
return Request.CreateResponse(HttpStatusCode.OK, table);
}
}
}
您正在使用SqlConnection来连接数据库,而您应该使用MySqlConnection和MySqlCommand来连接和操作MySQL Server。
确保数据库名称和凭据正确,并且连接字符串看起来不错。
此外,您可以尝试以下连接字符串:
<add name="EmployeeAppDB" connectionString="Server=localhost; Database=employeedb; Uid=root; Pwd=root;Convert Zero Datetime=True;Connection Timeout=60;"/>