设为首页 收藏本站
查看: 990|回复: 0

[经验分享] 学院派福利——C#+SQL Server图书管理系统

[复制链接]

尚未签到

发表于 2017-7-13 18:49:57 | 显示全部楼层 |阅读模式
  这是sql server的第七、八次上机内容,抽了几天时间给做了
  在原有的booksDB库中加了一个Admin表:UserName:root,PassWord:123456.
  环境:Visual Studio 2010,SQL Server 2008
  参考书籍:
DSC0000.jpg

  项目结构:
DSC0001.png

DSC0002.png




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;
namespace WindowsFormsApplication2
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click_1(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{   
string User = UserName.Text;
string Pwd = PassWord.Text;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=.;database=booksDB;integrated security=True";
try
{
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select * from Admin where UserName='" + User + "'";
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
{
string password = reader.GetString(reader.GetOrdinal("PassWord"));
if (Pwd == password)
{
MessageBox.Show("登陆成功!");
new Thread(() => Application.Run(new Menu())).Start();
this.Close();
//this.Hide();
//Form M = new Management();
//M.Show();
                    }
else
{
MessageBox.Show("密码错误!");
UserName.Text = " ";
PassWord.Text = " ";
}
}
else
{
MessageBox.Show("用户不存在!");
UserName.Text = " ";
PassWord.Text = " ";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错");
}
finally
{
conn.Close();
}
}
}
}
DSC0003.png




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
new ReaderTypeM().Show();
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
new BookM().Show();
}
private void button3_Click(object sender, EventArgs e)
{
this.Hide();
new ReaderM().Show();
}
private void button4_Click(object sender, EventArgs e)
{
this.Hide();
new BorrowAndReturn().Show();
}
private void Menu_Load(object sender, EventArgs e)
{
}
private void Out_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
  连数据库前先用BindingSource控件绑定数据库数据。再用DataGridView控件展示数据。
