Demo entry 6686153

1

   

Submitted by anonymous on Dec 22, 2017 at 14:23
Language: VB.net. Code size: 9.8 kB.

Public Class AdminBookEdit
    Dim objDS As New DataSet
    Dim objXSTable As DataTable
    Dim objda As New SqlClient.SqlDataAdapter
    Dim CheckResult As Boolean
    Dim cn As New SqlClient.SqlConnection("server=.;uid=ad;pwd=12345;database=LMSS")
    Public Sub refreshdata()
        objda.Update(objDS, "BookInfo")  '第二个参数为需要更新数据库表名   
        DataGridView1.DataSource = objXSTable
    End Sub

    Private Sub AdminBookEdit_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim connstr, sqlcmd1, sqlcmd2 As String
        connstr = "UID=ad;pwd=12345;database=LMSS;server=."
        sqlcmd1 = "select * from BookInfo"
        sqlcmd2 = "select * from BookInfo"
        conn = New SqlConnection(connstr)
        conn.Open()
        da1 = New SqlDataAdapter(sqlcmd1, conn)
        da2 = New SqlDataAdapter(sqlcmd2, conn)
        ds = New DataSet()
        da1.Fill(ds, "BookInfo")
        da2.Fill(ds, "BookInfo")
        Dim cmd As New SqlClient.SqlCommand
        cmd.Connection = cn   '数据从当前建立的连接cn中进入
        Dim builder As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(objda)
        cn.Open()
        cmd.CommandText = "select * from BookInfo"
        objda.SelectCommand = cmd
        objda.Fill(objDS, "BookInfo")
        cn.Close()
        objXSTable = objDS.Tables("BookInfo")
        DataGridView1.DataSource = ""
        DataGridView1.DataSource = objXSTable
        Timer1.Enabled = True

    End Sub

    Private Sub SearchBtn_Click(sender As Object, e As EventArgs) Handles SearchBtn.Click
        If TextBox1.Text <> "" Or TextBox2.Text <> "" Or Catalog.Text <> "" Or TextBox3.Text <> "" Then
            Dim sqlcmd1, sqlcmd2, sqlcmd3 sqlcmd4 As String
            sqlcmd1 = "select * from BookInfo where 正题名=" & "'" & TextBox1.Text & "'"
            sqlcmd2 = "select * from BookInfo where 责任者=" & "'" & TextBox2.Text & "'"
            sqlcmd3 = "select * from BookInfo where 图书分类=" & "'" & Catalog.Text & "'"
            sqlcmd4 = "select * from BookInfo where 索取号=" & "'" & TextBox3.Text & "'"
            da1 = New SqlDataAdapter(sqlcmd1, conn)
            da2 = New SqlDataAdapter(sqlcmd2, conn)
            da3 = New SqlDataAdapter(sqlcmd3, conn)
            da4 = New SqlDataAdapter(sqlcmd4, conn)
            ds = New DataSet()                 '创建dataset
            da1.Fill(ds, "BookInfo")           '填充datsset
            da2.Fill(ds, "BookInfo")           '填充datsset
            da3.Fill(ds, "BookInfo")           '填充datsset
            da4.Fill(ds, "BookInfo")           '填充datsset
            If ds.Tables(0).Rows.Count <> 0 Then
                '绑定DataGridView
                DataGridView1.DataSource = ds.Tables("BookInfo")
                '创建与数据相对应的BindingMangerBase 对象 bm
                bm = Me.BindingContext(ds, "BookInfo")
            Else
                MsgBox("抱歉,未查到与此信息匹配的图书。", vbOKOnly, "请注意")
                TextBox2.Text = ""
                TextBox3.Text = ""
                TextBox1.Focus()
                TextBox1.SelectAll()
            End If
        Else
            MsgBox("查询项不能为空。", vbOKOnly, "请注意")
        End If
    End Sub

    Private Sub ResetBtn_Click(sender As Object, e As EventArgs) Handles ResetBtn.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        Catalog.SelectedIndex = -1
        DataGridView1.DataSource = ""
        txtBookNum.Text = ""
        txtBookNum.Text = ""
        txtTitle.Text = ""
        txtAuthor.Text = ""
        txtPublisher.Text = ""
        txtPrice.Text = ""
        txtClassification.SelectedIndex = -1
        txtShelf.SelectedIndex = -1
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Timer1.Interval = 1000
        CurrentTime.Text = "今天是:" & Year(Today) & "/" & Month(Today) & "/" & DateAndTime.Day(Now) & vbCr & "北京时间:" & TimeOfDay
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        txtTitle.ForeColor = Color.Black
        txtBookNum.Text = DataGridView1.CurrentRow.Cells.Item(0).Value.ToString
        txtTitle.Text = DataGridView1.CurrentRow.Cells.Item(1).Value.ToString
        txtAuthor.Text = DataGridView1.CurrentRow.Cells.Item(2).Value.ToString
        txtPublisher.Text = DataGridView1.CurrentRow.Cells.Item(3).Value.ToString
        txtPrice.Text = DataGridView1.CurrentRow.Cells.Item(4).Value.ToString
        txtClassification.Text = DataGridView1.CurrentRow.Cells.Item(5).Value.ToString
        txtShelf.Text = DataGridView1.CurrentRow.Cells.Item(6).Value.ToString()
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim response As MsgBoxResult
        response = MsgBox("确认是否将当前信息录入为新书?", vbOKCancel + vbQuestion, "请注意")
        If response = MsgBoxResult.Ok Then
            If txtBookNum.Text <> "" Then
                CheckBookNum()
                '对当前输入的索取号与数据库信息做匹配,若发生冲突则进行判断。
                If CheckResult = True Then
                    Dim myRow As DataRow
                    myRow = objXSTable.NewRow
                    myRow("索取号") = txtBookNum.Text
                    myRow("正题名") = txtTitle.Text
                    myRow("责任者") = txtAuthor.Text
                    myRow("出版者") = txtPublisher.Text
                    myRow("零售单价") = Val(txtPrice.Text)
                    myRow("图书分类") = txtClassification.Text
                    myRow("存书处") = txtShelf.Text
                    objXSTable.Rows.Add(myRow)
                    'objda.InsertCommand = New SqlClient.SqlCommand("insert into BookInfo (索取号,正题名,责任者,出版者,零售单价,图书分类,存书处) values('" & txtBookNum.Text & "','" & txtTitle.Text & "','" & txtAuthor.Text & "','" & txtPublisher.Text & "','" & Val(txtPrice.Text) & "','" & txtClassification.Text & "','" & txtShelf.Text & "')", cn)
                    objda.Update(objDS, "BookInfo")
                    objDS.AcceptChanges()
                    refreshdata()
                    'DataGridView1.FirstDisplayedScrollingRowIndex = DataGridView1.RowCount - 1
                    MsgBox("成功录入了新书。", vbOKOnly, "请注意")
                ElseIf CheckResult = False Then
                    MsgBox("您正在录入的图书索取号已经存在。" & vbCrLf & "请检查输入并键入新的索取号。", vbOKOnly, "请注意")
                Else
                    MsgBox("您不能录入索取号为空的图书。" & vbCrLf & "请键入索取号。", vbOKOnly, "请注意")
                End If
            End If
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        txtBookNum.Text = ""
        txtBookNum.Text = ""
        txtTitle.Text = ""
        txtAuthor.Text = ""
        txtPublisher.Text = ""
        txtPrice.Text = ""
        txtClassification.SelectedIndex = -1
        txtShelf.SelectedIndex = -1
    End Sub

    Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
        Dim response As MsgBoxResult
        response = MsgBox("确认是否更新当前修改的信息?", vbOKCancel + vbQuestion, "请注意")

        If response = MsgBoxResult.Ok Then
            objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Item(0) = txtBookNum.Text
            objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Item(1) = txtTitle.Text
            objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Item(2) = txtAuthor.Text
            objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Item(3) = txtPublisher.Text
            objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Item(4) = Val(txtPrice.Text)
            objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Item(5) = txtClassification.Text
            objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Item(6) = txtShelf.Text
            objda.Update(objDS, "BookInfo")
            objDS.AcceptChanges()
            refreshdata()
        End If
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        DeleteRow()
    End Sub

    Sub DeleteRow()
        Dim cmd As New SqlClient.SqlCommand
        Dim response As MsgBoxResult
        response = MsgBox("确认是否删除当前选择的信息?", vbOKCancel + vbQuestion, "请注意")
        If response = MsgBoxResult.Ok Then
            objXSTable.Rows.Item(DataGridView1.CurrentRow.Index).Delete()
            cmd.CommandText = "DELETE FROM BookInfo WHERE 索取号 = " + "'" + txtBookNum.Text + "'"
            '通过指定Delete的SQL语句命令,可以不顾及数据库主键问题
            objda.DeleteCommand = cmd
            refreshdata()
        End If
    End Sub

    Private Sub txtTitle_Validated(sender As Object, e As EventArgs) Handles txtTitle.Validated
        txtTitle.ForeColor = Color.Red
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Me.Hide()
        AdminForm.Show()
    End Sub

    Private Sub CheckBookNum()
        Dim sqlCnStr As String = "server=.;uid=ad;pwd=12345;database=LMSS"
        Dim sqlCn As SqlConnection = New SqlConnection(sqlCnStr)
        Dim UserDataTable As New DataTable    '定义一个DataTable对象
        Dim sql As String = "select * from BookInfo where 索取号=" & "'" & txtBookNum.Text & "'"
        Dim cmd As SqlCommand = New SqlCommand(sql, sqlCn)     '定义sqlCommand对象
        Dim read As IDataReader
        sqlCn.Open()
        read = cmd.ExecuteReader()
        UserDataTable.Load(read)
        If UserDataTable.Rows.Count <> 0 Then
            If txtBookNum.Text = UserDataTable.Rows(0).Item("索取号") Then
                CheckResult = False
            End If
        Else
            CheckResult = True
        End If
        sqlCn.Close()
    End Sub
End Class

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).