這是sql server的第七、八次上機內容,抽了幾天時間給做了
在原有的booksDB庫中加了一個Admin表:UserName:root,PassWord:123456.
環境:Visual Studio 2010,SQL Server 2008
參考書籍:
項目結構:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Threading; namespace WindowsFormsApplication2 { public partial class Login : Form { public Login() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } private void textBox2_TextChanged(object sender, EventArgs e) { } private void label1_Click_1(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { string User = UserName.Text; string Pwd = PassWord.Text; SqlConnection conn = new SqlConnection(); conn.ConnectionString = "server=.;database=booksDB;integrated security=True"; try { conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "select * from Admin where UserName='" + User + "'"; SqlDataReader reader = comm.ExecuteReader(); if (reader.Read()) { string password = reader.GetString(reader.GetOrdinal("PassWord")); if (Pwd == password) { MessageBox.Show("登陸成功!"); new Thread(() => Application.Run(new Menu())).Start(); this.Close(); //this.Hide(); //Form M = new Management(); //M.Show(); } else { MessageBox.Show("密碼錯誤!"); UserName.Text = " "; PassWord.Text = " "; } } else { MessageBox.Show("用戶不存在!"); UserName.Text = " "; PassWord.Text = " "; } } catch (Exception ex) { MessageBox.Show(ex.Message, "操作數據庫出錯"); } finally { conn.Close(); } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Menu : Form { public Menu() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.Hide(); new ReaderTypeM().Show(); } private void button2_Click(object sender, EventArgs e) { this.Hide(); new BookM().Show(); } private void button3_Click(object sender, EventArgs e) { this.Hide(); new ReaderM().Show(); } private void button4_Click(object sender, EventArgs e) { this.Hide(); new BorrowAndReturn().Show(); } private void Menu_Load(object sender, EventArgs e) { } private void Out_Click(object sender, EventArgs e) { this.Close(); } } }
連數據庫前先用BindingSource控件綁定數據庫數據。再用DataGridView控件展示數據。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication2 { public partial class ReaderTypeM : Form { public ReaderTypeM() { InitializeComponent(); } private void BackMenu_Click(object sender, EventArgs e) { this.Hide(); new Menu().Show(); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void Add_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "insert into ReaderType values(@rdType,@rdTypeName,@canLendQty,@canLendDay)"; comm.Parameters.AddWithValue("@rdType", txtrdType.Text); comm.Parameters.AddWithValue("@rdTypeName", txtrdTypeName.Text); comm.Parameters.AddWithValue("@canLendQty", txtcanLendQty.Text); comm.Parameters.AddWithValue("@canLendDay", txtcanLendDay.Text); try { comm.ExecuteNonQuery(); MessageBox.Show("插入成功!"); DataBind(); } catch (Exception ex) { MessageBox.Show("插入失敗!" + ex.Message); } } private void Search_Click(object sender, EventArgs e) { DataBind(); } private void Delete_Click(object sender, EventArgs e) { MessageBoxButtons messButton = MessageBoxButtons.OKCancel; DialogResult dr = MessageBox.Show("確定要刪除嗎?", "確定", messButton); if (dr == DialogResult.OK) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "delete from ReaderType where rdType = @rdType"; comm.Parameters.AddWithValue("@rdType", txtrdType.Text); try { comm.ExecuteNonQuery(); MessageBox.Show("刪除成功!"); DataBind(); } catch (Exception ex) { MessageBox.Show("刪除失敗!" + ex.Message); } } } private void Alter_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "update ReaderType set rdType=@rdType,rdTypeName=@rdTypeName,canLendQty=@canLendQty,canLendDay=@canLendDay where rdType=@rdType"; comm.Parameters.AddWithValue("@rdType", txtrdType.Text); comm.Parameters.AddWithValue("@rdTypeName", txtrdTypeName.Text); comm.Parameters.AddWithValue("@canLendQty", txtcanLendQty.Text); comm.Parameters.AddWithValue("@canLendDay", txtcanLendDay.Text); try { comm.ExecuteNonQuery(); MessageBox.Show("更新成功!"); DataBind(); } catch (Exception ex) { MessageBox.Show("更新失敗!" + ex.Message); } } private void DataBind() { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "select rdType 類別號,rdTypeName 類別名稱,canLendQty 可借數量,canLendDay 可借天數 from ReaderType"; SqlDataAdapter sda = new SqlDataAdapter(comm); DataSet ds = new DataSet(); sda.Fill(ds); dgvBook.DataSource = ds.Tables[0]; txtrdType.DataBindings.Clear(); txtrdTypeName.DataBindings.Clear(); txtcanLendQty.DataBindings.Clear(); txtcanLendDay.DataBindings.Clear(); txtrdType.DataBindings.Add("Text", ds.Tables[0], "類別號"); txtrdTypeName.DataBindings.Add("Text", ds.Tables[0], "類別名稱"); txtcanLendQty.DataBindings.Add("Text", ds.Tables[0], "可借數量"); txtcanLendDay.DataBindings.Add("Text", ds.Tables[0], "可借天數"); conn.Close(); } private void ReaderTypeM_Load(object sender, EventArgs e) { DataBind(); } private void bindingSource1_CurrentChanged(object sender, EventArgs e) { } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication2 { public partial class BookM : Form { public BookM() { InitializeComponent(); } private void bindingSource1_CurrentChanged(object sender, EventArgs e) { } private void BackMenu_Click(object sender, EventArgs e) { this.Hide(); new Menu().Show(); } private void Add_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "insert into Book values(@bkID,@bkName,@bkAuthor,@bkPress,@bkPrice,@bkStatus)"; comm.Parameters.AddWithValue("@bkID", txtbkID.Text); comm.Parameters.AddWithValue("@bkName", txtbkName.Text); comm.Parameters.AddWithValue("@bkAuthor", txtbkAuthor.Text); comm.Parameters.AddWithValue("@bkPress", txtbkPress.Text); comm.Parameters.AddWithValue("@bkPrice", txtbkPrice.Text); comm.Parameters.AddWithValue("@bkStatus", txtbkStatus.Text); try { comm.ExecuteNonQuery(); MessageBox.Show("插入成功!"); DataBind(); } catch (Exception ex) { MessageBox.Show("插入失敗!" + ex.Message); } } private void Search_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "select bkID 書號,bkName 書名,bkAuthor 作者,bkPress 出版社,bkPrice 單價,bkStatus 狀態 from Book where bkName like + @bkName + '%' "; comm.Parameters.AddWithValue("@bkName", txtbkName.Text); SqlDataAdapter sda = new SqlDataAdapter(comm); DataSet ds = new DataSet(); sda.Fill(ds); dgvBook.DataSource = ds.Tables[0]; } private void Delete_Click(object sender, EventArgs e) { MessageBoxButtons messButton = MessageBoxButtons.OKCancel; DialogResult dr = MessageBox.Show("確定要刪除嗎?", "確定", messButton); if (dr == DialogResult.OK) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "delete from Book where bkID = @bkID"; comm.Parameters.AddWithValue("@bkID", txtbkID.Text); try { comm.ExecuteNonQuery(); MessageBox.Show("刪除成功!"); DataBind(); } catch (Exception ex) { MessageBox.Show("刪除失敗!" + ex.Message); } } } private void Alter_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "update Book set bkID=@bkID,bkName=@bkName,bkAuthor=@bkAuthor,bkPress=@bkPress,bkPrice=@bkPrice,bkStatus=@bkStatus where bkID=@bkID"; comm.Parameters.AddWithValue("@bkID", txtbkID.Text); comm.Parameters.AddWithValue("@bkName", txtbkName.Text); comm.Parameters.AddWithValue("@bkAuthor", txtbkAuthor.Text); comm.Parameters.AddWithValue("@bkPress", txtbkPress.Text); comm.Parameters.AddWithValue("@bkPrice", txtbkPrice.Text); comm.Parameters.AddWithValue("@bkStatus", txtbkStatus.Text); try { comm.ExecuteNonQuery(); MessageBox.Show("更新成功!"); DataBind(); } catch (Exception ex) { MessageBox.Show("更新失敗!" + ex.Message); } } private void DataBind() { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "select bkID 書號,bkName 書名,bkAuthor 作者,bkPress 出版社,bkPrice 單價,bkStatus 狀態 from Book"; SqlDataAdapter sda = new SqlDataAdapter(comm); DataSet ds = new DataSet(); sda.Fill(ds); dgvBook.DataSource = ds.Tables[0]; txtbkID.DataBindings.Clear(); txtbkName.DataBindings.Clear(); txtbkAuthor.DataBindings.Clear(); txtbkPress.DataBindings.Clear(); txtbkPrice.DataBindings.Clear(); txtbkStatus.DataBindings.Clear(); txtbkID.DataBindings.Add("Text", ds.Tables[0], "書號"); txtbkName.DataBindings.Add("Text", ds.Tables[0], "書名"); txtbkAuthor.DataBindings.Add("Text", ds.Tables[0], "作者"); txtbkPress.DataBindings.Add("Text", ds.Tables[0], "出版社"); txtbkPrice.DataBindings.Add("Text",ds.Tables[0],"單價"); txtbkStatus.DataBindings.Add("Text",ds.Tables[0],"狀態"); conn.Close(); } private void BookM_Load(object sender, EventArgs e) { DataBind(); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication2 { public partial class ReaderM : Form { public ReaderM() { InitializeComponent(); } private void BackMenu_Click(object sender, EventArgs e) { this.Hide(); new Menu().Show(); } private void Add_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "insert into Reader values(@rdID,@rdType,@rdName,@rdDept,@rdQQ,@rdBorrowQty)"; comm.Parameters.AddWithValue("@rdID", txtrdID.Text); comm.Parameters.AddWithValue("@rdType", txtrdType.Text); comm.Parameters.AddWithValue("@rdName", txtrdName.Text); comm.Parameters.AddWithValue("@rdDept", txtrdDept.Text); comm.Parameters.AddWithValue("@rdQQ", txtrdQQ.Text); comm.Parameters.AddWithValue("@rdBorrowQty", txtrdBorrowQty.Text); try { comm.ExecuteNonQuery(); MessageBox.Show("插入成功!"); DataBind(); } catch (Exception ex) { MessageBox.Show("插入失敗!" + ex.Message); } } private void Search_Click(object sender, EventArgs e) { DataBind(); } private void Delete_Click(object sender, EventArgs e) { MessageBoxButtons messButton = MessageBoxButtons.OKCancel; DialogResult dr = MessageBox.Show("確定要刪除嗎?", "確定", messButton); if (dr == DialogResult.OK) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "delete from Reader where rdID = @rdID"; comm.Parameters.AddWithValue("@rdID", txtrdID.Text); try { comm.ExecuteNonQuery(); MessageBox.Show("刪除成功!"); DataBind(); } catch (Exception ex) { MessageBox.Show("刪除失敗!" + ex.Message); } } } private void Alter_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "update Reader set rdID=@rdID,rdType=@rdType,rdName=@rdName,rdDept=@rdDept,rdQQ=@rdQQ,rdBorrowQty=@rdBorrowQty where rdID=@rdID"; comm.Parameters.AddWithValue("@rdID", txtrdID.Text); comm.Parameters.AddWithValue("@rdType", txtrdType .Text); comm.Parameters.AddWithValue("@rdName", txtrdName.Text); comm.Parameters.AddWithValue("@rdDept", txtrdDept.Text); comm.Parameters.AddWithValue("@rdQQ", txtrdQQ.Text); comm.Parameters.AddWithValue("@rdBorrowQty",txtrdBorrowQty.Text); try { comm.ExecuteNonQuery(); MessageBox.Show("更新成功!"); DataBind(); } catch (Exception ex) { MessageBox.Show("更新失敗!" + ex.Message); } } private void DataBind() { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "select rdID 讀者編號,rdType 讀者類別號,rdName 讀者姓名,rdDept 讀者單位,rdQQ 讀者QQ,rdBorrowQty 已借書數量 from Reader"; SqlDataAdapter sda = new SqlDataAdapter(comm); DataSet ds = new DataSet(); sda.Fill(ds); dgvReader.DataSource = ds.Tables[0]; txtrdID.DataBindings.Clear(); txtrdType.DataBindings.Clear(); txtrdName.DataBindings.Clear(); txtrdDept.DataBindings.Clear(); txtrdQQ.DataBindings.Clear(); txtrdBorrowQty.DataBindings.Clear(); txtrdID.DataBindings.Add("Text", ds.Tables[0], "讀者編號"); txtrdType.DataBindings.Add("Text", ds.Tables[0], "讀者類別號"); txtrdName.DataBindings.Add("Text", ds.Tables[0], "讀者姓名"); txtrdDept.DataBindings.Add("Text", ds.Tables[0], "讀者單位"); txtrdQQ.DataBindings.Add("Text", ds.Tables[0], "讀者QQ"); txtrdBorrowQty.DataBindings.Add("Text", ds.Tables[0], "已借書數量"); conn.Close(); } private void ReaderM_Load(object sender, EventArgs e) { DataBind(); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication2 { public partial class BorrowAndReturn : Form { public BorrowAndReturn() { InitializeComponent(); } private void BorrowBook_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "exec usp_BorrowBook @rdID,@bkID"; comm.Parameters.AddWithValue("@rdID", txtrdID.Text); comm.Parameters.AddWithValue("@bkID", txtbkID.Text); try { comm.ExecuteNonQuery(); MessageBox.Show("借書成功!"); } catch (Exception ex) { MessageBox.Show("借書失敗!" + ex.Message); } } private void ReturnBook_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "exec usp_ReturnBook @rdID,@bkID"; comm.Parameters.AddWithValue("@rdID", txtrdID.Text); comm.Parameters.AddWithValue("@bkID", txtbkID.Text); try { comm.ExecuteNonQuery(); MessageBox.Show("還書成功!"); } catch (Exception ex) { MessageBox.Show("還書失敗!" + ex.Message); } } private void BorrowAndReturn_Load(object sender, EventArgs e) { } private void BackMenu_Click(object sender, EventArgs e) { this.Hide(); new Menu().Show(); } private void Borrow_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=booksDB;integrated security=True"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "select rdID 讀者編號,bkID 書號,DateBorrow 借書日期,DateLendPlan 應還日期,DateLendAct 實際還書日期 from Borrow"; SqlDataAdapter sda = new SqlDataAdapter(comm); DataSet ds = new DataSet(); sda.Fill(ds); dgvBorrow.DataSource = ds.Tables[0]; conn.Close(); } } }
源碼地址:https://github.com/Vito-Yan/iCourse