简体   繁体   中英

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:

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)

I have installed both MySql for Visual studio and Connector/net. 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. 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. You have to use MySqlConnection to connect to MySql-server. This also applies to the SqlCommand. Furthermore the used connection-string is not valid for MySql ('Password' should be 'Pwd') Compare your string to https://www.connectionstrings.com/mysql/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM