python图书管理系统增删改查_图书管理系统的增删改查以及本地文件存储

usingLibraryProDemo.Models;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Runtime.Serialization.Formatters.Binary;namespaceLibraryProDemo

{public partial classFrmManageBook : Form

{//定义一个保存图书对象的容器

private List bookList = new List();publicFrmManageBook()

{

InitializeComponent();//禁止dgv自动生成列

this.dgvBookList.AutoGenerateColumns = false;//初始化出版社下拉框

InitPublisher();

}private voidInitPublisher()

{//实践开发中应该从数据库或其他数据源中获取数据(现阶段在集合中模拟)

List publisherList = new List()

{new Pulisher(){PublisherId=1,PiblisherName="北京大学出版社"},new Pulisher(){PublisherId=2,PiblisherName="南京大学出版社"},new Pulisher(){PublisherId=3,PiblisherName="天津大学出版社"},new Pulisher(){PublisherId=4,PiblisherName="广东大学出版社"},new Pulisher(){PublisherId=5,PiblisherName="广州大学出版社"},new Pulisher(){PublisherId=6,PiblisherName="呵呵大学出版社"},

};//给出版社下拉框设置数据源

this.cboPublisher.DataSource =publisherList;this.cboPublisher.DisplayMember = "PiblisherName";this.cboPublisher.ValueMember = "PublisherId";this.cboPublisher.SelectedIndex = -1;

}//添加图书

private void btnAdd_Click(objectsender, EventArgs e)

{//1、数据验证(非空验证、其他合法性验证…)

if(this.txtBookName.Text.Trim().Length==0)

{

MessageBox.Show("请输入图书名称!", "验证提示");this.txtBookName.Focus();return;

}//2、封装对象(将用户输入的内容转换为对象)

Book objBook = newBook()

{

Author= this.txtAuthor.Text.Trim(),

BarCode= this.txtBarCode.Text.Trim(),

BookName= this.txtBookName.Text.Trim(),

PublishDate= Convert.ToDateTime(this.dtpPublishDate.Text),

UnitPrice= Convert.ToDouble(this.txtUnitPrice.Text.Trim()),

PublisherId= Convert.ToInt32(this.cboPublisher.SelectedValue)

};//3、将对象保存到数据源(多数情况是数据库、也可以是其他数据源)//3、同步显示添加的对象

this.bookList.Add(objBook);this.dgvBookList.DataSource = null;this.dgvBookList.DataSource=this.bookList;//4、将对象保存到文件中

FileStream fs = new FileStream("bookList.obj",FileMode.Create);

BinaryFormatter bf= new BinaryFormatter();//创建二进制格式化器

bf.Serialize(fs, this.bookList);//将当前集合数据序列化到文件

fs.Close();//关闭文件流//5、清空用户输入

this.txtAuthor.Clear();this.txtBarCode.Text="";this.txtBookName.Clear();this.txtUnitPrice.Clear();this.cboPublisher.SelectedValue = -1;this.txtBookName.Focus();

}//删除图书

private void btnDel_Click(objectsender, EventArgs e)

{//如果当前没有任何数据,则不执行任何操作

if (this.dgvBookList.CurrentRow == null || this.dgvBookList.RowCount == 0) return;//1、删除确认

DialogResult result = MessageBox.Show("确认要删除当前图书吗?","删除询问",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);if (result == DialogResult.Cancel) return;//2、获取要修改的图书信息(对象)

string barCode = this.dgvBookList.CurrentRow.Cells["BarCode"].Value.ToString();

Book objBook= (from b in this.bookList where b.BarCode.ToString().Equals(barCode) select b).First();//3、从数据库和集合中删除对象(数据源中删除暂时省略)

this.bookList.Remove(objBook);//同步更新显示

this.dgvBookList.DataSource = null;this.dgvBookList.DataSource = this.bookList;

}//修改图书

private void btnEdit_Click(objectsender, EventArgs e)

{//如果当前没有任何数据,则不执行任何操作

if (this.dgvBookList.CurrentRow == null || this.dgvBookList.RowCount == 0) return;//1、获取要修改的图书信息(对象)

string barCode = this.dgvBookList.CurrentRow.Cells["BarCode"].Value.ToString();

Book objBook=(from b in this.bookList where b.BarCode.ToString().Equals(barCode)select b).First();//2、显示要修改的窗体(把图书信息显示在修改窗体中)

FrmEditBook objEdit = newFrmEditBook(objBook);

DialogResult result=objEdit.ShowDialog();//3、根据修改是否成功来决定是否同步显示

if(result==DialogResult.OK)

{//首先获取修改后的对象

objBook =(Book)objEdit.Tag;//重新找到要修改的图书对象并修改属性值

Book editBook = (from b in this.bookList where b.BarCode.ToString().Equals(barCode) select b).First();

editBook.BookName=objBook.BookName;

editBook.Author=objBook.Author;

editBook.PublishDate=objBook.PublishDate;

editBook.UnitPrice=objBook.UnitPrice;

editBook.PublisherId=objBook.PublisherId;//同步刷新

this.dgvBookList.Refresh();

}

}//查询图书

private void btnQueryBook_Click(objectsender, EventArgs e)

{//从数据源中获取全部图书(返回List集合)

this.dgvBookList.DataSource = this.bookList;if (!File.Exists("bookList.obj")) return;//从文件中获取全部图书

FileStream fs = new FileStream("bookList.obj", FileMode.Open);

BinaryFormatter bf= newBinaryFormatter();this.bookList=(List)bf.Deserialize(fs);

fs.Close();this.dgvBookList.DataSource=this.bookList;

}//清除显示

private void btnClear_Click(objectsender, EventArgs e)

{this.bookList.Clear();this.dgvBookList.DataSource = null;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值