BookForm.cs
资源名称:BookStore.rar [点击查看]
上传用户:xyl529207
上传日期:2022-08-03
资源大小:935k
文件大小:6k
源码类别:
行业应用
开发平台:
SQL
- 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;
- using System.Configuration;
- namespace BookStoreMan
- {
- public partial class BookForm : Form
- {
- protected SqlConnection conn;
- protected SqlCommand cmd;
- protected BookStoreDataSetTableAdapters.BookTableAdapter adapter;
- public BookForm()
- {
- InitializeComponent();
- }
- private void BookForm_Load(object sender, EventArgs e)
- {
- try
- {
- conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BookStoreMan.Properties.Settings.BookStoreConnectionString"].ConnectionString);
- conn.Open();
- this.InitData();
- adapter = new BookStoreDataSetTableAdapters.BookTableAdapter();
- adapter.Fill(bookStoreDataSet1.Book);
- dgvBook.DataSource = bookStoreDataSet1.Book.DefaultView;
- }
- catch (Exception exp)
- {
- MessageBox.Show("无法建立数据连接:" + exp.Message);
- this.Close();
- }
- finally
- {
- if (conn != null && conn.State != ConnectionState.Closed)
- conn.Close();
- }
- }
- private void btnQuery_Click(object sender, EventArgs e)
- {
- if (!chbName.Checked && !chbPress.Checked && !chbAuthor.Checked)
- {
- MessageBox.Show("请至少选择一项查询条件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- if (chbName.Checked && tbName.Text == "")
- {
- MessageBox.Show("请输入书名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- tbName.Focus();
- return;
- }
- if (chbPress.Checked && cmbPress.Text == "")
- {
- MessageBox.Show("请输入出版社!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- cmbPress.Focus();
- return;
- }
- if (chbAuthor.Checked && tbAuthor.Text == "")
- {
- MessageBox.Show("请输入作者!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- tbAuthor.Focus();
- return;
- }
- bookStoreDataSet1.Book.DefaultView.RowFilter = this.GetFilter();
- }
- private void btnReset_Click(object sender, EventArgs e)
- {
- chbName.Checked = true;
- chbPress.Checked = false;
- chbAuthor.Checked = false;
- tbName.Text = "";
- cmbPress.Text = "";
- tbAuthor.Text = "";
- }
- private void btnViewAll_Click(object sender, EventArgs e)
- {
- bookStoreDataSet1.Book.DefaultView.RowFilter = "";
- }
- private void btnSel_Click(object sender, EventArgs e)
- {
- if (cmbBookBarcode.Text == "")
- return;
- bookStoreDataSet1.Book.DefaultView.RowFilter = string.Format("Barcode ='{0}'", cmbBookBarcode.Text);
- }
- private void dgvBook_DataError(object sender, DataGridViewDataErrorEventArgs e)
- {
- MessageBox.Show("输入不合法, 请重新输入或按Esc键取消修改. 原因:" + e.Exception.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- private void btnSave_Click(object sender, EventArgs e)
- {
- if (!bookStoreDataSet1.HasChanges())
- return;
- if (MessageBox.Show("确认保存所进行的修改吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
- return;
- try
- {
- adapter.Update(bookStoreDataSet1.Book);
- MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch (Exception exp)
- {
- MessageBox.Show("保存失败:" + exp.Message);
- }
- }
- private void btnAbort_Click(object sender, EventArgs e)
- {
- if (!bookStoreDataSet1.HasChanges())
- return;
- if(MessageBox.Show("确认要放弃上次保存以来的所有修改吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
- bookStoreDataSet1.Book.RejectChanges();
- }
- protected void InitData()
- {
- cmd = new SqlCommand("SELECT DISTINCT [PRESS] FROM [BOOK] ORDER BY [PRESS]", conn);
- SqlDataReader reader1 = cmd.ExecuteReader();
- cmbPress.Items.Clear();
- while (reader1.Read())
- cmbPress.Items.Add(reader1[0]);
- reader1.Close();
- cmd.CommandText = "SELECT DISTINCT [Barcode] FROM [Book] ORDER BY [Barcode]";
- reader1 = cmd.ExecuteReader();
- cmbBookBarcode.Items.Clear();
- while (reader1.Read())
- cmbBookBarcode.Items.Add(reader1[0]);
- reader1.Close();
- }
- protected string GetFilter()
- {
- StringBuilder sb1 = new StringBuilder();
- if (chbName.Checked)
- sb1.Append(string.Format("Name LIKE '%{0}%'", tbName.Text));
- if (chbPress.Checked)
- {
- if (sb1.Length > 0)
- sb1.Append(" AND ");
- sb1.Append(string.Format("Press = '{0}'", cmbPress.Text));
- }
- if (chbAuthor.Checked)
- {
- if (sb1.Length > 0)
- sb1.Append(" AND ");
- sb1.Append(string.Format("Author LIKE '%{0}%'", tbAuthor.Text));
- }
- return sb1.ToString();
- }
- }
- }