//删除影碟信息,返回影响行数。删除之前,首先查看那些关系表中是否还有该影碟的相关信息。 public int deleteVideoinfo(videoinfo v) { int recom = (int)SqlHelper.ExecuteScalar("select count(*) from recommend where videoid=@videoid", new SqlParameter("@videoid", v.videoid)); int rev = (int)SqlHelper.ExecuteScalar("select count(*) from review where videoid=@videoid", new SqlParameter("@videoid", v.videoid)); int hire = (int)SqlHelper.ExecuteScalar("select count(*) from hireinfo where videoid=@videoid", new SqlParameter("@videoid", v.videoid)); int total = recom + rev + hire; //若大于0,说明其他表还有引用,不能删除 if (total > 0) { return(-1); } else { //先把文件夹中的图片删除,再删除数据库中的数据 string delimage = (string)SqlHelper.ExecuteScalar("select imageurl from videoinfo where videoid=@videoid", new SqlParameter("@videoid", v.videoid)); string delimagepath = @"F:\VS2012 工作目录\Web应用\自做项目\video\video" + delimage; //如果图片存在,则删除;图片不存在,不用处理 if (IOHelper.Exists(delimagepath)) { IOHelper.DeleteFile(delimagepath); } else { } string sql = "delete from videoinfo where videoid=@videoid"; SqlParameter parm = new SqlParameter("@videoid", v.videoid); return(SqlHelper.ExecuteNonQuery(sql, parm)); } }
private void button1_Click(object sender, EventArgs e) { videoinfo v = new videoinfo(); v.videoid = this.label2.Text; v.videoname = this.textBox1.Text; v.number = TypeConversion.stringTOint(this.textBox2.Text); v.price = TypeConversion.stringTOdouble(this.textBox3.Text); v.buydate = TypeConversion.stringTOdatetime(this.label8.Text); v.describe = this.textBox6.Text; v.clickrate = TypeConversion.stringTOint(this.textBox4.Text); v.imageurl = imagepath; int flagReturn = bv.upVideoinfo(v); //返回值大于0,成功;等于0,保存失败;等于-1,上传路径中已经存在同名文件 if (flagReturn > 0) { MessageBox.Show("保存成功!"); this.Close(); } else if (flagReturn == 0) { MessageBox.Show("保存失败,请重试"); } else if (flagReturn < 0) { MessageBox.Show("上传路径中已经存在同名文件,请先修改文件名,然后重新选择!"); } }
private void button2_Click(object sender, EventArgs e) { videoinfo v = new videoinfo(); v.videoid = this.textBox1.Text; v.videoname = this.textBox2.Text; v.number = TypeConversion.stringTOint(this.textBox3.Text); v.price = TypeConversion.stringTOdouble(this.textBox4.Text); v.buydate = dateTimePicker1.Value; v.describe = this.textBox7.Text; v.clickrate = TypeConversion.stringTOint(this.textBox6.Text); v.imageurl = imagepath; //数据验证 //点击录入按钮时,对影碟编号和图片进行验证 if (this.textBox1.Text == "") { MessageBox.Show("影碟编号不能为空,请输入!"); } else { if (bv.countById(this.textBox1.Text) > 0) //影碟编号已经存在 { MessageBox.Show("影碟编号已经存在,不能重复录入!"); } else { if (this.textBox5.Text == "") { MessageBox.Show("影碟图片不能为空,请选择!"); } //符合条件,可以录入 else { int flagReturn = bv.addVideoinfo(v); //返回值大于0,成功;等于0,保存失败;等于-1,上传路径中已经存在同名文件 if (flagReturn > 0) { MessageBox.Show("录入成功!"); this.Close(); } else if (flagReturn == 0) { MessageBox.Show("录入失败,请重试"); } else if (flagReturn < 0) { MessageBox.Show("上传路径中已经存在同名文件,请先修改文件名,然后重新选择!"); } } } } }
//修改影碟信息,返回影响行数 public int updateVideoinfo(videoinfo v) { //对图片的路径需要特殊处理 string imagePath = v.imageurl; //由于数据库中图片的存储格式 为 "/images/003.jpg" (web的调用) ,书写方式和本地系统有点差距,但是实质一样。 string defaultPath = @"F:\VS2012 工作目录\Web应用\自做项目\video\video/images"; string defaultPath1 = @"F:\VS2012 工作目录\Web应用\自做项目\video\video\images"; //需要录入的图片就在默认路径中,只需把路径存储在数据库中,给web调用 if (Path.GetDirectoryName(imagePath) == defaultPath || Path.GetDirectoryName(imagePath) == defaultPath1) { v.imageurl = "/images/" + Path.GetFileName(imagePath); } //需要录入的图片不在默认路径中,需要自己复制 else { //把当前时间转换成string型,加上传递来的文件的后缀,充当新的文件名 //string newImageName=DateTime.Now.ToString("yyyyMMddHHmmss")+Path.GetExtension(imagePath); //string newImageFullName = @"F:\VS2012 工作目录\Web应用\自做项目\video\video\images\" + newImageName; //IOHelper.CreateFile(newImageFullName); //string imageContent = IOHelper.ReadFile(imagePath); //IOHelper.WriteFile(newImageFullName,imageContent); //首先判断该文件是否存在,若存在,返回-1 string newImageFullName = @"F:\VS2012 工作目录\Web应用\自做项目\video\video\images\" + Path.GetFileName(imagePath); if (IOHelper.Exists(newImageFullName)) { return(-1); } else { IOHelper.CopyFileToDirectory(imagePath, defaultPath1); v.imageurl = "/images/" + Path.GetFileName(imagePath); } } string sql = "update videoinfo set videoname=@videoname,number=@number,price=@price,buydate=@buydate,describe=@describe,imageurl=@imageurl,clickrate=@clickrate where videoid=@videoid"; SqlParameter[] parms = { new SqlParameter("@videoid", v.videoid), new SqlParameter("@videoname", v.videoname), new SqlParameter("@number", v.number), new SqlParameter("@price", v.price), new SqlParameter("@buydate", v.buydate), new SqlParameter("@describe", v.describe), new SqlParameter("@imageurl", v.imageurl), new SqlParameter("@clickrate", v.clickrate) }; return(SqlHelper.ExecuteNonQuery(sql, parms)); }
//添加影碟信息,返回受影响的行数 public int insertVideoinfo(videoinfo v) { //对图片的路径需要特殊处理 string imagePath = v.imageurl; //由于数据库中图片的存储格式 为 "/images/003.jpg" (web的调用) ,书写方式和本地系统有点差距,但是实质一样。 string defaultPath = @"F:\VS2012 工作目录\Web应用\自做项目\video\video/images"; string defaultPath1 = @"F:\VS2012 工作目录\Web应用\自做项目\video\video\images"; //需要录入的图片就在默认路径中,只需把路径存储在数据库中,给web调用 if (Path.GetDirectoryName(imagePath) == defaultPath || Path.GetDirectoryName(imagePath) == defaultPath1) { v.imageurl = "/images/" + Path.GetFileName(imagePath); } //需要录入的图片不在默认路径中,需要自己复制 else { //首先判断该文件是否存在,若存在,返回-1 string newImageFullName = @"F:\VS2012 工作目录\Web应用\自做项目\video\video\images\" + Path.GetFileName(imagePath); if (IOHelper.Exists(newImageFullName)) { return(-1); } else { IOHelper.CopyFileToDirectory(imagePath, defaultPath1); v.imageurl = "/images/" + Path.GetFileName(imagePath); } } string sql = "insert into videoinfo(videoid,videoname,number,price,buydate,describe,imageurl,clickrate) values(@videoid,@videoname,@number,@price,@buydate,@describe,@imageurl,@clickrate)"; SqlParameter[] parms = { new SqlParameter("@videoid", v.videoid), new SqlParameter("@videoname", v.videoname), new SqlParameter("@number", v.number), new SqlParameter("@price", v.price), new SqlParameter("@buydate", v.buydate), new SqlParameter("@describe", v.describe), new SqlParameter("@imageurl", v.imageurl), new SqlParameter("@clickrate", v.clickrate) }; return(SqlHelper.ExecuteNonQuery(sql, parms)); }
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { int clickingColumn = e.ColumnIndex; //点击-1—7列时,不响应 if (clickingColumn >= -1 && clickingColumn <= 7) { } else { DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex]; string content = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; if (column.HeaderText == "删除" && content == "删除") { if (MessageBox.Show("确定要删除该影碟吗?", "删除影碟", MessageBoxButtons.OKCancel) == DialogResult.OK) { //删除之前先查询,否则存在关系的不允许删除 videoinfo v = new videoinfo(); v.videoid = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(); int delflag = bv.delVideoinfo(v); if (delflag > 0) //删除成功 { MessageBox.Show("删除成功!"); dataGridView1.Rows.RemoveAt(e.RowIndex); } else if (delflag == 0) //删除失败 { MessageBox.Show("删除失败,请稍后重试!"); } else if (delflag == -1) //不让删除 { MessageBox.Show("该影碟还被引用,不能删除!"); } } } else if (column.HeaderText == "编辑" && content == "编辑") { string videoid = (string)dataGridView1.Rows[e.RowIndex].Cells[1].Value; new videoUpdate(videoid).Show(); } } }
//修改影碟信息,是否成功 public int upVideoinfo(videoinfo v) { return(dv.updateVideoinfo(v)); //返回值大于0,成功;等于0,保存失败;等于-1,上传路径中已经存在同名文件 }
//删除影碟信息,是否成功 public int delVideoinfo(videoinfo v) { return(dv.deleteVideoinfo(v)); //返回值大于0,成功;等于0,保存失败;等于-1,不能删除 }
//添加影碟信息,是否成功 public int addVideoinfo(videoinfo v) { return(dv.insertVideoinfo(v)); //返回值大于0,成功;等于0,保存失败;等于-1,上传路径中已经存在同名文件 }