c#第四次作业 c#操作MySQL数据库
2015-07-08 21:24
741 查看
<span style="font-size:32px;">关键代码如下</span>
//添加一行数据private void button1_Click(object sender, EventArgs e){//添加空行DataRow dr = dt.NewRow();dt.Rows.Add(dr);this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 2].Cells[0].Value = this.textBox1.Text;this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 2].Cells[1].Value = this.textBox2.Text;this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 2].Cells[2].Value = this.textBox3.Text;this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 2].Cells[3].Value = this.textBox4.Text;}//删除选中行private void button2_Click(object sender, EventArgs e){int RowNumber;RowNumber = dataGridView1.CurrentCell.RowIndex;dataGridView1.Rows.RemoveAt(RowNumber);}//修改后的dataGridView1存入数据库private void button4_Click(object sender, EventArgs e){try{//建立数据库连接String con = @"server=ADMIN/PC;database=shop;user id=sa;password=lihua";SqlConnection conn = new SqlConnection(con);//定义sql语句StringBuilder strsql = new StringBuilder();conn.Open();for (int i = 0; i < dataGridView1.Rows.Count - 1; i++){strsql.Append("insert into em03_demo(班级,学号,姓名,作业网址)");strsql.Append("values(");strsql.Append("'" + dataGridView1.Rows[i].Cells[0].Value + "',");strsql.Append("'" + dataGridView1.Rows[i].Cells[1].Value + "',");strsql.Append("'" + dataGridView1.Rows[i].Cells[2].Value + "',");strsql.Append("'" + dataGridView1.Rows[i].Cells[3].Value + "')");using (SqlCommand cmd = new SqlCommand(strsql.ToString(), conn)) //定义连接命令{cmd.ExecuteNonQuery();//执行sql语句}strsql.Clear();}conn.Close(); //关闭数据库conn.Dispose(); //释放内存空MessageBox.Show("已存入数据库");}
相关文章推荐
- 使用MYSQL benchmark - DBT2 Benchmark Tool遇到的问题
- mysql常用的东西
- MySQL 5.6的GTID复制报错没有事务号
- mysqldump使用方法
- mysqldump使用方法
- 关于mysql写触发器中运用游标的问题。
- mysql集群探索与实践
- mysql集群+复制
- mysqlcluster简介
- 数据类型和运算符(一)
- mysql相关时间函数
- 执行MySql更新报错:error code [1785]
- MySQL的binlog数据如何查看
- MySQL的binlog数据如何查看
- [转]LAMP 系统性能调优,第 3 部分: MySQL 服务器调优
- mysql数据类型
- Mysql数据库的优化
- jdbc 连接mysql数据库
- Mysql 简介
- MySQL与PostgreSQL:该选择哪个开源数据库?哪一个更好?