1
我有樹莓派IP = 192.168.137.165和我的Windows筆記本電腦有IP = 192.168.137.1 我寫C#代碼連接我的筆記本電腦的MySQL。如何在樹莓派上遠程連接mysql?
我也有RAS PI改變的my.cnf文件如下:
#bind-address = 127.0.0.1
bind-address = 0.0.0.0
而且在MySQL,配置爲允許來自任何IP地址的訪問:
GRANT ALL PRIVILEGES ON luan_van.* TO 'root_b'@'%' IDENTIFIED BY 'root';
flush privileges;
這裏我的C#代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace bp_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_connect_Click(object sender, EventArgs e)
{
try
{
string constring = "Server=192.168.137.165;Database=luan_van;Port=3306;User ID=root_b; Password=root";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlDataAdapter myData = new MySqlDataAdapter();
myData.SelectCommand = new MySqlCommand("select * from tt_nhanvien;", conDataBase);
MySqlCommandBuilder cb = new MySqlCommandBuilder(myData);
conDataBase.Open();
MessageBox.Show("Connect");
conDataBase.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
它的錯誤「爲您的地址無法獲取主機名」
它有什麼問題?我該如何解決它? 請幫幫我。謝謝!
有用的信息:不要硬編碼的連接字符串,你可以使用'MySqlConnectionStringBuilder'類來生成它爲您。相應地設置屬性並確保始終獲取有效的連接字符串。 – 2014-10-31 09:21:35