清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
vs2010中运行,必须引用Mysql.data /连接数据库/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using MySql.Data; using System.Data; using MySql.Data.MySqlClient; namespace 连接数据库 { class Program { static void Main(string[] args) { MySqlConnection conn = null; conn=new MySqlConnection(“Data Source=localhost;Database=php;User ID=root;Password=111111”); conn.Open(); if (conn.State.ToString() == “Open”) Console.WriteLine(“连接成功!”); else Console.WriteLine(“连接失败!”); } } } /执行增删改操作/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using MySql.Data; using System.Data; using MySql.Data.MySqlClient; namespace 连接数据库 { class Program { static void Main(string[] args) { MySqlConnection conn = null; conn=new MySqlConnection(“Data Source=localhost;Database=php;User ID=root;Password=111111”); conn.Open(); if (conn.State.ToString() == “Open”) Console.WriteLine(“连接成功!”); else Console.WriteLine(“连接失败!”); MySqlCommand com = new MySqlCommand(); com.Connection = conn; com.CommandText = "insert into class values(1,'xiaoming',23),(2,'xiaoliang',5)"; int i = com.ExecuteNonQuery(); if (i > 0) Console.WriteLine("插入记录{0}条", i); else Console.WriteLine("插入不成功!"); } } } /查询操作/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using MySql.Data; using System.Data; using MySql.Data.MySqlClient; namespace 连接数据库 { class Program { static void Main(string[] args) { MySqlConnection conn = null; conn=new MySqlConnection(“Data Source=localhost;Database=php;User ID=root;Password=111111”); conn.Open(); if (conn.State.ToString() == “Open”) Console.WriteLine(“连接成功!”); else Console.WriteLine(“连接失败!”); MySqlCommand com = new MySqlCommand("select * from class", conn); MySqlDataReader dr = null; dr = com.ExecuteReader(); while (dr.Read()) { Console.WriteLine(dr["id"] + " " + dr["name"] + " " + dr["age"]); } } } } /连续查询/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using MySql.Data; using System.Data; using MySql.Data.MySqlClient; namespace 连接数据库 { class Program { static void Main(string[] args) { MySqlConnection conn = null; conn=new MySqlConnection(“Data Source=localhost;Database=php;User ID=root;Password=111111”); conn.Open(); if (conn.State.ToString() == “Open”) Console.WriteLine(“连接成功!”); else Console.WriteLine(“连接失败!”); MySqlCommand com = new MySqlCommand("select * from class where id =1;select * from class where id =2", conn); MySqlDataReader dr = null; dr = com.ExecuteReader(); while (dr.Read()) { Console.WriteLine(dr["id"] + " " + dr["name"] + " " + dr["age"]); } if (dr.NextResult()) { while(dr.Read()) Console.WriteLine(dr["id"] + " " + dr["name"] + " " + dr["age"]); } } } } SqlConnection conn; SqlDataAdapter adapter; private void button1_Click(object sender, EventArgs e) { conn = new SqlConnection("server=.;database=db_16;uid=sa;pwd=123456");//连接数据库 SqlDataAdapter sda = new SqlDataAdapter("select * from tb_emp",conn); DataSet ds = new DataSet(); sda.Fill(ds); dataGridView1.DataSource = ds.Tables[0];//绑定数据源到datagridView dataGridView1.RowHeadersVisible = false;//隐藏首列 for (int i = 0; i < dataGridView1.ColumnCount;i++ ) { dataGridView1.Columns[i].Width = 84; } button1.Enabled = false; dataGridView1.Columns[0].ReadOnly = true; } private DataTable dbconn(string strSql)//连接数据库,填充DataTable并返回 { conn.Open(); this.adapter = new SqlDataAdapter(strSql, conn); DataTable dtSelect = new DataTable(); int rnt = this.adapter.Fill(dtSelect); conn.Close(); return dtSelect; } private void button2_Click(object sender, EventArgs e) { if (dbUpdate()) //调用private Bool dbUpdate()并判断 { MessageBox.Show("修改成功!"); } } private Boolean dbUpdate() { string strSql = "select * from tb_emp"; DataTable dtUpdate = new DataTable(); dtUpdate = this.dbconn(strSql);//调用private DataTable dbconn(string strSql)并得到dtUpdate空表 dtUpdate.Rows.Clear(); DataTable dtShow = new DataTable(); dtShow = (DataTable)this.dataGridView1.DataSource;//把修改后的datagridview数据绑定到dtShow表中 for (int i = 0; i < dtShow.Rows.Count; i++) { dtUpdate.ImportRow(dtShow.Rows[i]);//把dtShow表中的数据复制到dtUpdate空表中 } try { this.conn.Open(); //打开数据库并更新 SqlCommandBuilder CommandBuiler; CommandBuiler = new SqlCommandBuilder(this.adapter); this.adapter.Update(dtUpdate); this.conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); return false; } dtUpdate.AcceptChanges();//更新数据库 return true; } }