|
发表于 2020-1-18 21:00:01
|
显示全部楼层
河南省郑州市
- 1.完成SqlHelper 中DataSet 部分的封装
- #region 04 执行cha询语句 DataAdapter 方式集合
- /// <summary>
- /// 执行cha询语句 DataAdapter,获取DataSet
- /// </summary>
- /// <returns>返回DataSet</returns>
- public DataSet ExecuteDataAdapter(string cmdText, Mxxarameter[] parameters, string connStr = "", CommandType type = CommandType.Text,string dataSetName = "table")
- {
- connStr = string.IsNullOrWhiteSpace(connStr) ? _connStr : connStr;
- using (MySqlConnection conn = new MySqlConnection(connStr))
- {
- using (MySqlCommand cmd = new MySqlCommand())
- {
- Prepare(conn, type, cmdText, cmd, null, parameters);
- using (MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd))
- {
- DataSet ds = new DataSet();
- dataAdapter.Fill(ds, dataSetName);
- return ds;
- }
- }
- }
- }
- public DataSet ExecuteDataAdapter(MySqlConnection conn, string cmdText, Mxxarameter[] parameters, CommandType type = CommandType.Text, string dataSetName = "table")
- {
- using (MySqlCommand cmd = new MySqlCommand())
- {
- Prepare(conn, type, cmdText, cmd, null, parameters);
- using (MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd))
- {
- DataSet ds = new DataSet();
- dataAdapter.Fill(ds, dataSetName);
- return ds;
- }
- }
- }
- public DataSet ExecuteDataAdapter(MySqlTransaction trans, string cmdText, Mxxarameter[] parameters, CommandType type = CommandType.Text, string dataSetName = "table")
- {
- using (MySqlCommand cmd = new MySqlCommand())
- {
- Prepare(trans.Connection, type, cmdText, cmd, trans, parameters);
- using (MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd))
- {
- DataSet ds = new DataSet();
- dataAdapter.Fill(ds, dataSetName);
- return ds;
- }
- }
- }
- #endregion
- 2.完成课程中后面的 修改 删除
- class StudentDal中
- /// <summary>
- /// 删除选中行的第一个
- /// </summary>
- /// <returns>受影响行数</returns>
- public int Delete(int id)
- {
- string sql = "DELETE FROM students WHERE id = @id";
- List<Mxxarameter> parameters = new List<Mxxarameter> {
- new Mxxarameter("@id",id)
- };
- MysqlHelper mysql = new MysqlHelper("connStr");
- int rel = Convert.ToInt32(mysql.ExecuteNonQuery(sql, parameters.ToArray()));
- return rel;
- }
- /// <summary>
- /// 更新选中行,选中列的值
- /// </summary>
- /// <returns>受影响行数</returns>
- public int Update(int id,string item,string value)
- {
- string sql = "UPDATE FROM students SET "+item+" =[url=home.php?mod=space&uid=22064]@value[/url] WHERE id = @id";
- List<Mxxarameter> parameters = new List<Mxxarameter> {
- new Mxxarameter("@id",id),
- new Mxxarameter("@value",value)
- };
- MysqlHelper mysql = new MysqlHelper("connStr");
- int rel = Convert.ToInt32(mysql.ExecuteNonQuery(sql, parameters.ToArray()));
- return rel;
- }
- 删除调用
- private void tsmu_delete_Click(object sender, EventArgs e)
- {
- if (dgv_students.SelectedRows.Count > 0)
- {
- int index = dgv_students.SelectedRows[0].Index;
- int id = Convert.ToInt32(dgv_students.Rows[index].Cells[0].Value);
- if (dal.Delete(id) > 0) {
- list.Clear();
- int total;
- List<Student> students = dal.GetStudentsByPage(1, 10, out total);
- list = new BindingList<Student>(students);
- dgv_students.DataSource = list;
- }
- }
-
- }
复制代码 |
|