首页 > 代码库 > winFormToMysql&&几个控件的数据绑定

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

运行图:

技术分享

代码:

 1  private void button1_Click(object sender, EventArgs e)
 2         {
 3             string str = "database=csharp;data source=localhost;user id=root;password=root";
 4             MySqlConnection con = new MySqlConnection(str);
 5             con.Open();
 6             if (con.State == ConnectionState.Open) {
 7                 label1.Text = "success";
 8 
 9                 string mysql="select * from student";
10                 MySqlCommand cmd = new MySqlCommand(mysql, con);
11                 MySqlDataAdapter da = new MySqlDataAdapter(cmd);
12                 DataSet ds = new DataSet();
13 
14                 da.Fill(ds, "haha");
15                 dataGridView1.DataSource = ds;
16                 dataGridView1.DataMember = "haha";
17 
18                 textBox1.DataBindings.Clear();
19                 textBox1.DataBindings.Add("text", ds, "haha.name");
20 
21                 comboBox1.DataSource = ds;
22                 comboBox1.DisplayMember = "haha.name";
23                 comboBox1.ValueMember = "haha.name";
24             }

 

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

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