简体   繁体   English

如何使用mysql在asp.net中连接远程数据库?

[英]How to connect remote database in asp.net using mysql?

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        string MyConString = "SERVER=http://10.54.3.208:8080/Ager/person;" + "DATABASE=agero;" + "UID=root;" + "PASSWORD=root;";
        MySqlConnection con = new MySqlConnection(MyConString);
        //MySqlConnection command = con.CreateCommand();
        con.Open();
        string s = "select * from boopathi where STU_ID =@sid and STU_PWD =@pwd";
        MySqlCommand cmd = new MySqlCommand(s, con);
        cmd.Parameters.AddWithValue("@sid", TextBox1.Text.ToString());
        cmd.Parameters.AddWithValue("@pwd", TextBox2.Text.ToString());
        cmd.ExecuteNonQuery();
        MySqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            if (dr.HasRows == true)
            {
                Server.Transfer("WebForm1.aspx");
            }
        }      
        //close connection
        con.Close();

    }
    catch (Exception ex)
    {
        Response.Write("An error occured: " + ex.Message);
    }
}

In localhost it is working perfectly. 在本地主机中,它运行良好。 Once I set my remote link instead of localhost. 一旦我设置了远程链接而不是本地主机。 It's not connecting anymore. 它不再连接了。 I am getting exception like Unable to connect to any of the specified MySQL hosts. 我遇到Unable to connect to any of the specified MySQL hosts.类的异常Unable to connect to any of the specified MySQL hosts.

Try it:- 试试吧:-

MySqlConnection c = new MySqlConnection("server=10.54.3.208; database=agero; uid=root; pwd=root");

OR 要么

string MyConString = "SERVER=10.54.3.208;" + "DATABASE=agero;" + "UID=root;" + "Pwd=root;";

refer this link for more information MySQL Connector 请参阅此链接以获取更多信息MySQL Connector

I hope it may help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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