?? cancelreader.cs
字號:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Library
{
/// <summary>
/// CancelReader 的摘要說明。
/// </summary>
public class CancelReader : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Button btnOK;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
private SqlCommand cmd;
private SqlDataReader dr;
public CancelReader()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.btnClose = new System.Windows.Forms.Button();
this.btnOK = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.groupBox1.Location = new System.Drawing.Point(16, 16);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(256, 128);
this.groupBox1.TabIndex = 5;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "注銷讀者資料";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(28, 72);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(196, 21);
this.textBox1.TabIndex = 9;
this.textBox1.Text = "";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(28, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(66, 17);
this.label2.TabIndex = 8;
this.label2.Text = "借書證號碼";
//
// btnClose
//
this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnClose.Location = new System.Drawing.Point(192, 160);
this.btnClose.Name = "btnClose";
this.btnClose.TabIndex = 6;
this.btnClose.Text = "關閉";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnOK
//
this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnOK.Location = new System.Drawing.Point(96, 160);
this.btnOK.Name = "btnOK";
this.btnOK.TabIndex = 7;
this.btnOK.Text = "確定";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// CancelReader
//
this.AcceptButton = this.btnOK;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(290, 200);
this.ControlBox = false;
this.Controls.Add(this.btnOK);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "CancelReader";
this.ShowInTaskbar = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "注銷讀者";
this.Load += new System.EventHandler(this.CancelReader_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void initializeScreen()
{
textBox1.Text = "";
textBox1.Focus();
}
/// <summary>
/// 刪除讀者記錄
/// </summary>
/// <param name="id">讀者證號碼</param>
private void deleteReader(string id)
{
string str = "";
if(MessageBox.Show("注銷借書證會將所有與之相關聯的記錄全部刪除!\n此操作不可恢復!\n\n是否繼續?","操作存在風險",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.Yes)
{
str = "delete from 借書證 where 借書證號='" + textBox1.Text + "'";
//創建事務
SqlTransaction trans = Global.conn.BeginTransaction();
cmd.Transaction = trans;
try
{
cmd.CommandText = str;
cmd.ExecuteNonQuery();
str = "delete from 流通 where 借書證號='" + textBox1.Text + "'";
cmd.CommandText = str;
cmd.ExecuteNonQuery();
trans.Commit();
MessageBox.Show("成功刪除記錄!");
}
catch
{
try
{
trans.Rollback();
}
catch (SqlException ex)
{
if (trans.Connection != null)
{
MessageBox.Show(ex.ToString());
}
}
MessageBox.Show("發生錯誤!圖書借閱未成功!");
initializeScreen();
return;
}
}
//string str = "delete";
}
/// <summary>
/// 檢查是否存在此借書證
/// </summary>
/// <param name="id"></param>
private void checkCardID(string id)
{
string str = "select * from 借書證 where 借書證號='" + id + "'";
cmd.CommandText = str;
try
{
dr = cmd.ExecuteReader();
dr.Read();
if(!dr.HasRows)
{
if(dr!=null)dr.Close();
MessageBox.Show("數據庫不存在此借書證!請確保借書證號碼填寫正確!","錯誤");
textBox1.Focus();
textBox1.SelectAll();
return;
}
if(dr!=null)dr.Close();
}
catch(Exception ex)
{
if(dr!= null)dr.Close();
MessageBox.Show(ex.ToString() + "錯誤");
return;
}
}
/// <summary>
/// 關閉
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClose_Click(object sender, System.EventArgs e)
{
Global.sbpGlobal.Text = "就緒";
this.Close();
}
/// <summary>
/// 確定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOK_Click(object sender, System.EventArgs e)
{
if(textBox1.Text == "")
{
MessageBox.Show("借書證號碼不能為空!");
textBox1.Focus();
}
else
{
deleteReader(textBox1.Text);
}
}
/// <summary>
/// 窗體加載代碼
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CancelReader_Load(object sender, System.EventArgs e)
{
cmd = Global.conn.CreateCommand();
initializeScreen();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -