您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“C#如何實現圖書管理系統”,內容詳細,步驟清晰,細節處理妥當,希望這篇“C#如何實現圖書管理系統”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
通過模擬圖書管理系統,實現以下功能
學生賬號的注冊
學生對館藏圖書狀況的查詢
學生借書,還書狀態的查詢
學生借書以及歸還圖書
學生欠款的查詢
數據庫主要包括:
學生表:學生學號,學生姓名,學生性別,學生類型,學生密碼
圖書表:圖書編號,圖書名,圖書出版社,圖書作者,圖書狀態(1代表可以借,0代表不可以,)圖書類型,圖書出版日期
借書表:學生號,圖書號,圖書名,借出日期
還書表:學生號,圖書號,圖書名,歸還日期
欠費金額表:學生號,圖書號,圖書名,欠費金額(超過30天每天收0.3元)
VS2013,SQLSERVER2014
在客戶端與數據庫之間增加了一個“中間層”,分為表示層,業務邏輯層,數據訪問層
表示層:為客戶提供對應程序的訪問
業務邏輯層:以類庫的形式為表示層服務
數據訪問層:實現整個系統的數據庫連接,數據庫存取操作,以組件類庫的形式為業務邏輯層提供服務
數據庫的建立:
create database MyLibrary on (name=Mylibrary_data1, filename='d:\Mylibrary_data1.mdf', size=10, maxsize=50, filegrowth=4) log on (name=Mylibrary_log1, filename='d:\Mylibrary_log1.ldf', size=10MB, maxsize=20MB, filegrowth=2MB) use MyLibrary; create table reader( readerid varchar(20) primary key, readername varchar(20) not null, readersex varchar(20) not null, readertype varchar(20) not null, readerpd varchar(20) not null ) create table book( bookid varchar(20) primary key, booktype varchar(20) not null, bookauthor varchar(20) not null, bookname varchar(20) not null, isbrrowed varchar(20) not null, booklibrary varchar(20) not null, bookpubdate datetime ) create table brrow_data( readerid varchar(20) primary key, bookid varchar(20) , bookname varchar(20), brrowdate datetime foreign key (readerid) references reader(readerid), foreign key (bookid) references book(bookid) ) create table return_record( readerid varchar (20) primary key, bookid varchar(20), bookname varchar(20), returndate datetime, foreign key (readerid) references reader(readerid), foreign key (bookid) references book(bookid) ) create table readerfee( readerid varchar(20) primary key, bookid varchar(20), bookname varchar(20), readerpay varchar(20), foreign key (readerid) references reader(readerid), foreign key (bookid) references book(bookid) ) --已有圖書插如表中 insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921000','縹緲之旅','修真','小豬','仙王出版社','2005-09-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921001','唐朝好男人','穿越','多一半','新星出版社','2005-07-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921002','鬼吹燈','恐怖','天下霸唱','安徽文藝出版社','2005-09-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921003','和空姐同居的日子','都市','海王','中國海關出版社','2005-08-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921004','卡圖','科幻','方向','廣西人民出版社','2005-06-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921005','蜀山劍俠傳','仙俠','還珠樓主','陜西人民出版社','2005-04-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921006','何以笙簫默','言情','顧漫','朝華出版社','2005-05-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921007','步步驚心','穿越','童話','民族出版社','2005-12-01','否'); --將讀者插如表中 insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007000','李磊','男','學生','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007001','棠張僧','男','學生','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007002','韓梅梅','女','學生','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007003','路西','女','學生','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007004','王強','男','學生','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007005','馬正標','男','管理','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007006','盧海鵬','男','管理','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007007','李艷玲','女','管理','000000'); --查詢功能 select *from reader; select *from book; select *from brrow_data; select *from return_record; select *from readerfee; --刪除表 drop table reader; drop table book; drop table brrow_data; drop table return_record; drop table readerfee;
登錄界面的編輯
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace Library { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void panel1_Paint(object sender, PaintEventArgs e) { } //登錄按鈕 private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); this.Hide(); form2.Show(); mycon.Close(); //sql = string.Format("select count(*) from reader where readerid='{0}'",textBox1.Text); //SqlCommand com = new SqlCommand(sql,mycon); //int count = (int)com.ExecuteScalar(); //if (count==1) //{ // sql = string.Format("select count(*) from reader where readerpd='{0}'",textBox2.Text); // com = new SqlCommand(sql, mycon); // count = (int)com.ExecuteScalar(); // if (count>=1) // { // if (comboBox1.Text=="學生") // { // Form2 form2 = new Form2(); // this.Hide(); // form2.Show(); form2.readerid = textBox1.Text; // mycon.Close(); // } // else if (comboBox1.Text=="管理員") // { // Form4 form4 = new Form4(); // this.Hide(); // form4.Show(); // mycon.Close(); // } // else // { // MessageBox.Show("請選擇您的身份!"); // } // } // else // { // MessageBox.Show("密碼錯誤,請重新輸入!"); // } //} //else{ // MessageBox.Show("賬號不存在,請重新輸入!"); //} } string sql, con; SqlConnection mycon; //加載事件 private void Form1_Load(object sender, EventArgs e) { con = "Server=.;Database=MyLibrary;Trusted_Connection=SSPI"; mycon = new SqlConnection(con); mycon.Open(); } //注冊 private void button2_Click(object sender, EventArgs e) { Form3 form3 = new Form3(); this.Hide(); form3.Show(); } } }
注冊界面的編輯
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace Library { public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void label3_Click(object sender, EventArgs e) { } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } //提交申請 private void button1_Click(object sender, EventArgs e) { sql = string.Format("select count(*) from reader where readerpd='{0}'", textBox2.Text); SqlCommand com = new SqlCommand(sql, mycon); int count = (int)com.ExecuteScalar(); if (count==1) { MessageBox.Show("抱歉,該賬號已存在!"); } else { if (textBox1.Text==""||textBox2.Text==""||textBox4.Text==""||comboBox1.Text==""||textBox3.Text=="") { MessageBox.Show("請將信息填寫完整!"); } else { Form1 form1 = new Form1(); form1.Show(); this.Hide(); } } } string sql, con; SqlConnection mycon; private void Form3_Load(object sender, EventArgs e) { con = "Server=.;Database=MyLibrary;Trusted_Connection=SSPI"; mycon = new SqlConnection(con); mycon.Open(); } } }
學生管理界面的編輯
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace Library { public partial class Form2 : Form { public Form2() { InitializeComponent(); } public string readerid; string sql, con; SqlConnection mycon; SqlCommand com; //加載事件 private void Form2_Load(object sender, EventArgs e) { label1.Hide(); textBox1.Hide(); button4.Hide(); dataGridView1.Hide(); con = "Server=.;Database=MyLibrary;Trusted_Connection=SSPI"; mycon = new SqlConnection(con); mycon.Open(); } //個人信息 private void button1_Click(object sender, EventArgs e) { dataGridView1.Show(); label1.Hide(); textBox1.Hide(); button4.Hide(); sql = string.Format("select * from reader where readerid='{0}'", readerid); SqlDataAdapter mydata = new SqlDataAdapter(sql, con); DataSet myds = new DataSet(); mydata.Fill(myds, "reader"); dataGridView1.DataSource = myds.Tables["reader"]; } //圖書信息 private void button2_Click(object sender, EventArgs e) { dataGridView1.Show(); label1.Hide(); textBox1.Hide(); button4.Hide(); sql = "select * from book"; SqlDataAdapter mydata = new SqlDataAdapter(sql, con); DataSet myds = new DataSet(); mydata.Fill(myds, "book"); dataGridView1.DataSource = myds.Tables["book"]; } //繳費系統 private void button3_Click(object sender, EventArgs e) { try { sql = string.Format("insert into readerfee(readerid,bookid,bookname,readerpay)select readerid ,bookid , bookname,0.03*(datediff(day,convert(smalldatetime,brrowdate),getdate())-30) from brrow_data where datediff(day,convert(smalldatetime,brrowdate),getdate())>=0;"); com = new SqlCommand(); com.Connection = mycon; com.CommandType = CommandType.Text; com.CommandText = sql; SqlDataReader dr = com.ExecuteReader(); dr.Close(); } catch { } sql = string.Format("select * from readerfee where readerid='{0}'", readerid); SqlDataAdapter mydata = new SqlDataAdapter(sql, con); DataSet myds = new DataSet(); mydata.Fill(myds, "readerfee"); dataGridView1.DataSource = myds.Tables["readerfee"]; } //借還記錄 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { dataGridView1.Show(); label1.Hide(); textBox1.Hide(); button4.Hide(); if (comboBox1.Text == "借書記錄") { sql = string.Format("select * from brrow_data where readerid='{0}'", readerid); SqlDataAdapter mydata = new SqlDataAdapter(sql, con); DataSet myds = new DataSet(); mydata.Fill(myds, "reader"); dataGridView1.DataSource = myds.Tables["reader"]; } else { sql = string.Format("select * from return_record where readerid='{0}'", readerid); SqlDataAdapter mydata = new SqlDataAdapter(sql, con); DataSet myds = new DataSet(); mydata.Fill(myds, "reader"); dataGridView1.DataSource = myds.Tables["reader"]; } } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { dataGridView1.Hide(); if (comboBox2.Text == "我要借書") { button4.Show(); button4.Text = "借閱本書"; label1.Text = "搜索書名"; label1.Show(); textBox1.Show(); } else if (comboBox2.Text == "我要還書") { button4.Show(); button4.Text = "歸還本書"; label1.Text = "所借書號"; label1.Show(); textBox1.Show(); } } private void button4_Click(object sender, EventArgs e) { if (button4.Text == "借閱本書") { sql = string.Format("select count(*) from book where bookname='{0}'", textBox1.Text); com = new SqlCommand(sql, mycon); int count = (int)com.ExecuteScalar(); if (count >= 1) { sql = string.Format("select bookid from book where bookname='{0}'", textBox1.Text); com = new SqlCommand(sql, mycon); string bookid = (string)com.ExecuteScalar(); sql = string.Format("insert into brrow_data (readerid,bookid,bookname,brrowdate) values('{0}','{1}','{2}','{3}')", readerid, bookid, textBox1.Text, DateTime.Now); com = new SqlCommand(); com.Connection = mycon; com.CommandType = CommandType.Text; com.CommandText = sql; SqlDataReader dr = com.ExecuteReader(); dr.Close(); MessageBox.Show("借書成功!"); } else MessageBox.Show("抱歉,不存在本書!"); } else if (button4.Text == "歸還本書") { sql = string.Format("select count(*) from brrow_data where bookid='{0}'", textBox1.Text); com = new SqlCommand(sql, mycon); int count = (int)com.ExecuteScalar(); if (count >= 1) { sql = string.Format("select bookname from brrow_data where bookid='{0}'", textBox1.Text); com = new SqlCommand(sql, mycon); string bookname = (string)com.ExecuteScalar(); sql = string.Format("insert into return_record(readerid,bookid,bookname,returndate) values ('{0}','{1}','{2}','{3}')", readerid, textBox1.Text, bookname, DateTime.Now); com = new SqlCommand(); com.Connection = mycon; com.CommandType = CommandType.Text; com.CommandText = sql; SqlDataReader dr = com.ExecuteReader(); dr.Close(); sql = string.Format("delete from brrow_data where bookid='{0}'", textBox1.Text); com = new SqlCommand(); com.Connection = mycon; com.CommandType = CommandType.Text; com.CommandText = sql; SqlDataReader dr1 = com.ExecuteReader(); dr1.Close(); MessageBox.Show(bookname + "還書成功!"); } else { MessageBox.Show("抱歉!您沒有借這本書!"); } } } } }
讀到這里,這篇“C#如何實現圖書管理系統”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。