柚子快報(bào)邀請(qǐng)碼778899分享:開(kāi)發(fā)語(yǔ)言 C#簡(jiǎn)單圖書管理系統(tǒng)
柚子快報(bào)邀請(qǐng)碼778899分享:開(kāi)發(fā)語(yǔ)言 C#簡(jiǎn)單圖書管理系統(tǒng)
摘要:
具有圖書管理員管理員登錄,用戶登錄,圖書添加、圖書刪除、圖書修改、借書、還書等功能
1、新建項(xiàng)目
1-1、首先創(chuàng)建數(shù)據(jù)庫(kù)連接類庫(kù),并命名,在類里寫入以下語(yǔ)句
1-1-1、//新建這幾個(gè)數(shù)據(jù)表
1-1-2、//下面是鏈接類庫(kù)代碼
? ? ? ?SqlConnection sc; ? ? ? ?public SqlConnection connect() ? ? ? ?{ ? ? ? ? ? ?string str = @"Data Source =DESKTOP-MQFHV47;Initial Catalog=BookDB;Integrated Security=True"; ? ? ? ? ? ?sc = new SqlConnection(str); ? ? ? ? ? ?sc.Open(); ? ? ? ? ? ?return sc; ? ? ? ?} ? ? ? ?public SqlCommand command(string sql) ? ? ? ?{ ? ? ? ? ? ?SqlCommand cmd = new SqlCommand(sql, connect()); ? ? ? ? ? ?return cmd; ? ? ? ?} ? ? ? ?public int Execute(string sql) ? ? ? ?{ ? ? ? ? ? ?return command(sql).ExecuteNonQuery(); ? ? ? ?} ? ? ? ?public SqlDataReader read(string sql) ? ? ? ?{ ? ? ? ? ? ?return command(sql).ExecuteReader(); ? ? ? ?} ? ? ? ?public void Daoclose() ? ? ? ?{ ? ? ? ? ? ?sc.Close(); ? ? ? ?}
1-2、接下來(lái)創(chuàng)建data數(shù)據(jù)類,這里存放用戶的ID和姓名
namespace project { ? ? internal class Data ? ? { ? ? ? ? public static string UID = "", UName = ""; ? ? } }
2、分別添加這些屬性的窗口,添加類型如上圖的窗口類型相同
2-1、登錄界面
3、分別布局窗口
3-1、admin1窗口,有menustrip控件和label控件
3-2、admin2窗口
3-3、admin21窗口
3-4、admin22窗口
3-5、user1窗口
3-6、user2窗口
3-7、user3窗口
4、之后在每個(gè)窗口控件上添加各項(xiàng)代碼
4-1、//admin窗口代碼
namespace project { ? ? public partial class admin1 : Form ? ? { ? ? ? ? public admin1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? }
? ? ? ? private void 圖書管理ToolStripMenuItem_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? admin2 admin= new admin2();? ? ? ? ? ? ? admin.ShowDialog(); ? ? ? ? }
? ? ? ? private void admin1_Load(object sender, EventArgs e) ? ? ? ? {
? ? ? ? } ? ? } }
4-2、//admin2代碼
? ? ? ? ? ? dao.Daoclose(); ? ? ? ? }
? ? ? ? public void TableName() ? ? ? ? { ? ? ? ? ? ? dataGridView1.Rows.Clear(); ? ? ? ? ? ? Dao dao = new Dao(); ? ? ? ? ? ? string sql = $"select*from t_book where name like'%{textBox2.Text}%'"; ? ? ? ? ? ? IDataReader dc = dao.read(sql); ? ? ? ? ? ? while (dc.Read()) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString()); ? ? ? ? ? ? } ? ? ? ? ? ? dc.Close(); ? ? ? ? ? ? dao.Daoclose(); ? ? ? ? }
? ? ? ? private void button3_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); ? ? ? ? ? ? ? ? label2.Text = id + dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); ? ? ? ? ? ? ? ? DialogResult dr = MessageBox.Show("確認(rèn)刪除嗎?","信息提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question); ? ? ? ? ? ? ? ? if (dr == DialogResult.OK) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? string sql = $"delete from t_book where id= '{id}' "; ? ? ? ? ? ? ? ? ? ? Dao dao = new Dao(); ? ? ? ? ? ? ? ? ? ? if (dao.Execute(sql) > 0) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("刪除成功"); ? ? ? ? ? ? ? ? ? ? ? ? Table(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("刪除失敗"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? dao.Daoclose(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("請(qǐng)先在表格中選擇要?jiǎng)h除的圖書記錄!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error); ? ? ? ? ? ? } ? ? ? ? }
? ? ? ? private void dataGridView1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? label2.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString()+ " ? " ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? +dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); ? ? ? ? }
? ? ? ? private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? admin21 a= new admin21(); ? ? ? ? ? ? a.ShowDialog();? ? ? ? ? }
? ? ? ? private void button2_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); ? ? ? ? ? ? ? ? string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); ? ? ? ? ? ? ? ? string author = dataGridView1.SelectedRows[0].Cells[2].Value.ToString(); ? ? ? ? ? ? ? ? string press = dataGridView1.SelectedRows[0].Cells[3].Value.ToString(); ? ? ? ? ? ? ? ? string number = dataGridView1.SelectedRows[0].Cells[4].Value.ToString(); ? ? ? ? ? ? ? ? admin22 admin=new admin22(id,name,author,press,number); ? ? ? ? ? ? ? ? admin.ShowDialog(); ? ? ? ? ? ? ? ? Table();//刷新數(shù)據(jù) ? ? ? ? ? ? } ? ? ? ? ? ? catch? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("ERROR"); ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? }
? ? ? ? private void button5_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? TableID(); ? ? ? ? }
? ? ? ? private void button6_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? TableName(); ? ? ? ? }
? ? ? ? private void button4_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? Table(); ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? textBox2.Text = ""; ? ? ? ? } ? ? } } ?
4-3、//admin21代碼
namespace project { ? ? public partial class admin21 : Form ? ? { ? ? ? ? public admin21() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? }
? ? ? ? private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "" ) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Dao dao = new Dao(); ? ? ? ? ? ? ? ? string sql = ? ? ? ? ? ? ? ? ? ? $"insert into t_book values( '{textBox1.Text}','{textBox2.Text}','{textBox3.Text}','{textBox4.Text}',{textBox5.Text})"; ? ? ? ? ? ? ? ? dao.Execute(sql); ? ? ? ? ? ? ? ? int n = dao.Execute(sql); ? ? ? ? ? ? ? ? if (n > 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show("添加成功"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show("添加失敗"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? textBox2.Text = ""; ? ? ? ? ? ? ? ? textBox3.Text = ""; ? ? ? ? ? ? ? ? textBox4.Text = ""; ? ? ? ? ? ? ? ? textBox5.Text = ""; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("輸入不能有空項(xiàng)"); ? ? ? ? ? ? } ? ? ? ? }
? ? ? ? private void button2_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? textBox2.Text = ""; ? ? ? ? ? ? textBox3.Text = ""; ? ? ? ? ? ? textBox4.Text = ""; ? ? ? ? ? ? textBox5.Text = ""; ? ? ? ? }
? ? ? ? private void admin21_Load(object sender, EventArgs e) ? ? ? ? {
? ? ? ? } ? ? } }
4-4、//admin22代碼
namespace project { ? ? public partial class admin22 : Form ? ? { ? ? ? ? string ID = ""; ? ? ? ? public admin22() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? public admin22(string id,string name,string author,string press,string number) ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? ? ? ID=textBox1.Text = id; ? ? ? ? ? ? textBox2.Text = name; ? ? ? ? ? ? textBox3.Text = author; ? ? ? ? ? ? textBox4.Text = press; ? ? ? ? ? ? textBox5.Text = number; ? ? ? ? }
? ? ? ? private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? string sql = $"update t_book set id='{textBox1.Text}',[name]='{textBox2.Text}'," + ? ? ? ? ? ? ? ? $"author='{textBox3.Text}',press='{textBox4.Text}',number={textBox5.Text} where id='{ID}'"; ? ? ? ? ? ? Dao dao=new Dao(); ? ? ? ? ? ? if (dao.Execute(sql) > 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("修改成功"); ? ? ? ? ? ? ? ? this.Close(); ? ? ? ? ? ? } ? ? ? ? }
? ? ? ? private void admin22_Load(object sender, EventArgs e) ? ? ? ? {
? ? ? ? } ? ? } }
4-5、//Login的代碼
namespace project { ? ? public partial class Login : Form ? ? { ? ? ? ? public Login() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? }
? ? ? ? private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (textBox1.Text != "" && textBox2.Text != "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? login(); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("輸入有空項(xiàng),請(qǐng)重新輸入"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? public void login() ? ? ? ? { ? ? ? ? ? ? if (radioButtonUser.Checked == true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Dao dao = new Dao(); ? ? ? ? ? ? ? ? string sql = String.Format("select*from t_user where id= '{0}' and paw='{1}'", textBox1.Text, textBox2.Text); ? ? ? ? ? ? ? ? IDataReader dc = dao.read(sql);
? ? ? ? ? ? ? ? //MessageBox.Show(dc[0].ToString() + " ? ?"+dc["name"].ToString()); ? ? ? ? ? ? ? ? if (dc.Read()) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Data.UID = dc["id"].ToString(); ? ? ? ? ? ? ? ? ? ? Data.UName = dc["name"].ToString(); ? ? ? ? ? ? ? ? ? ? MessageBox.Show("登陸成功"); ? ? ? ? ? ? ? ? ? ? user1 user = new user1(); ? ? ? ? ? ? ? ? ? ? this.Hide(); ? ? ? ? ? ? ? ? ? ? user.ShowDialog(); ? ? ? ? ? ? ? ? ? ? this.Show(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show("登錄失敗"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? dao.Daoclose(); ? ? ? ? ? ? } ? ? ? ? ? ? if (radioButtonAdmin.Checked == true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Dao dao = new Dao(); ? ? ? ? ? ? ? ? string sql = String.Format("select*from t_admin where id= '{0}' and paw='{1}'", textBox1.Text, textBox2.Text); ? ? ? ? ? ? ? ? IDataReader dc = dao.read(sql);
? ? ? ? ? ? ? ? //MessageBox.Show(dc[0].ToString() + " ? ?"+dc["name"].ToString()); ? ? ? ? ? ? ? ? if (dc.Read()) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show("登陸成功"); ? ? ? ? ? ? ? ? ? ? admin1 a = new admin1(); ? ? ? ? ? ? ? ? ? ? this.Hide(); ? ? ? ? ? ? ? ? ? ? a.ShowDialog(); ? ? ? ? ? ? ? ? ? ? this.Show(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show("登錄失敗"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? dao.Daoclose(); ? ? ? ? ? ? } ? ? ? ? }
? ? ? ? private void Login_Load(object sender, EventArgs e) ? ? ? ? {
? ? ? ? } ? ? } }
4-5、//user1代碼
namespace project { ? ? public partial class user1 : Form ? ? { ? ? ? ? public user1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? ? ? label1.Text = $"歡迎{Data.UName}登錄"; ? ? ? ? }
? ? ? ? private void 圖書查看和借閱ToolStripMenuItem_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? user2 user2 = new user2(); ? ? ? ? ? ? user2.ShowDialog(); ? ? ? ? }
? ? ? ? private void user1_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //admin2 admin = new admin2();
? ? ? ? }
? ? ? ? private void 當(dāng)前借出圖書和歸還ToolStripMenuItem_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? user3 user3 = new user3(); ? ? ? ? ? ? user3.ShowDialog(); ? ? ? ? } ? ? } }
? ? ?4-6、//user2代碼
namespace project { ? ? public partial class user2 : Form ? ? { ? ? ? ? public user2() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? ? ? Table(); ? ? ? ? }
? ? ? ? private void user2_Load(object sender, EventArgs e) ? ? ? ? {
? ? ? ? } ? ? ? ? public void Table() ? ? ? ? { ? ? ? ? ? ? dataGridView1.Rows.Clear(); ? ? ? ? ? ? Dao dao = new Dao(); ? ? ? ? ? ? string sql = "select*from t_book"; ? ? ? ? ? ? IDataReader dc = dao.read(sql); ? ? ? ? ? ? while (dc.Read()) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString()); ? ? ? ? ? ? } ? ? ? ? ? ? dc.Close(); ? ? ? ? ? ? dao.Daoclose(); ? ? ? ? }
? ? ? ? private void 借出圖書_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); ? ? ? ? ? ? int number = int.Parse(dataGridView1.SelectedRows[0].Cells[4].Value.ToString()); ? ? ? ? ? ? if (number < 1) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("庫(kù)存不足,請(qǐng)聯(lián)系管理員購(gòu)入"); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? string sql = $"insert into t_lend([uid],bid,[datetime]) values('{Data.UID}'," + ? ? ? ? ? ? ? ? ? ? $"'{id}',getdate());update t_book set number=number-1 where id='{id}'"; ? ? ? ? ? ? ? ? Dao dao = new Dao(); ? ? ? ? ? ? ? ? if (dao.Execute(sql) > 1) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show($"用戶{Data.UName}借出了圖書{ id}!"); ? ? ? ? ? ? ? ? ? ? Table(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } }
4-8、//user3代碼
namespace project { ? ? public partial class user3 : Form ? ? { ? ? ? ? public user3() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? ? ? Table(); ? ? ? ? }
? ? ? ? public void Table() ? ? ? ? { ? ? ? ? ? ? dataGridView1.Rows.Clear(); ? ? ? ? ? ? Dao dao = new Dao(); ? ? ? ? ? ? string sql = $"select [no],[bid],[datetime] from t_lend where [uid]='{Data.UID}'"; ? ? ? ? ? ? IDataReader dc = dao.read(sql); ? ? ? ? ? ? while (dc.Read()) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString()); ? ? ? ? ? ? } ? ? ? ? ? ? dc.Close(); ? ? ? ? ? ? dao.Daoclose(); ? ? ? ? } ? ? ? ? private void user3_Load(object sender, EventArgs e) ? ? ? ? {
? ? ? ? }
? ? ? ? private void 借出圖書_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? string no= dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); ? ? ? ? ? ? string id= dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); ? ? ? ? ? ? string sql = $"delete from t_lend where[no] = {no}; update t_book set number = number + 1 where id = '{id}'";? ? ? ? ? ? ? Dao dao = new Dao(); ? ? ? ? ? ? if (dao.Execute(sql) > 1) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("歸還成功"); ? ? ? ? ? ? ? ? Table(); ? ? ? ? ? ? } ? ? ? ? } ? ? } }
5、總結(jié)
????????寫這篇文章是為了記錄一下我自己入門C#的第一個(gè)入門項(xiàng)目,從開(kāi)始的一點(diǎn)不懂,做完之后能懂得一些簡(jiǎn)簡(jiǎn)單單的語(yǔ)句布局控件之類的,前前后后經(jīng)歷了差不多一周的時(shí)間,光是弄SQL Server就弄了兩三天的時(shí)間,當(dāng)然是斷斷續(xù)續(xù)的兩三天時(shí)間,并不是從早到晚的,在自己本來(lái)的電腦上一直都連接不上數(shù)據(jù)庫(kù),一直提示什么鬼的添加了C++程序,刪了好多和C++有關(guān)的程序和軟件就是裝不上,找了很久都不知道什么鬼原因,后來(lái)猜測(cè)可能是不兼容Win11吧(因?yàn)槲規(guī)讉€(gè)同學(xué)的就是W10的系統(tǒng),都裝上了),就把另外一臺(tái)學(xué)校的電腦格式化成了W10系統(tǒng)就可以連接上了。
需要源碼或者需要解答的可以留言哈。
6、感謝欄
感謝B站的UP主:面朝星海我心澎湃
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? UID124790846
我就是從他的視頻開(kāi)始入門的,講的非常的好,很詳細(xì),特別的適合小白和想入門C#的小伙伴學(xué)習(xí)。
代碼連接如下:
鏈接:https://pan.baidu.com/s/1jo4By8jx-zY7ySsbInqnTw?pwd=1mko? 提取碼:1mko
后續(xù)我會(huì)更新一個(gè)書店售賣系統(tǒng),大家可以支持支持哈哈。
柚子快報(bào)邀請(qǐng)碼778899分享:開(kāi)發(fā)語(yǔ)言 C#簡(jiǎn)單圖書管理系統(tǒng)
推薦鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。