进行数据操作必然少了对数据的增删改查,用代码生成器生成的代码不是那么满意!方便在今后使用,这里就主要写“数据访问层(Dal)”
注:这里由于是用于用于测试时,临时建的数据库用于测试使用,在实际使用过程中些许改点参数就可以使用了
/// <summary> /// 是否存在该记录 /// </summary> public bool Exists(long Id) { string sql = "select count(*) Name from t_temp where [email protected]"; MySqlParameter[] param = { new MySqlParameter("@id", Id) }; long data=(long)MySqlHelper.ExecuteScalar(sql, param); if (data > 0) { return true; } return false; }
/// <summary> /// 增加一条数据 /// </summary> public long Add(Model model) { string sql = "insert into t_temp (Name) values (@name)"; MySqlParameter[] param = { new MySqlParameter("@name", model.Name) }; return MySqlHelper.ExecuteNonQuery(sql, param); }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model model) { string sql = "Update t_temp set [email protected] where [email protected]"; MySqlParameter[] param = { new MySqlParameter("@name", model.Name), new MySqlParameter("@id", model.Id) }; if (MySqlHelper.ExecuteNonQuery(sql, param) > 0) { return true; } return false; }
/// <summary> /// 删除一条数据 /// </summary> public bool Delete(long Id) { string sql = "delete from t_temp where [email protected]"; MySqlParameter[] param = { new MySqlParameter("@id", Id) }; if (MySqlHelper.ExecuteNonQuery(sql, param) > 0) { return true; } return false; }
/// <summary> /// 批量删除数据 /// </summary> public bool DeleteList(string Idlist) { string sql = "delete from t_temp where ID in (" + Idlist + ")"; if (MySqlHelper.ExecuteNonQuery(sql) > 0) { return true; } return false; }
/// <summary> /// 得到一个对象实体 /// </summary> public Model GetModel(long Id) { string sql = "select ID,Name from t_temp where [email protected]"; MySqlParameter[] param = { new MySqlParameter("@id", Id) }; DataTable table= MySqlHelper.ExecuteQuery(sql, param); if (table != null) { if (table.Rows.Count > 0) { return DataRowToModel(table.Rows[0]); } } return null; } /// <summary> /// 得到一个对象实体 /// </summary> public Model DataRowToModel(DataRow row) { Model mod = new Model(); if (row != null) { if(!string.IsNullOrEmpty(row["ID"].ToString())) { mod.Id=long.Parse(row["ID"].ToString()); } if (!string.IsNullOrEmpty(row["Name"].ToString())) { mod.Name = row["Name"].ToString(); } } return mod; }
时间: 2024-10-02 21:38:27