📄 frmdepartment.vb
字号:
Imports System.Data.SqlClient
Imports System.IO
Public Class FrmDepartment
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 DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents TxtDepID As System.Windows.Forms.TextBox
Friend WithEvents LblDepID As System.Windows.Forms.Label
Friend WithEvents LblDepName As System.Windows.Forms.Label
Friend WithEvents TxtDepName As System.Windows.Forms.TextBox
Friend WithEvents LblDepMan As System.Windows.Forms.Label
Friend WithEvents TxtDepMan As System.Windows.Forms.TextBox
Friend WithEvents BtnExit As System.Windows.Forms.Button
Friend WithEvents BtnDel As System.Windows.Forms.Button
Friend WithEvents BtnEdit As System.Windows.Forms.Button
Friend WithEvents BtnAdd As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.BtnExit = New System.Windows.Forms.Button
Me.BtnDel = New System.Windows.Forms.Button
Me.BtnEdit = New System.Windows.Forms.Button
Me.BtnAdd = New System.Windows.Forms.Button
Me.LblDepMan = New System.Windows.Forms.Label
Me.TxtDepMan = New System.Windows.Forms.TextBox
Me.LblDepName = New System.Windows.Forms.Label
Me.TxtDepName = New System.Windows.Forms.TextBox
Me.LblDepID = New System.Windows.Forms.Label
Me.TxtDepID = New System.Windows.Forms.TextBox
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(8, 8)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(264, 296)
Me.DataGrid1.TabIndex = 0
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.BtnExit)
Me.GroupBox1.Controls.Add(Me.BtnDel)
Me.GroupBox1.Controls.Add(Me.BtnEdit)
Me.GroupBox1.Controls.Add(Me.BtnAdd)
Me.GroupBox1.Controls.Add(Me.LblDepMan)
Me.GroupBox1.Controls.Add(Me.TxtDepMan)
Me.GroupBox1.Controls.Add(Me.LblDepName)
Me.GroupBox1.Controls.Add(Me.TxtDepName)
Me.GroupBox1.Controls.Add(Me.LblDepID)
Me.GroupBox1.Controls.Add(Me.TxtDepID)
Me.GroupBox1.Location = New System.Drawing.Point(280, 8)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(128, 296)
Me.GroupBox1.TabIndex = 1
Me.GroupBox1.TabStop = False
'
'BtnExit
'
Me.BtnExit.Location = New System.Drawing.Point(16, 256)
Me.BtnExit.Name = "BtnExit"
Me.BtnExit.Size = New System.Drawing.Size(80, 24)
Me.BtnExit.TabIndex = 9
Me.BtnExit.Text = "退出"
'
'BtnDel
'
Me.BtnDel.Location = New System.Drawing.Point(16, 224)
Me.BtnDel.Name = "BtnDel"
Me.BtnDel.Size = New System.Drawing.Size(80, 24)
Me.BtnDel.TabIndex = 8
Me.BtnDel.Text = "删除院系"
'
'BtnEdit
'
Me.BtnEdit.Location = New System.Drawing.Point(16, 192)
Me.BtnEdit.Name = "BtnEdit"
Me.BtnEdit.Size = New System.Drawing.Size(80, 24)
Me.BtnEdit.TabIndex = 7
Me.BtnEdit.Text = "修改信息"
'
'BtnAdd
'
Me.BtnAdd.Location = New System.Drawing.Point(16, 160)
Me.BtnAdd.Name = "BtnAdd"
Me.BtnAdd.Size = New System.Drawing.Size(80, 24)
Me.BtnAdd.TabIndex = 6
Me.BtnAdd.Text = "添加院系"
'
'LblDepMan
'
Me.LblDepMan.Location = New System.Drawing.Point(8, 112)
Me.LblDepMan.Name = "LblDepMan"
Me.LblDepMan.Size = New System.Drawing.Size(96, 16)
Me.LblDepMan.TabIndex = 5
Me.LblDepMan.Text = "系主任:"
'
'TxtDepMan
'
Me.TxtDepMan.Location = New System.Drawing.Point(8, 128)
Me.TxtDepMan.Name = "TxtDepMan"
Me.TxtDepMan.Size = New System.Drawing.Size(104, 21)
Me.TxtDepMan.TabIndex = 4
Me.TxtDepMan.Text = ""
'
'LblDepName
'
Me.LblDepName.Location = New System.Drawing.Point(8, 64)
Me.LblDepName.Name = "LblDepName"
Me.LblDepName.Size = New System.Drawing.Size(96, 16)
Me.LblDepName.TabIndex = 3
Me.LblDepName.Text = "系别名称:"
'
'TxtDepName
'
Me.TxtDepName.Location = New System.Drawing.Point(8, 80)
Me.TxtDepName.Name = "TxtDepName"
Me.TxtDepName.Size = New System.Drawing.Size(104, 21)
Me.TxtDepName.TabIndex = 2
Me.TxtDepName.Text = ""
'
'LblDepID
'
Me.LblDepID.Location = New System.Drawing.Point(8, 17)
Me.LblDepID.Name = "LblDepID"
Me.LblDepID.Size = New System.Drawing.Size(96, 16)
Me.LblDepID.TabIndex = 1
Me.LblDepID.Text = "系别编号:"
'
'TxtDepID
'
Me.TxtDepID.Location = New System.Drawing.Point(8, 32)
Me.TxtDepID.Name = "TxtDepID"
Me.TxtDepID.Size = New System.Drawing.Size(104, 21)
Me.TxtDepID.TabIndex = 0
Me.TxtDepID.Text = ""
'
'FrmDepartment
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(416, 310)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "FrmDepartment"
Me.Text = "系别管理"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.GroupBox1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub FrmDepartment_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
BindData()
If DataGrid1.VisibleRowCount > 0 Then
bindTextBox(0)
End If
End Sub
Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
bindTextBox(Me.DataGrid1.CurrentCell.RowNumber)
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
If Me.TxtDepID.Text.Trim() = String.Empty Then
MessageBox.Show("系编号不能为空")
Me.TxtDepID.Focus()
ElseIf Not JudgeInt(TxtDepID.Text.Trim) Then
MessageBox.Show("系编号必须为整数")
Me.TxtDepID.Focus()
ElseIf JudgeKeyExist("departmentinfo", "departmentid", TxtDepID.Text.Trim) Then
MessageBox.Show("系编号已经存在")
Me.TxtDepID.Focus()
ElseIf Me.TxtDepName.Text.Trim() = String.Empty Then
MessageBox.Show("系名不能为空")
Me.TxtDepName.Focus()
ElseIf Me.TxtDepMan.Text.Trim() = String.Empty Then
MessageBox.Show("系主任不能为空")
Me.TxtDepMan.Focus()
Else
Me.InsertData()
End If
End Sub
Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
If Me.TxtDepID.Text.Trim() = String.Empty Then
MessageBox.Show("系编号不能为空")
Me.TxtDepID.Focus()
ElseIf Not JudgeInt(TxtDepID.Text.Trim) Then
MessageBox.Show("系编号必须为整数")
Me.TxtDepID.Focus()
ElseIf Not JudgeKeyExist("departmentinfo", "departmentid", TxtDepID.Text.Trim) Then
MessageBox.Show("要更新的记录集不存在")
Me.TxtDepID.Focus()
ElseIf Me.TxtDepName.Text.Trim() = String.Empty Then
MessageBox.Show("系名不能为空")
Me.TxtDepName.Focus()
ElseIf Me.TxtDepMan.Text.Trim() = String.Empty Then
MessageBox.Show("系主任不能为空")
Me.TxtDepMan.Focus()
Else
Me.UpdateData()
End If
End Sub
Private Sub BtnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDel.Click
If Me.TxtDepID.Text.Trim() = String.Empty Then
MessageBox.Show("系编号不能为空")
Me.TxtDepID.Focus()
ElseIf Not JudgeInt(TxtDepID.Text.Trim) Then
MessageBox.Show("系编号必须为整数")
Me.TxtDepID.Focus()
ElseIf Not JudgeKeyExist("departmentinfo", "departmentid", TxtDepID.Text.Trim) Then
MessageBox.Show("要删除的记录集不存在")
Me.TxtDepID.Text = ""
Me.TxtDepID.Focus()
Else
If MessageBox.Show("确定要删除?", "删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = DialogResult.OK Then
Me.DeleteData()
End If
End If
End Sub
Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
Private Sub BindData()
Try
Dim sql As String = "select Departmentid as '系别编号',department as '系别名称',depmanager as '系主任' from Departmentinfo"
Dim dt As DataTable = GetDataTable(sql)
If dt.Rows.Count <> 0 Then
DataGrid1.DataSource = dt
End If
Catch ex As Exception
WriteErr(ex)
End Try
End Sub
Private Sub bindTextBox(ByVal row As Int16)
Me.TxtDepID.Text = Me.DataGrid1.Item(row, 0)
Me.TxtDepName.Text = Me.DataGrid1.Item(row, 1)
Me.TxtDepMan.Text = Me.DataGrid1.Item(row, 2)
End Sub
Private Sub InsertData()
Dim DepID As Int16 = Int16.Parse(TxtDepID.Text.Trim)
Dim DepName As String = TxtDepName.Text.Trim
Dim DepMan As String = TxtDepMan.Text.Trim
Dim value As String = DepID.ToString + ",'" + DepName + "','" + DepMan + "'"
Dim sql As String = "INSERT INTO DEPARTMENTINFO (DEPARTMENTID, DEPARTMENT,DEPMANAGER) VALUES(" + value + ")"
Dim sqlCmd As New SqlCommand
sqlCmd.Connection = SqlCon
sqlCmd.CommandText = sql
Try
SqlCon.Open()
sqlCmd.ExecuteNonQuery()
MessageBox.Show("添加记录成功")
Catch ex As Exception
MessageBox.Show("添加记录失败")
WriteErr(ex)
Finally
SqlCon.Close()
End Try
BindData()
End Sub
Private Sub UpdateData()
Dim DepID As Int16 = Int16.Parse(TxtDepID.Text.Trim)
Dim DepName As String = TxtDepName.Text.Trim
Dim DepMan As String = TxtDepMan.Text.Trim
Dim sql As String = "UPDATE DEPARTMENTINFO SET DEPARTMENT='" + DepName + "',DEPMANAGER='" + DepMan + "' WHERE DEPARTMENTID= " + DepID.ToString
Dim sqlCmd As New SqlCommand
sqlCmd.Connection = SqlCon
sqlCmd.CommandText = sql
Try
SqlCon.Open()
sqlCmd.ExecuteNonQuery()
MessageBox.Show("修改记录成功")
Catch ex As Exception
MessageBox.Show("修改记录失败")
WriteErr(ex)
Finally
SqlCon.Close()
End Try
BindData()
End Sub
Private Sub DeleteData()
Dim DepID As Int16 = Int16.Parse(TxtDepID.Text.Trim)
Dim sql As String = "DELETE FROM DEPARTMENTINFO WHERE DEPARTMENTID= " + DepID.ToString
Dim sqlCmd As New SqlCommand
sqlCmd.Connection = SqlCon
sqlCmd.CommandText = sql
Try
SqlCon.Open()
sqlCmd.ExecuteNonQuery()
MessageBox.Show("删除记录成功")
Catch ex As Exception
MessageBox.Show("删除记录失败")
WriteErr(ex)
Finally
SqlCon.Close()
End Try
BindData()
bindTextBox(0)
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -