⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmbookcat.vb

📁 一个简单的图书管理系统,只为业余者学习了解方便之用.
💻 VB
字号:
Imports System.Data
Imports System.Data.SqlClient

Public Class FrmBookCat
    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 lstBookCat As System.Windows.Forms.ListBox
    Friend WithEvents TxtCatName As System.Windows.Forms.TextBox
    Friend WithEvents BtnAdd As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lstBookCat = New System.Windows.Forms.ListBox
        Me.TxtCatName = New System.Windows.Forms.TextBox
        Me.BtnAdd = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'lstBookCat
        '
        Me.lstBookCat.ItemHeight = 12
        Me.lstBookCat.Location = New System.Drawing.Point(8, 8)
        Me.lstBookCat.Name = "lstBookCat"
        Me.lstBookCat.Size = New System.Drawing.Size(272, 220)
        Me.lstBookCat.TabIndex = 0
        '
        'TxtCatName
        '
        Me.TxtCatName.Location = New System.Drawing.Point(8, 232)
        Me.TxtCatName.Name = "TxtCatName"
        Me.TxtCatName.TabIndex = 1
        Me.TxtCatName.Text = "TextBox1"
        '
        'BtnAdd
        '
        Me.BtnAdd.Location = New System.Drawing.Point(120, 232)
        Me.BtnAdd.Name = "BtnAdd"
        Me.BtnAdd.TabIndex = 2
        Me.BtnAdd.Text = "添加"
        '
        'FrmBookCat
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(288, 261)
        Me.Controls.Add(Me.BtnAdd)
        Me.Controls.Add(Me.TxtCatName)
        Me.Controls.Add(Me.lstBookCat)
        Me.Name = "FrmBookCat"
        Me.Text = "FrmBookCat"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub FrmBookCat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '添加到数据库
        '定义如何联接
        Dim connStr As String
        connStr = "Server=BJS1;Database=BookSystem;User ID=sa;Password=;Trusted_Connection=False"
        '定义一个连接对象,连接到数据库
        Dim myBookConn As New SqlConnection(connStr)
        myBookConn.Open()
        '定义一个执行对象,用于Sql语句执行
        Dim myBookCommand As New SqlCommand("select * from bookcat")
        '执行Sql语句
        myBookCommand.Connection = myBookConn
        '获取数据
        Dim myReader As SqlDataReader
        myReader = myBookCommand.ExecuteReader(CommandBehavior.CloseConnection)
        '在列表框上显示
        Do While myReader.Read
            lstBookCat.Items.Add(myReader.Item("catName"))
        Loop

    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        '在界面上添加
        lstBookCat.Items.Add(TxtCatName.Text)
        '添加到数据库
        '定义如何联接
        Dim connStr As String
        connStr = "Server=BJS1;Database=BookSystem;User ID=sa;Password=;Trusted_Connection=False"
        '定义一个连接对象,连接到数据库
        Dim myBookConn As New SqlConnection(connStr)
        myBookConn.Open()
        '定义一个执行对象,用于Sql语句执行
        Dim myBookCommand As New SqlCommand("insert into bookcat (catName) values('" + TxtCatName.Text + "')")
        '执行Sql语句
        myBookCommand.Connection = myBookConn

        myBookCommand.ExecuteNonQuery()
        myBookConn.Close()

    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -