[英]How to make a connection with MySql database from my CPanel with Visual Studio 2017
I am trying to make a connection to MySQL database from my cpanel with Visual studio, but I keep getting an error:我正在尝试使用 Visual Studio 从我的 cpanel 连接到 MySQL 数据库,但我不断收到错误消息:
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
与 SQL Server 建立连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible.
服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
(提供程序:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接)
I have installed both MySql for Visual studio and Connector/net.我已经为 Visual Studio 和 Connector/net 安装了 MySql。 I have tried with both Server Explorer and through the code using the connection string.
我已经尝试过使用服务器资源管理器并通过使用连接字符串的代码。 I have added my ip to the access host list on Remote MySql in CPanel.
我已将我的 ip 添加到 CPanel 中 Remote MySql 上的访问主机列表中。 But nothing worked.
但没有任何效果。
namespace Program
{
public partial class WebForm1 : System.Web.UI.Page
{
string connectionString = @"SERVER=mydomain.net;DATABASE=mydatabasename;UID=myuser;PASSWORD=mypass";
protected void Page_Load(object sender, EventArgs e)
{
FillDropDown(DropDownList1);
}
public void FillDropDown(DropDownList dropDown)
{
try
{
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
SqlCommand sqlCmd = new SqlCommand("Select * from MyTable", sqlCon);
sqlCon.Open();
dropDown.DataSource = sqlCmd.ExecuteReader();
dropDown.DataBind();
dropDown.DataTextField = "Name";
dropDown.DataValueField = "Id";
}
}
catch(Exception ex)
{
lblError.Text = ex.Message;
}
}
}
}
The SqlConnection-object may only be used for connections to MS-SqlServers. SqlConnection 对象只能用于连接到 MS-SqlServer。 You have to use MySqlConnection to connect to MySql-server.
您必须使用 MySqlConnection 连接到 MySql 服务器。 This also applies to the SqlCommand.
这也适用于 SqlCommand。 Furthermore the used connection-string is not valid for MySql ('Password' should be 'Pwd') Compare your string to https://www.connectionstrings.com/mysql/
此外,使用的连接字符串对 MySql 无效(“密码”应为“密码”)将您的字符串与https://www.connectionstrings.com/mysql/进行比较
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.