gpt4 book ai didi

c# - 如何使用 Visual Studio 2017 从我的 CPanel 连接 MySql 数据库

转载 作者:行者123 更新时间:2023-11-29 07:24:27 24 4
gpt4 key购买 nike

我正在尝试使用 Visual Studio 从我的 cpanel 连接到 MySQL 数据库,但我一直收到错误:

A network-related or instance-specific error occurred while establishing a connection to 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. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

我已经安装了 MySql for Visual studio 和 Connector/net。我已经尝试使用服务器资源管理器并通过使用连接字符串的代码。我已将我的 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;
}
}

}
}

最佳答案

SqlConnection 对象只能用于连接到 MS-SqlServers。您必须使用 MySqlConnection 连接到 MySql-server。这也适用于 SqlCommand。此外,使用的连接字符串对 MySql 无效(“密码”应为“密码”)将您的字符串与 https://www.connectionstrings.com/mysql/ 进行比较

关于c# - 如何使用 Visual Studio 2017 从我的 CPanel 连接 MySql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54589666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com