DSC0004.png




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
public partial class ReaderTypeM : Form
{
public ReaderTypeM()
{
InitializeComponent();
}
private void BackMenu_Click(object sender, EventArgs e)
{
this.Hide();
new Menu().Show();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "insert into ReaderType values(@rdType,@rdTypeName,@canLendQty,@canLendDay)";
comm.Parameters.AddWithValue("@rdType", txtrdType.Text);
comm.Parameters.AddWithValue("@rdTypeName", txtrdTypeName.Text);
comm.Parameters.AddWithValue("@canLendQty", txtcanLendQty.Text);
comm.Parameters.AddWithValue("@canLendDay", txtcanLendDay.Text);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("插入成功!");
DataBind();
}
catch (Exception ex)
{
MessageBox.Show("插入失败!" + ex.Message);
}
}
private void Search_Click(object sender, EventArgs e)
{
DataBind();
}
private void Delete_Click(object sender, EventArgs e)
{
MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
DialogResult dr = MessageBox.Show("确定要删除吗?", "确定", messButton);
if (dr == DialogResult.OK)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "delete from ReaderType where rdType = @rdType";
comm.Parameters.AddWithValue("@rdType", txtrdType.Text);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("删除成功!");
DataBind();
}
catch (Exception ex)
{
MessageBox.Show("删除失败!" + ex.Message);
}
}
}
private void Alter_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "update ReaderType set rdType=@rdType,rdTypeName=@rdTypeName,canLendQty=@canLendQty,canLendDay=@canLendDay where rdType=@rdType";
comm.Parameters.AddWithValue("@rdType", txtrdType.Text);
comm.Parameters.AddWithValue("@rdTypeName", txtrdTypeName.Text);
comm.Parameters.AddWithValue("@canLendQty", txtcanLendQty.Text);
comm.Parameters.AddWithValue("@canLendDay", txtcanLendDay.Text);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("更新成功!");
DataBind();
}
catch (Exception ex)
{
MessageBox.Show("更新失败!" + ex.Message);
}
}
private void DataBind()
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select rdType 类别号,rdTypeName 类别名称,canLendQty 可借数量,canLendDay 可借天数 from ReaderType";
SqlDataAdapter sda = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
sda.Fill(ds);
dgvBook.DataSource = ds.Tables[0];
txtrdType.DataBindings.Clear();
txtrdTypeName.DataBindings.Clear();
txtcanLendQty.DataBindings.Clear();
txtcanLendDay.DataBindings.Clear();
txtrdType.DataBindings.Add("Text", ds.Tables[0], "类别号");
txtrdTypeName.DataBindings.Add("Text", ds.Tables[0], "类别名称");
txtcanLendQty.DataBindings.Add("Text", ds.Tables[0], "可借数量");
txtcanLendDay.DataBindings.Add("Text", ds.Tables[0], "可借天数");
conn.Close();
}
private void ReaderTypeM_Load(object sender, EventArgs e)
{
DataBind();
}
private void bindingSource1_CurrentChanged(object sender, EventArgs e)
{
}
}
}
DSC0005.png




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
public partial class BookM : Form
{
public BookM()
{
InitializeComponent();
}
private void bindingSource1_CurrentChanged(object sender, EventArgs e)
{
}
private void BackMenu_Click(object sender, EventArgs e)
{
this.Hide();
new Menu().Show();
}
private void Add_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "insert into Book values(@bkID,@bkName,@bkAuthor,@bkPress,@bkPrice,@bkStatus)";
comm.Parameters.AddWithValue("@bkID", txtbkID.Text);
comm.Parameters.AddWithValue("@bkName", txtbkName.Text);
comm.Parameters.AddWithValue("@bkAuthor", txtbkAuthor.Text);
comm.Parameters.AddWithValue("@bkPress", txtbkPress.Text);
comm.Parameters.AddWithValue("@bkPrice", txtbkPrice.Text);
comm.Parameters.AddWithValue("@bkStatus", txtbkStatus.Text);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("插入成功!");
DataBind();
}
catch (Exception ex)
{
MessageBox.Show("插入失败!" + ex.Message);
}
}
private void Search_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select bkID 书号,bkName 书名,bkAuthor 作者,bkPress 出版社,bkPrice 单价,bkStatus 状态 from Book where bkName like + @bkName + '%'  ";  
comm.Parameters.AddWithValue("@bkName", txtbkName.Text);
SqlDataAdapter sda = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
sda.Fill(ds);
dgvBook.DataSource = ds.Tables[0];
}
private void Delete_Click(object sender, EventArgs e)
{
MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
DialogResult dr = MessageBox.Show("确定要删除吗?", "确定", messButton);
if (dr == DialogResult.OK)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "delete from Book where bkID = @bkID";
comm.Parameters.AddWithValue("@bkID", txtbkID.Text);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("删除成功!");
DataBind();
}
catch (Exception ex)
{
MessageBox.Show("删除失败!" + ex.Message);
}
}
}
private void Alter_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "update Book set bkID=@bkID,bkName=@bkName,bkAuthor=@bkAuthor,bkPress=@bkPress,bkPrice=@bkPrice,bkStatus=@bkStatus where bkID=@bkID";
comm.Parameters.AddWithValue("@bkID", txtbkID.Text);
comm.Parameters.AddWithValue("@bkName", txtbkName.Text);
comm.Parameters.AddWithValue("@bkAuthor", txtbkAuthor.Text);
comm.Parameters.AddWithValue("@bkPress", txtbkPress.Text);
comm.Parameters.AddWithValue("@bkPrice", txtbkPrice.Text);
comm.Parameters.AddWithValue("@bkStatus", txtbkStatus.Text);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("更新成功!");
DataBind();
}
catch (Exception ex)
{
MessageBox.Show("更新失败!" + ex.Message);
}
}
private void DataBind()
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select bkID 书号,bkName 书名,bkAuthor 作者,bkPress 出版社,bkPrice 单价,bkStatus 状态 from Book";
SqlDataAdapter sda = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
sda.Fill(ds);
dgvBook.DataSource = ds.Tables[0];
txtbkID.DataBindings.Clear();
txtbkName.DataBindings.Clear();
txtbkAuthor.DataBindings.Clear();
txtbkPress.DataBindings.Clear();
txtbkPrice.DataBindings.Clear();
txtbkStatus.DataBindings.Clear();
txtbkID.DataBindings.Add("Text", ds.Tables[0], "书号");
txtbkName.DataBindings.Add("Text", ds.Tables[0], "书名");
txtbkAuthor.DataBindings.Add("Text", ds.Tables[0], "作者");
txtbkPress.DataBindings.Add("Text", ds.Tables[0], "出版社");
txtbkPrice.DataBindings.Add("Text",ds.Tables[0],"单价");
txtbkStatus.DataBindings.Add("Text",ds.Tables[0],"状态");
conn.Close();
}
private void BookM_Load(object sender, EventArgs e)
{
DataBind();
}
}
}
DSC0006.png




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
public partial class ReaderM : Form
{
public ReaderM()
{
InitializeComponent();
}
private void BackMenu_Click(object sender, EventArgs e)
{
this.Hide();
new Menu().Show();
}
private void Add_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "insert into Reader values(@rdID,@rdType,@rdName,@rdDept,@rdQQ,@rdBorrowQty)";
comm.Parameters.AddWithValue("@rdID", txtrdID.Text);
comm.Parameters.AddWithValue("@rdType", txtrdType.Text);
comm.Parameters.AddWithValue("@rdName", txtrdName.Text);
comm.Parameters.AddWithValue("@rdDept", txtrdDept.Text);
comm.Parameters.AddWithValue("@rdQQ", txtrdQQ.Text);
comm.Parameters.AddWithValue("@rdBorrowQty", txtrdBorrowQty.Text);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("插入成功!");
DataBind();
}
catch (Exception ex)
{
MessageBox.Show("插入失败!" + ex.Message);
}
}
private void Search_Click(object sender, EventArgs e)
{
DataBind();
}
private void Delete_Click(object sender, EventArgs e)
{
MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
DialogResult dr = MessageBox.Show("确定要删除吗?", "确定", messButton);
if (dr == DialogResult.OK)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "delete from Reader where rdID = @rdID";
comm.Parameters.AddWithValue("@rdID", txtrdID.Text);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("删除成功!");
DataBind();
}
catch (Exception ex)
{
MessageBox.Show("删除失败!" + ex.Message);
}
}
}
private void Alter_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "update Reader set rdID=@rdID,rdType=@rdType,rdName=@rdName,rdDept=@rdDept,rdQQ=@rdQQ,rdBorrowQty=@rdBorrowQty where rdID=@rdID";
comm.Parameters.AddWithValue("@rdID", txtrdID.Text);
comm.Parameters.AddWithValue("@rdType", txtrdType .Text);
comm.Parameters.AddWithValue("@rdName", txtrdName.Text);
comm.Parameters.AddWithValue("@rdDept", txtrdDept.Text);
comm.Parameters.AddWithValue("@rdQQ", txtrdQQ.Text);
comm.Parameters.AddWithValue("@rdBorrowQty",txtrdBorrowQty.Text);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("更新成功!");
DataBind();
}
catch (Exception ex)
{
MessageBox.Show("更新失败!" + ex.Message);
}
}
private void DataBind()
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select rdID 读者编号,rdType 读者类别号,rdName 读者姓名,rdDept 读者单位,rdQQ 读者QQ,rdBorrowQty 已借书数量 from Reader";
SqlDataAdapter sda = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
sda.Fill(ds);
dgvReader.DataSource = ds.Tables[0];
txtrdID.DataBindings.Clear();
txtrdType.DataBindings.Clear();
txtrdName.DataBindings.Clear();
txtrdDept.DataBindings.Clear();
txtrdQQ.DataBindings.Clear();
txtrdBorrowQty.DataBindings.Clear();
txtrdID.DataBindings.Add("Text", ds.Tables[0], "读者编号");
txtrdType.DataBindings.Add("Text", ds.Tables[0], "读者类别号");
txtrdName.DataBindings.Add("Text", ds.Tables[0], "读者姓名");
txtrdDept.DataBindings.Add("Text", ds.Tables[0], "读者单位");
txtrdQQ.DataBindings.Add("Text", ds.Tables[0], "读者QQ");
txtrdBorrowQty.DataBindings.Add("Text", ds.Tables[0], "已借书数量");
conn.Close();
}
private void ReaderM_Load(object sender, EventArgs e)
{
DataBind();
}
}
}
DSC0007.png




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
public partial class BorrowAndReturn : Form
{
public BorrowAndReturn()
{
InitializeComponent();
}
private void BorrowBook_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "exec usp_BorrowBook @rdID,@bkID";
comm.Parameters.AddWithValue("@rdID", txtrdID.Text);
comm.Parameters.AddWithValue("@bkID", txtbkID.Text);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("借书成功!");
}
catch (Exception ex)
{
MessageBox.Show("借书失败!" + ex.Message);
}
}
private void ReturnBook_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "exec usp_ReturnBook @rdID,@bkID";
comm.Parameters.AddWithValue("@rdID", txtrdID.Text);
comm.Parameters.AddWithValue("@bkID", txtbkID.Text);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("还书成功!");
}
catch (Exception ex)
{
MessageBox.Show("还书失败!" + ex.Message);
}
}
private void BorrowAndReturn_Load(object sender, EventArgs e)
{
}
private void BackMenu_Click(object sender, EventArgs e)
{
this.Hide();
new Menu().Show();
}
private void Borrow_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True");
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select rdID 读者编号,bkID 书号,DateBorrow 借书日期,DateLendPlan 应还日期,DateLendAct 实际还书日期 from Borrow";
SqlDataAdapter sda = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
sda.Fill(ds);
dgvBorrow.DataSource = ds.Tables[0];
conn.Close();  
}
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-393523-1-1.html 上篇帖子: sql server 2008 R2 压缩备份数据库 下篇帖子: SQL Server配置管理器”远程过程调用失败“的问题解决
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表