简体   繁体   English

如何使用 Visual Studio 2017 从我的 CPanel 与 MySql 数据库建立连接

[英]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.

相关问题 从Visual Studio 2017社区建立到Linux虚拟机上运行的mysql的ac#连接 - make a c# connection to mysql running on linux virtual box from visual studio 2017 community 如何在Visual Studio 2017中从C#代码制作SQL Server 2017本地数据库? - How to make a SQL Server 2017 local database from C# code in Visual Studio 2017? Visual Studio 2017 未在连接列表中显示 MySQL - Visual Studio 2017 not showing MySQL in connection list 如何在Visual Studio 2017中使用数据库 - How to use a database in Visual Studio 2017 MySql Visual Studio 2017(错误:您已经有一个可用的连接) - MySql Visual Studio 2017 (Error: You have a usable connection already) Visual Studio 2017 Web部署emply数据库连接字符串 - visual studio 2017 web deploy emply database connection strings 在Visual Studio 2017中使用Windows Service访问MySQL数据库 - Acessing MySQL database with Windows Service in Visual Studio 2017 用Visual Studio 2017无法生成我程序的可执行文件 - Cannot make executable file of my program with Visual Studio 2017 Visual Studio 2017和MySQL EntityFramework - Visual Studio 2017 and MySQL EntityFramework 如何在仍然引用 NUnit 的同时使我的 Visual Studio 2017 项目不是测试项目 - How can I make my visual studio 2017 project NOT a test project while still referencing NUnit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM