0

我是智能设备项目的新手。我安装了 MySQL Connector Net 6.4.4。我在 vs 2008 中为 mysql 连接添加了 MySql.Data.CF。

但我无法得到输出。“无法连接到任何指定的 MySQL 主机。” connection.open() 上的异常。

server=192.168.1.100;User Id=mcubic;Persist Security Info=True;database=mcubic

它是一个有效的连接字符串。所以数据库服务器用户名、密码都是正确的。没有问题。但我不明白为什么它只通过智能设备项目上的错误。

下面给出的代码。

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;

但没有用。

4

2 回答 2

2

我解决了我的问题。希望这对你也有帮助。下面的代码非常适合我。

    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();
于 2012-01-25T15:16:26.223 回答
0

我认为您的问题可能是由于智能设备连接到外部网络的方式。我认为您正在尝试使用模拟器从 Visual Studio 运行它。

我认为您需要先连接到 MySql 服务器所在的外部网络,然后再尝试连接到服务器本身。

这里有一篇文章涵盖了您的问题,但使用的是 SQL Server。我相信它会涵盖您遇到的相同问题。

于 2012-01-24T08:21:45.860 回答