📄 bookmaintenance.vb
字号:
Imports System.Data.SqlClient
Public Class BookMaintenance
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.Label2 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Label4 = New System.Windows.Forms.Label
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.Label5 = New System.Windows.Forms.Label
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(56, 32)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 0
Me.Label1.Text = "书籍名称"
'
'ComboBox1
'
Me.ComboBox1.Location = New System.Drawing.Point(184, 32)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(144, 20)
Me.ComboBox1.TabIndex = 1
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(56, 88)
Me.Label2.Name = "Label2"
Me.Label2.TabIndex = 2
Me.Label2.Text = "作 者"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(184, 88)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(144, 21)
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = ""
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(56, 136)
Me.Label3.Name = "Label3"
Me.Label3.TabIndex = 4
Me.Label3.Text = "出 版 社"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(184, 136)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(144, 21)
Me.TextBox2.TabIndex = 5
Me.TextBox2.Text = ""
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(56, 184)
Me.Label4.Name = "Label4"
Me.Label4.TabIndex = 6
Me.Label4.Text = "书籍定价"
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(184, 184)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(144, 21)
Me.TextBox3.TabIndex = 7
Me.TextBox3.Text = ""
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(56, 232)
Me.Label5.Name = "Label5"
Me.Label5.TabIndex = 8
Me.Label5.Text = "出版日期"
'
'TextBox4
'
Me.TextBox4.Location = New System.Drawing.Point(184, 232)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.Size = New System.Drawing.Size(144, 21)
Me.TextBox4.TabIndex = 9
Me.TextBox4.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(48, 296)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 10
Me.Button1.Text = "更新"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(152, 296)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 11
Me.Button2.Text = "删除"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(256, 296)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 12
Me.Button3.Text = "取消"
'
'BookMaintenance
'
Me.AcceptButton = Me.Button1
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.BackColor = System.Drawing.SystemColors.ActiveBorder
Me.ClientSize = New System.Drawing.Size(384, 357)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.ComboBox1)
Me.Controls.Add(Me.Label1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.MaximizeBox = False
Me.Name = "BookMaintenance"
Me.Text = "书籍信息维护"
Me.ResumeLayout(False)
End Sub
#End Region
Private ID1 As Integer
Public Function CheckData() As Boolean
'验证数据的合法性
If ComboBox1.Text = "" Then
MessageBox.Show("书籍选项不能为空!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return False
End If
If TextBox1.Text = "" Then
MessageBox.Show("作者选项不能为空!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return False
End If
If TextBox2.Text = "" Then
MessageBox.Show("出版社选项不能为空!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return False
End If
If TextBox3.Text = "" Then
MessageBox.Show("书籍定价项不能为空!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return False
Else
If Not IsNumeric(TextBox3.Text) Then
MessageBox.Show("书籍选项必须为数字!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return False
End If
End If
If TextBox4.Text = "" Then
MessageBox.Show("出版日期选项不能为空!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return False
End If
Return True
End Function
Private Sub ReadDataToComboBox()
Try
If mycon.State = ConnectionState.Closed Then
mycon.Open()
End If
Dim sqlstr As String = "select * from Book Where Flag='0'" '只有未借的书才有可能更新或修改,呵呵
Dim myreader As SqlDataReader
Dim mycom As New SqlCommand(sqlstr, mycon)
myreader = mycom.ExecuteReader()
While myreader.Read
ComboBox1.Items.Add(myreader("Name"))
End While
myreader.Close()
ComboBox1.SelectedIndex = -1
mycon.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BookMaintenance_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'将数据库里的book表里的内容读到ComboBox里来
Call ReadDataToComboBox()
End Sub
'由一个字段获取其ID号
Public Function GetID(ByVal s2 As String, ByVal s3 As String) As Integer
Try
If mycon.State = ConnectionState.Closed Then
mycon.Open()
End If
Dim str As String
Dim ID As Integer
Dim table As New SqlParameter("@table", SqlDbType.VarChar, 10)
Dim Name As New SqlParameter("@Name", SqlDbType.Int, 4)
table.Value = s3
Name.Value = s2
str = "select * from @table Where Name=@Name"
' Dim da1 As New SqlDataAdapter(str, mycon)
' Dim myset1 As New DataSet
' da1.Fill(myset1, "Book")
' ID = myset1.Tables("Book").Rows(myset1.Tables("Book").Rows.Count - 1).Item("ID")'获取最高的ID号的代码
Dim mycom1 As New SqlCommand(str, mycon) '可以节约内存吗?
Dim myreader1 As SqlDataReader
mycom1.Parameters.Add(table)
mycom1.Parameters.Add(Name)
myreader1 = mycom1.ExecuteReader
While myreader1.Read
ID = myreader1("ID")
End While
Return ID
mycon.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'更新按钮的代码
'首先检验数据的合法性
If Not CheckData() Then
Exit Sub
End If
Try '取起ID
'再次打开mycon
If mycon.State = ConnectionState.Closed Then
mycon.Open()
End If
Dim ID2 As New SqlParameter("@ID2", SqlDbType.Int, 4)
ID2.Value = ID1
Dim sqlstr As String = "update Book set Name='" & ComboBox1.Text & "' ,Anchor='" & TextBox1.Text & "',Publisher='" & TextBox2.Text & "',Price='" & TextBox3.Text & "',PublishDate='" & TextBox4.Text & "' Where ID=@ID2 and Flag='0'"
Dim mycom As New SqlCommand(sqlstr, mycon)
mycom.Parameters.Add(ID2)
mycom.ExecuteNonQuery()
MessageBox.Show("更新成功!", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information)
mycon.Close()
'修改Book_HisTory中相应的字段
mycon.Open()
mycom = New SqlCommand("Update Book_HisTory Set BookName='" & ComboBox1.Text & "' Where BookID='" & ID1 & "'", mycon)
mycom.ExecuteNonQuery()
mycon.Close()
MessageBox.Show("在Book_HisTory中更改也成功了!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'删除按钮的代码
' If CheckData() = False Then
' Exit Sub
' End If
Try
If mycon.State = ConnectionState.Closed Then
mycon.Open()
End If
Dim sqlstr As String = "delete from Book where Name='" & ComboBox1.Text & "' and Anchor='" & TextBox1.Text & "' and Publisher='" & TextBox2.Text & "' and Flag='0'"
Dim mycom As New SqlCommand(sqlstr, mycon)
mycom.ExecuteNonQuery()
MessageBox.Show("删除成功!", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information)
mycon.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
ComboBox1.Text = ""
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
ComboBox1.Focus()
'在重新读数到ComBoBox1控件中,呵呵(有点失误)
Call ReadDataToComboBox()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'取消按钮的代码
Me.Close()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'将数据读入各个控件中
Try
If mycon.State = ConnectionState.Closed Then
mycon.Open()
End If
Dim sqlstr As String = "select Anchor,Publisher,Price,PublishDate from Book where Name='" & ComboBox1.Text & "' and Flag='0'"
Dim myreader As SqlDataReader
Dim mycom As New SqlCommand(sqlstr, mycon)
myreader = mycom.ExecuteReader()
While myreader.Read
TextBox1.Text = myreader("Anchor")
TextBox2.Text = myreader("Publisher")
TextBox3.Text = myreader("Price")
TextBox4.Text = myreader("PublishDate")
End While
mycon.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'取其ComboBox对应相的ID,呵呵,没想到真的很有用
If mycon.State = ConnectionState.Closed Then
mycon.Open()
End If
Dim str As String
str = "select * from Book Where Name='" & ComboBox1.Text & "' and Flag='0'"
Dim mycom1 As New SqlCommand(str, mycon)
Dim myreader1 As SqlDataReader
myreader1 = mycom1.ExecuteReader
While myreader1.Read
ID1 = myreader1("ID")
End While
mycon.Close()
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -