📄 bookadd.vb
字号:
Imports System.Data.Sqlclient
Public Class BookAdd
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 Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents TextBox6 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.Label2 = New System.Windows.Forms.Label
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.Label4 = New System.Windows.Forms.Label
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.Label5 = New System.Windows.Forms.Label
Me.TextBox5 = New System.Windows.Forms.TextBox
Me.Label6 = New System.Windows.Forms.Label
Me.TextBox6 = 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()
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(56, 40)
Me.Label2.Name = "Label2"
Me.Label2.TabIndex = 2
Me.Label2.Text = "书籍名称"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(200, 40)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(160, 21)
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = ""
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(56, 96)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(104, 23)
Me.Label3.TabIndex = 4
Me.Label3.Text = "作 者"
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(200, 88)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(160, 21)
Me.TextBox3.TabIndex = 5
Me.TextBox3.Text = ""
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(56, 144)
Me.Label4.Name = "Label4"
Me.Label4.TabIndex = 6
Me.Label4.Text = "出 版 社"
'
'TextBox4
'
Me.TextBox4.Location = New System.Drawing.Point(200, 136)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.Size = New System.Drawing.Size(160, 21)
Me.TextBox4.TabIndex = 7
Me.TextBox4.Text = ""
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(56, 192)
Me.Label5.Name = "Label5"
Me.Label5.TabIndex = 8
Me.Label5.Text = "书籍定价"
'
'TextBox5
'
Me.TextBox5.Location = New System.Drawing.Point(200, 184)
Me.TextBox5.Name = "TextBox5"
Me.TextBox5.Size = New System.Drawing.Size(160, 21)
Me.TextBox5.TabIndex = 9
Me.TextBox5.Text = ""
'
'Label6
'
Me.Label6.Location = New System.Drawing.Point(56, 248)
Me.Label6.Name = "Label6"
Me.Label6.TabIndex = 10
Me.Label6.Text = "出版日期"
'
'TextBox6
'
Me.TextBox6.Location = New System.Drawing.Point(200, 248)
Me.TextBox6.Name = "TextBox6"
Me.TextBox6.Size = New System.Drawing.Size(160, 21)
Me.TextBox6.TabIndex = 11
Me.TextBox6.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(80, 304)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 12
Me.Button1.Text = "增加"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(192, 304)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 13
Me.Button2.Text = "清空"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(296, 304)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 14
Me.Button3.Text = "取消"
'
'BookAdd
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.BackColor = System.Drawing.SystemColors.ActiveBorder
Me.ClientSize = New System.Drawing.Size(456, 365)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox6)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.TextBox5)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Label2)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.MaximizeBox = False
Me.Name = "BookAdd"
Me.Text = "新增书籍"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub ClearData()
'清空各个控件数据的过程
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
End Sub
Private Function CheckData() As Boolean
'检查数据合法性的过程
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
End If
If TextBox4.Text = "" Then
MessageBox.Show("出版社选相不能为空!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return False
End If
If TextBox5.Text = "" Then
MessageBox.Show("书籍定价不能为空!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return False
Else
If IsNumeric(TextBox5.Text) = False Then
MessageBox.Show("输入的书籍定价不是整数!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return False
End If
End If
If TextBox6.Text = "" Then
MessageBox.Show("日期选项不能为空!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return False
Else
If IsDate(TextBox6.Text) = False Then '判断当前超过当前日期
MessageBox.Show("输入的日期格式不正确!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TextBox6.Text = ""
Return False
End If
End If
Return True
End Function
Private Sub BookAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ClearData()
TextBox2.Focus()
End Sub
Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress
If Asc(e.KeyChar) = System.Windows.Forms.Keys.Enter Then
Button1.PerformClick()
End If
End Sub
Private Function GetHeightID() As Integer
'定义一个函数获取其最大的ID号
If mycon.State = ConnectionState.Closed Then
mycon.Open()
End If
Dim Id As Integer
Dim myset1 As New DataSet
Dim myda1 As New SqlDataAdapter("select ID,Name,Anchor,Publisher,Price,PublishDate from Book", mycon)
myda1.Fill(myset1, "Book")
If myset1 Is Nothing OrElse myset1.Tables("Book").Rows.Count = 0 Then
Id = 1
Return Id
Else
'得到最大的ID
Id = myset1.Tables("Book").Rows(myset1.Tables("Book").Rows.Count - 1).Item("ID")
Return Id
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'增加书籍按钮的代码
'首先检验数据的合法性
If CheckData() = False Then
Exit Sub
End If
Try
If mycon.State = ConnectionState.Closed Then
mycon.Open()
End If
Dim ID As Integer
ID = GetHeightID()
Dim ID1 As New SqlParameter("@ID1", SqlDbType.Int, 4)
ID1.Value = ID + 1
Dim sqlstr As String = "insert into Book values(@ID1,'" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "',1)"
Dim mycom As New SqlCommand(sqlstr, mycon)
mycom.Parameters.Add(ID1)
mycom.ExecuteNonQuery()
MessageBox.Show("恭喜你,保存成功!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information)
Call ClearData()
mycon.Close()
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
'退出按钮的代码
ClearData()
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 Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -