使用vb.net使用()列表来制作网格视图

时间:2017-12-06 00:13:05

标签: mysql vb.net

我有一个名为BOOK的类,其中包含信息,我希望使用()或数组的列表在DELETE_BOOKS类的数据网格中填充本书的信息。但是当我试图运行代码时,信息没有显示出来。请告诉我代码中是否有错误。 fyi,我不擅长编程。 这是我的代码:

Imports MySql.Data.MySqlClient

Public Class CONNECTION
    Protected con As New MySqlConnection
    Protected da As New MySqlDataAdapter
    Protected ds As New DataSet
    Protected cmds As New MySqlCommand
    Protected dr As MySqlDataReader
    Protected dbcon, sql As String
    Protected dt As DataTable

    Public Sub New()
        con = New MySqlConnection
    End Sub
    Public Sub opendb()
        con.ConnectionString = "server=localhost;User Id=root;database=school_library"
        con.Open()
    End Sub
    Public Sub closedb()
        con.Close()
    End Sub
End Class
Imports MySql.Data.MySqlClient

Public Class DELETE_BOOKS
    Private Sub DELETE_BOOKS_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'lload()
        Dim bk As New List(Of BOOK)
        bindgrid(bk)
    End Sub
    Private Sub bindgrid(ByVal bk As List(Of BOOK))
        DataGridView1.DataSource = bk
    End Sub
Imports MySql.Data.MySqlClient

Public Class BOOK
    Inherits CONNECTION

    Dim cmd As New MySqlCommand
    Private titles As String
    Private authors As String
    Private pubs As String
    Private cond As String
    Private cats As String
    'Private quan As Integer
    Private avail_quan As Integer
    Private shelfinfo As Integer
    Private phys As String

    Public Sub New()

    End Sub
    'Public Sub New(titles As String, authors As String, pubs As String, cond As String, cats As String, quan As Integer, avail_quan As Integer, shelfinfo As Integer, phys As String)
    Public Sub New(titles As String, authors As String, pubs As String, cond As String, cats As String, avail_quan As Integer, shelfinfo As Integer, phys As String)
        Me.titles = titles
        Me.authors = authors
        Me.pubs = pubs
        Me.cond = cond
        Me.cats = cats
        'Me.quan = quan
        Me.avail_quan = avail_quan
        Me.shelfinfo = shelfinfo
        Me.phys = phys
    End Sub

        Public Function populatedata() As List(Of BOOK)

        Try
            cmd = New MySqlCommand("SELECT *FROM books", con)
            opendb()
            dr = cmd.ExecuteReader
            Dim bk As New List(Of BOOK)

            While dr.Read
                bk.Add(New BOOK)
                bk((bk.Count - 1)).titles = dr("Title").ToString
                bk((bk.Count - 1)).authors = dr("Author").ToString
                bk((bk.Count - 1)).pubs = dr("Publisher").ToString
                bk((bk.Count - 1)).cond = dr("Condition").ToString
                bk((bk.Count - 1)).cats = dr("Category").ToString
                bk((bk.Count - 1)).avail_quan = dr("Quantity").ToString
                bk((bk.Count - 1)).shelfinfo = dr("Shelf Info").ToString
                bk((bk.Count - 1)).phys = dr("Physical Status").ToString
            End While
            con.Close()
            Return bk
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function

0 个答案:

没有答案