📄 frmguanli.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BookManage
{
public partial class frmguanli : Form
{
private SqlConnection objSqlConnection;
private SqlCommand objSqlCommand;
private SqlDataAdapter objSqlDataAdapter;
private DataSet objDataSet;
int count;
public frmguanli(int s)
{
InitializeComponent();
count = s;
}
private void btnclose_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
if (this.txtid.Text.Equals("") || this.txtname.Text.Equals("") || this.txtleixing.Text.Equals("") || this.txtPrice.Text.Equals("") || this.txtfengmian.Text.Equals("") || this.txtleirong.Text.Equals(""))
{
MessageBox.Show("请输入完整的详细信息!");
}
else
{
try
{
objSqlConnection.Open();
string updCmd = "update bookInfo set BookType='" + this.txtleixing.Text + "',BookName='" + this.txtname.Text + "',BookPrice='" + this.txtPrice.Text + "',BookPic='" + this.txtfengmian.Text + "',BookContent='" + this.txtleirong.Text + "' where BookID='" + this.txtid.Text + "'";
objSqlCommand = new SqlCommand(updCmd, objSqlConnection);
objSqlCommand.ExecuteNonQuery();
MessageBox.Show("更新成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void frmguanli_Load(object sender, EventArgs e)
{
objSqlConnection = new SqlConnection("server=(local);database=Book;Integrated Security=SSPI;");
objDataSet = new DataSet();
objSqlDataAdapter = new SqlDataAdapter("select * from bookInfo", objSqlConnection);
objSqlDataAdapter.Fill(objDataSet, "bookInfo");
txtid.Text = objDataSet.Tables[0].Rows[count]["BookID"].ToString();
txtleixing.Text = objDataSet.Tables[0].Rows[count]["BookType"].ToString();
txtname.Text = objDataSet.Tables[0].Rows[count]["BookName"].ToString();
txtPrice.Text = objDataSet.Tables[0].Rows[count]["BookPrice"].ToString();
txtfengmian.Text = objDataSet.Tables[0].Rows[count]["BookPic"].ToString();
txtleirong.Text = objDataSet.Tables[0].Rows[count]["BookContent"].ToString();
//txtid.Enabled = false;
}
private void btnadd_Click(object sender, EventArgs e)
{
if (this.txtid.Text.Equals("") || this.txtname.Text.Equals("") || this.txtleixing.Text.Equals("") || this.txtPrice.Text.Equals("") || this.txtfengmian.Text.Equals("") || this.txtleirong.Text.Equals(""))
{
MessageBox.Show("请输入完整的详细信息!");
}
else
{
try
{
objSqlConnection.Open();
string insCmd = "insert into bookInfo values('"+this.txtid.Text+"','" + this.txtleixing.Text + "','" + this.txtname.Text + "','" + this.txtPrice.Text + "','" + this.txtfengmian.Text + "','" + this.txtleirong.Text + "')";
objSqlCommand = new SqlCommand(insCmd, objSqlConnection);
objSqlCommand.ExecuteNonQuery();
MessageBox.Show("添加成功!");
}
catch (Exception ex)
{
MessageBox.Show("该书已存在!");
return;
}
finally
{
objSqlConnection.Close();
}
}
}
private void btndelete_Click(object sender, EventArgs e)
{
if (this.txtid.Text.Equals(""))
{
MessageBox.Show("请输入你要删除书的编号!");
}
else
{
try
{
objSqlConnection.Open();
string delCmd = "delete from bookInfo where BookID like '" + this.txtid.Text + "'";
objSqlCommand = new SqlCommand(delCmd, objSqlConnection);
DialogResult objDialogResult = MessageBox.Show("确定删除当前书吗", "确认", MessageBoxButtons.YesNo);
if (objDialogResult.Equals(DialogResult.Yes))
{
objSqlCommand.ExecuteNonQuery();
MessageBox.Show("删除成功!");
}
else
{
return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
objSqlConnection.Close();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -