//Menambahkan post baru public string AddNewPost(string judul, string deskripsi, string hasil_tani, DateTime tgl_post, string foto, string stok, string lokasi, string provinsi, string kota, string harga) { Form_Login fl = new Form_Login(); MySqlCommand cmd; FileStream fs; BinaryReader br; string cek = ""; try { if (judul.Equals("") || deskripsi.Equals("") || stok.Equals("") || lokasi.Equals("") || harga.Equals("") || foto.Equals("")) { cek = "Data harus terisi semua"; } else { string FileName = foto; byte[] ImageData; fs = new FileStream(FileName, FileMode.Open, FileAccess.Read); br = new BinaryReader(fs); ImageData = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); string CmdString = "insert into post (id, judul, deskripsi, hasil_tani, tgl_post, foto, " + "stok, lokasi, provinsi, kota, harga, username) values (NULL, @Judul, @Deskripsi, " + "@Hasil, @Tgl, @Foto, @Stok, @Lokasi, @Provinsi, @Kota, @Harga, @Username)"; cmd = new MySqlCommand(CmdString, con); cmd.Parameters.Add("@Judul", MySqlDbType.VarChar, 50); cmd.Parameters.Add("@Deskripsi", MySqlDbType.VarChar, 255); cmd.Parameters.Add("@Hasil", MySqlDbType.Enum); cmd.Parameters.Add("@Tgl", MySqlDbType.Date); cmd.Parameters.Add("@Foto", MySqlDbType.Blob); cmd.Parameters.Add("@Stok", MySqlDbType.Int32); cmd.Parameters.Add("@Lokasi", MySqlDbType.VarChar, 100); cmd.Parameters.Add("@Provinsi", MySqlDbType.Enum); cmd.Parameters.Add("@Kota", MySqlDbType.Enum); cmd.Parameters.Add("@Harga", MySqlDbType.Int32); cmd.Parameters.Add("@Username", MySqlDbType.VarChar, 50); cmd.Parameters["@Judul"].Value = judul; cmd.Parameters["@Deskripsi"].Value = deskripsi; cmd.Parameters["@Hasil"].Value = hasil_tani; cmd.Parameters["@Tgl"].Value = tgl_post; cmd.Parameters["@Foto"].Value = ImageData; cmd.Parameters["@Stok"].Value = Int32.Parse(stok); cmd.Parameters["@Lokasi"].Value = lokasi; cmd.Parameters["@Provinsi"].Value = provinsi; cmd.Parameters["@Kota"].Value = kota; cmd.Parameters["@Harga"].Value = Int32.Parse(harga); cmd.Parameters["@Username"].Value = fl.getusername(); con.Open(); int RowsAffected = cmd.ExecuteNonQuery(); if (RowsAffected > 0) { cek = "Data berhasil di tambah"; } con.Close(); } } catch (Exception ex) { cek = ex.ToString(); } return(cek); }
//Melakukan edit post public string ProcessEdited(string judul, string deskripsi, string hasil_tani, string foto, string stok, string lokasi, string provinsi, string kota, string harga, string idpost) { Form_Login fl = new Form_Login(); MySqlCommand cmd; FileStream fs; BinaryReader br; int RowsAffected = 0; string cek = ""; try { if (judul.Equals("") || deskripsi.Equals("") || stok.Equals("") || lokasi.Equals("") || harga.Equals("")) { cek = "Data harus terisi semua"; } if (foto.Length > 0) { string FileName = foto; byte[] ImageData; fs = new FileStream(FileName, FileMode.Open, FileAccess.Read); br = new BinaryReader(fs); ImageData = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); string CmdString = "update post set foto = @Foto where id = '" + idpost + "'"; cmd = new MySqlCommand(CmdString, con); cmd.Parameters.Add("@Foto", MySqlDbType.Blob); cmd.Parameters["@Foto"].Value = ImageData; con.Open(); RowsAffected = cmd.ExecuteNonQuery(); RowsAffected = 1; con.Close(); } if (judul.Length > 0) { string Cmd = "update post set judul=@Judul, deskripsi=@Deskripsi, " + "hasil_tani=@Hasil, stok=@Stok, lokasi=@Lokasi, provinsi=@Provinsi, " + "kota=@Kota, harga=@Harga, username=@Username where id='" + idpost + "'"; cmd = new MySqlCommand(Cmd, con); cmd.Parameters.Add("@Judul", MySqlDbType.VarChar, 50); cmd.Parameters.Add("@Deskripsi", MySqlDbType.VarChar, 255); cmd.Parameters.Add("@Hasil", MySqlDbType.Enum); cmd.Parameters.Add("@Stok", MySqlDbType.Int32); cmd.Parameters.Add("@Lokasi", MySqlDbType.VarChar, 100); cmd.Parameters.Add("@Provinsi", MySqlDbType.Enum); cmd.Parameters.Add("@Kota", MySqlDbType.Enum); cmd.Parameters.Add("@Harga", MySqlDbType.Int32); cmd.Parameters.Add("@Username", MySqlDbType.VarChar, 50); cmd.Parameters["@Judul"].Value = judul; cmd.Parameters["@Deskripsi"].Value = deskripsi; cmd.Parameters["@Hasil"].Value = hasil_tani; cmd.Parameters["@Stok"].Value = Int32.Parse(stok); cmd.Parameters["@Lokasi"].Value = lokasi; cmd.Parameters["@Provinsi"].Value = provinsi; cmd.Parameters["@Kota"].Value = kota; cmd.Parameters["@Harga"].Value = Int32.Parse(harga); cmd.Parameters["@Username"].Value = fl.getusername(); con.Open(); RowsAffected = cmd.ExecuteNonQuery(); RowsAffected = 1; con.Close(); } if (RowsAffected > 0) { cek = "Data berhasil di update"; } } catch (Exception ex) { cek = ex.ToString(); } return(cek); }