BookForm.cs
上传用户:xyl529207
上传日期:2022-08-03
资源大小:935k
文件大小:6k
源码类别:

行业应用

开发平台:

SQL

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9. using System.Configuration;
  10. namespace BookStoreMan
  11. {
  12.     public partial class BookForm : Form
  13.     {
  14.         protected SqlConnection conn;
  15.         protected SqlCommand cmd;
  16.         protected BookStoreDataSetTableAdapters.BookTableAdapter adapter;
  17.         public BookForm()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.         private void BookForm_Load(object sender, EventArgs e)
  22.         {
  23.             try
  24.             {
  25.                 conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BookStoreMan.Properties.Settings.BookStoreConnectionString"].ConnectionString);
  26.                 conn.Open();
  27.                 this.InitData();
  28.                 adapter = new BookStoreDataSetTableAdapters.BookTableAdapter();
  29.                 adapter.Fill(bookStoreDataSet1.Book);
  30.                 dgvBook.DataSource = bookStoreDataSet1.Book.DefaultView;
  31.             }
  32.             catch (Exception exp)
  33.             {
  34.                 MessageBox.Show("无法建立数据连接:" + exp.Message);
  35.                 this.Close();
  36.             }
  37.             finally
  38.             {
  39.                 if (conn != null && conn.State != ConnectionState.Closed)
  40.                     conn.Close();
  41.             }
  42.         }
  43.         private void btnQuery_Click(object sender, EventArgs e)
  44.         {
  45.             if (!chbName.Checked && !chbPress.Checked && !chbAuthor.Checked)
  46.             {
  47.                 MessageBox.Show("请至少选择一项查询条件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  48.                 return;
  49.             }
  50.             if (chbName.Checked && tbName.Text == "")
  51.             {
  52.                 MessageBox.Show("请输入书名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  53.                 tbName.Focus();
  54.                 return;
  55.             }
  56.             if (chbPress.Checked && cmbPress.Text == "")
  57.             {
  58.                 MessageBox.Show("请输入出版社!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  59.                 cmbPress.Focus();
  60.                 return;
  61.             }
  62.             if (chbAuthor.Checked && tbAuthor.Text == "")
  63.             {
  64.                 MessageBox.Show("请输入作者!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  65.                 tbAuthor.Focus();
  66.                 return;
  67.             }
  68.             bookStoreDataSet1.Book.DefaultView.RowFilter = this.GetFilter();
  69.         }
  70.         private void btnReset_Click(object sender, EventArgs e)
  71.         {
  72.             chbName.Checked = true;
  73.             chbPress.Checked = false;
  74.             chbAuthor.Checked = false;
  75.             tbName.Text = "";
  76.             cmbPress.Text = "";
  77.             tbAuthor.Text = "";
  78.         }
  79.         private void btnViewAll_Click(object sender, EventArgs e)
  80.         {
  81.             bookStoreDataSet1.Book.DefaultView.RowFilter = "";
  82.         }
  83.         private void btnSel_Click(object sender, EventArgs e)
  84.         {
  85.             if (cmbBookBarcode.Text == "")
  86.                 return;
  87.             bookStoreDataSet1.Book.DefaultView.RowFilter = string.Format("Barcode ='{0}'", cmbBookBarcode.Text);
  88.         }
  89.         private void dgvBook_DataError(object sender, DataGridViewDataErrorEventArgs e)
  90.         {
  91.             MessageBox.Show("输入不合法, 请重新输入或按Esc键取消修改. 原因:" + e.Exception.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  92.         }
  93.         private void btnSave_Click(object sender, EventArgs e)
  94.         {
  95.             if (!bookStoreDataSet1.HasChanges())
  96.                 return;
  97.             if (MessageBox.Show("确认保存所进行的修改吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
  98.                 return;
  99.             try
  100.             {
  101.                 adapter.Update(bookStoreDataSet1.Book);
  102.                 MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  103.             }
  104.             catch (Exception exp)
  105.             {
  106.                 MessageBox.Show("保存失败:" + exp.Message);
  107.             }
  108.         }
  109.         private void btnAbort_Click(object sender, EventArgs e)
  110.         {
  111.             if (!bookStoreDataSet1.HasChanges())
  112.                 return;
  113.             if(MessageBox.Show("确认要放弃上次保存以来的所有修改吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
  114.                 bookStoreDataSet1.Book.RejectChanges();
  115.         }
  116.         protected void InitData()
  117.         {
  118.             cmd = new SqlCommand("SELECT DISTINCT [PRESS] FROM [BOOK] ORDER BY [PRESS]", conn);
  119.             SqlDataReader reader1 = cmd.ExecuteReader();
  120.             cmbPress.Items.Clear();
  121.             while (reader1.Read())
  122.                 cmbPress.Items.Add(reader1[0]);
  123.             reader1.Close();
  124.             cmd.CommandText = "SELECT DISTINCT [Barcode] FROM [Book] ORDER BY [Barcode]";
  125.             reader1 = cmd.ExecuteReader();
  126.             cmbBookBarcode.Items.Clear();
  127.             while (reader1.Read())
  128.                 cmbBookBarcode.Items.Add(reader1[0]);
  129.             reader1.Close();
  130.         }
  131.         protected string GetFilter()
  132.         {
  133.             StringBuilder sb1 = new StringBuilder();
  134.             if (chbName.Checked)
  135.                 sb1.Append(string.Format("Name LIKE '%{0}%'", tbName.Text));
  136.             if (chbPress.Checked)
  137.             {
  138.                 if (sb1.Length > 0)
  139.                     sb1.Append(" AND ");
  140.                 sb1.Append(string.Format("Press = '{0}'", cmbPress.Text));
  141.             }
  142.             if (chbAuthor.Checked)
  143.             {
  144.                 if (sb1.Length > 0)
  145.                     sb1.Append(" AND ");
  146.                 sb1.Append(string.Format("Author LIKE '%{0}%'", tbAuthor.Text));
  147.             }
  148.             return sb1.ToString();
  149.         }
  150.     }
  151. }