public float getDanhGia(int id) { connect(); string query = "SELECT * FROM DANHGIA WHERE MaDuLieu = " + id; adapter = new SqlDataAdapter(query, connection); DataSet dataset = new DataSet(); adapter.Fill(dataset); ArrayList ls = ConvertDataSetToArrayList(dataset); List <DanhGia> arr = new List <DanhGia>(); int dem = 0; float tong = 0; foreach (Object o in ls) { DanhGia dg = (DanhGia)o; tong = tong + dg.Rate; dem++; } disconnect(); if (dem == 0) { return(0); } return(tong / dem); }
protected override object GetDataFromDataRow(DataTable dt, int i) { DanhGia dg = new DanhGia(); dg.IdUser = dt.Rows[i].IsNull("IdUser")? 0 : (int)dt.Rows[i]["IdUser"]; dg.IDMaDL = dt.Rows[i].IsNull("MaDuLieu") ? 0 : (int)dt.Rows[i]["MaDuLieu"]; dg.Rate = (dt.Rows[i].IsNull("DanhGia") == true) ? 0.0f : (float)(double)dt.Rows[i]["DanhGia"]; return((object)dg); }
public bool updateDanhGia(DanhGia dg) { try { connect(); string query = "SELECT * FROM DANHGIA WHERE IdUser = "******"AND MaDuLieu= " + dg.IDMaDL; adapter = new SqlDataAdapter(query, connection); DataSet dataset = new DataSet(); adapter.Fill(dataset); ArrayList ls = ConvertDataSetToArrayList(dataset); DanhGia arr = new DanhGia(); foreach (Object o in ls) { arr = (DanhGia)o; break; } if (arr.IdUser == 0 && arr.IDMaDL == 0 && arr.Rate == 0) { insertDanhGia(dg); } else { string updateCommand = "UPDATE DANHGIA SET DanhGia = " + dg.Rate + " WHERE IdUser = "******" AND MaDuLieu = " + dg.IDMaDL; executeNonQuery(updateCommand); } disconnect(); return(true); } catch (Exception e) { return(false); } //{ // try // { // connect(); // string updateCommand = "UPDATE DANHGIA SET DanhGia = " + dg.Rate + // " WHERE IdUser = "******" AND MaDuLieu = " + dg.IDMaDL; // executeNonQuery(updateCommand); // disconnect(); // return true; // } // catch (Exception e) // { // return false; // } }
public bool insertDanhGia(DanhGia dg) { try { connect(); string insertCommand = "INSERT INTO DANHGIA (IdUser, MaDuLieu, DanhGia) VALUES('" + dg.IdUser + "', " + dg.IDMaDL + ", " + dg.Rate + ")"; executeNonQuery(insertCommand); disconnect(); return(true); } catch (Exception e) { return(false); } }