【发布时间】:2019-02-08 18:21:05
【问题描述】:
我正在尝试使用 Visual Studio 从我的 cpanel 连接到 MySQL 数据库,但我不断收到错误消息:
发生网络相关或特定于实例的错误,同时 建立与 SQL Server 的连接。找不到服务器或 无法访问。验证实例名称是否正确,并且 SQL Server 配置为允许远程连接。 (提供者:命名 Pipes Provider,错误:40 - 无法打开与 SQL Server 的连接)
我已经为 Visual Studio 和 Connector/net 安装了 MySql。我已经尝试使用服务器资源管理器和使用连接字符串的代码。我已将我的 ip 添加到 CPanel 中 Remote MySql 的访问主机列表中。但没有任何效果。
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;
}
}
}
}
【问题讨论】:
-
您提到了 MySQL,但错误消息显示的是 SQL Server。这两个是不同的数据库引擎。确保使用正确的连接对象。