winFormToMysql&&几个控件的数据绑定

时间:2024-06-10 21:03:26

运行图:

winFormToMysql&&几个控件的数据绑定

代码:

  private void button1_Click(object sender, EventArgs e)
{
string str = "database=csharp;data source=localhost;user id=root;password=root";
MySqlConnection con = new MySqlConnection(str);
con.Open();
if (con.State == ConnectionState.Open) {
label1.Text = "success"; string mysql="select * from student";
MySqlCommand cmd = new MySqlCommand(mysql, con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet(); da.Fill(ds, "haha");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "haha"; textBox1.DataBindings.Clear();
textBox1.DataBindings.Add("text", ds, "haha.name"); comboBox1.DataSource = ds;
comboBox1.DisplayMember = "haha.name";
comboBox1.ValueMember = "haha.name";
}

MySql.Data.dll下载位置:http://download.****.net/detail/u010940770/6519079