";无法连接到任何指定的MySQL主机";在智能设备项目VS2008中
本文关键字:quot 智能 项目 VS2008 主机 连接 任何指 MySQL | 更新日期: 2023-09-27 18:21:22
我是智能设备项目的新手。我安装了MySQL连接器网络6.4.4。我在2008年的版本中添加了MySql连接的MySql.Data.CF。
但是我不能得到输出"无法连接到任何指定的MySQL主机。"连接上出现异常。open().
server=192.168.1.100;User Id=mcubic;Persist Security Info=True;database=mcubic
它是一个工作连接字符串。所以数据库服务器的用户id、密码都是正确的。这没什么问题。但我不明白为什么它通过错误的智能设备项目。
下面给出的代码。
try
{
string connectionString = "server=192.168.1.100;User Id=mcubic;Persist Security Info=True;database=mcubic";
string query = "select b.Outlet_Master_Name from mcs_user_outlet a,outlet_master b where a.Mcs_User_Outlet_User_Id=3 and a.Mcs_User_Outlet_Outlet_Id = b.Outlet_Master_Id";
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
comboBox1.Items.Add(Reader[0].ToString());
}
connection.Close();
}
catch(Exception ex)
{
MessageBox.Show(""+ex.Message);
}
帮我找出错误。
我已经尝试了以下连接字符串。
server=192.168.1.100;UID=mcubic;Persist Security Info=True;database=mcubic
server=192.168.1.100;User Id=mcubic;Persist Security Info=False;database=mcubic
server=192.168.1.100;User Id=mcubic;Persist Security Info=False;Initial Catalog=mcubic;
server=192.168.1.100;UID=mcubic;Persist Security Info=True;database=mcubic;pooling = false;
但没有用。
我解决了我的问题。希望这对你也有帮助。下面的代码非常适合我。
DataSet ds = new DataSet();
MySqlConnection conn = new MySqlConnection("server=server-ip;database=db-name;Character Set=utf8;Uid=user-name;password=****;");
MySqlCommand cmd = new MySqlCommand("select * from table-name", conn);
MySqlDataAdapter da = new MySqlDataAdapter();
conn.Open();
da.SelectCommand = cmd;
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Dispose();
cmd.Dispose();
我认为您的问题可能是由于智能设备连接外部网络的方式造成的。我认为您正试图使用模拟器从Visual Studio运行此程序。
我认为,在尝试连接到服务器本身之前,您需要连接到MySql服务器所在的外部网络。
这里有一篇文章介绍了您的问题,但涉及SQL Server。我相信它会涵盖你所经历的同样的问题。