远程连接到MYSQL

时间:2015-03-06 11:58:43

标签: c# mysql database ip

当我在mysql的connectionstring中硬编码ip地址时,客户端系统成功地从主机系统(ip)从数据库获取数据。但是当我尝试通过从客户端系统中的txt doc获取IP地址来连接到同一个时,它无法连接到我的主机数据库。但是相同的代码(从txt doc中恢复ip)在主机系统中工作。为什么它不能在客户端系统中工作?我使用了以下代码,任何人都可以帮我找到错误吗?

    namespace ThermalDotNet
    {
    public partial class ipcheck : Form
    {
    public ipcheck()
    {
        InitializeComponent();
        labelget();
        myconnectionstring = "Server='" + localhost + "';port = 3306; Database=" + database + "; Uid=root; Pwd=fatehshah; Allow Zero DateTime = True";
    }
   // string constr = "Server=192.168.1.102; Database=mydb; Uid=myusername; Pwd=mypassword; Allow Zero DateTime = True";
    string localhost = null;
    string str;
    string database = null;
    string[] strarray;
    string myconnectionstring = null;

    public void labelget()
    {

        try
        {
            // Create an instance of StreamReader to read from a file. 
            // The using statement also closes the StreamReader. 
            using (StreamReader sr = new StreamReader("C:/Requirement.txt"))
            {

                while ((str = sr.ReadLine()) != null)
                {
                    str = sr.ReadLine();
                    strarray = str.Split(',');
                    localhost = strarray[0];
                    database = strarray[1];
                }
            }
        }
        catch (Exception ea)
        {
            // Let the user know what went wrong.
            MessageBox.Show("error is " + ea);
        }
        Console.Read();
    }
    private void ipcheck_Load(object sender, EventArgs e)
    {
        MySqlConnection con = new MySqlConnection(myconnectionstring);
        con.Open();
        MySqlCommand cmd = new MySqlCommand("SELECT CAT_NAME FROM CATEGORY WHERE CAT_ID = '1'", con);
        MySqlDataReader r = cmd.ExecuteReader();
        while (r.Read())
        {
            label1.Text = r["CAT_NAME"].ToString();
        }
        con.Close();
    }
}
}

0 个答案:

没有答案