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

时间:2014-03-06 10:18:45

标签: c# mysql asp.net

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);
    }
}

在localhost中它完美运行。一旦我设置了远程链接而不是localhost。它不再连接了。我得到了像Unable to connect to any of the specified MySQL hosts.

这样的异常

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

试一试: -

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

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

请参阅此链接以获取更多信息MySQL Connector

我希望它可以帮助你。