C# winform简易图书管理系统设计代码(只实现增删查改的操作)
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace librarian
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//声明参数
string conStr;
SqlConnection conn;
//声明数据库连接方法体
private void DBConnect()
{
conStr = "server=localhost;database=DBbook;UID=sa;password=dataadmin";
conn = new SqlConnection(conStr);
}
//图书查功能
private void butSelect_Click(object sender, EventArgs e)
{
DBConnect();
if (txtID.Text != "")
{ //用图书编号查图书
string cmdStr = "select * from TbBookMessage where bookID='" + txtID.Text + "'";
SqlCommand cmd = new SqlCommand(cmdStr, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd; //将sql执行语句设置为已实例化的SqlDataAdapter查询命令
DataSet ds = new DataSet();
da.Fill(ds); //把数据填充到"临时仓库"DataSet
if (ds.Tables[0].Rows.Count > 0)
{ // 临时仓库表中存在数据时,将对应字段填充到textbox中
txtID.Text = ds.Tables[0].Rows[0]["bookID"].ToString();
txtName.Text = ds.Tables[0].Rows[0]["bookName"].ToString();
txtMessage.Text = ds.Tables[0].Rows[0]["bookMessage"].ToString();
}
else
{
MessageBox.Show("您要的图书不存在或已删除", "提示");
txtID.Clear(); //清空txtID文本框
txtID.Focus(); //设为焦点
}
}
else if (txtID.Text == "" && txtName.Text != "")
{ //用图书名称查图书
string cmdStr = "select * from TbBookMessage where bookName='" + txtName.Text + "'";
SqlCommand cmd = new SqlCommand(cmdStr, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
txtID.Text = ds.Tables[0].Rows[0]["bookID"].ToString();
txtName.Text = ds.Tables[0].Rows[0]["bookName"].ToString();
txtMessage.Text = ds.Tables[0].Rows[0]["bookMessage"].ToString();
}
else
{
MessageBox.Show("您要的图书不存在或已删除", "提示");
txtName.Clear();
txtName.Focus();
}
}
else
{
MessageBox.Show("请输入图书编号或名称", "提示");
txtID.Focus();
}
}
//图书修改功能
private void butUpdate_Click(object sender, EventArgs e)
{
conn.Open();
if (txtID.Text != "")
{
if (MessageBox.Show("确定要修改图书信息吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{ //二次确认
string cmdStr = "update TbBookMessage set bookName='" + txtName.Text + "',bookMessage='" + txtMessage.Text + "'where bookID='" + txtID.Text + "'"winform增删改查简单案例;
SqlCommand cmd = new SqlCommand(cmdStr, conn);
if (cmd.ExecuteNonQuery() > 0)
{ //受影响行数大于0,则说明sql语句执行成功
MessageBox.Show("图书信息修改成功", "提示");
}
else
{
MessageBox.Show("与服务器断开连接", "操作失败");
}
}
}
else
{
MessageBox.Show("请输入要修改的图书编号", "提示");
txtID.Focus();
}
conn.Close();
}
//图书删除功能
private void butDelete_Click(object sender, EventArgs e)
{
if (txtID.Text != "")
{
if (MessageBox.Show("确认要删除此图书吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论