访问记录行到标签或特定数据字段到特定标签的价目表

时间:2018-03-30 05:47:48

标签: database vb.net ms-access labels data-retrieval

我有一个名为“Contacts”的access数据库:“PriceTable”,这个表有3列:/ Piece / Price / Material /(显然也是“ID”),上面有24个预加载的记录(6每种材料: - 塑料,木材,钢,蜡 - )。我想将每条记录加载到标签中,以构建为价格表的形式 第三列“材料”它只是用于尝试组在运行时以某种方式类似记录类型到4个屏幕部分,但我也不知道。:

要检索标签上的记录,我研究并多次发现此代码,在运行时创建标签,但我无法达到工作

   Try
        Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Contacts.accdb;Persist Security Info=False;")
        cn.Open()
        Dim cmd As New OleDbCommand
        cmd.Connection = cn
        cmd.CommandText = "Select * from PriceTable"
        cmd.Prepare()
        Dim Piece = cmd.ExecuteReader
        Dim posY As Integer = 100
        With Trabajos
            .Read()
            For Each item In Piece
                Dim newlab As New Label
                newlab.Name = item("ID")
                newlab.Location = New Point(25, posY)
                posy += 35
                newlab.Font = Font
                newlab.Text = .Item("Piece") + " - " + .Item("Price")
                newlab.Visible = True
                Me.Controls.Add(newlab)
                newlab.BringToFront()
            Next
            .Close()
        End With

        cn.Close()
    Catch
   End Try

或者可能更容易创建和定位48(每个项目/记录的独立labels 1和每个项目/记录的价格1)然后问题是:如何获取特定标签上的特定记录? / strong>使用ID?

可惜它不是那么容易:

Label1.text = Piece("ID"(2))
Label2.text = Price("ID"(2))
Label4.text = Piece("ID"(3))
Label5.text = Price("ID"(3))

任何人都有一些代码?

3 个答案:

答案 0 :(得分:0)

很难使用您不了解的代码。尝试在表单上放置一个DataGridView,然后使用此代码。

Private Sub FillDataGrid()
        Dim cn As New OleDBConnection("Your connection string")
        Dim cmd As New OleDbCommand
        Dim da As New OleDBDataAdapter
        Dim dt As New DataTable
        cmd.Connection = cn
        cmd.CommandText = "Select * From PriceTable;"
        da.SelectCommand = cmd
        da.Fill(dt)
        DataGridView1.DataSource = dt
    End Sub

查找我以前用于了解如何在代码中使用数据库的更多信息。

答案 1 :(得分:0)

您可以使用DataTable,但DataReader被证明比DataAdapter更快。请尝试使用以下代码段:

  '''''create a class to handle all the data
  Public Class MyData

Private age1 As Integer

Public Property Age As Integer
    Get
        Return age
    End Get

    Set(ByVal value As Integer)
        age = value
    End Set
End Property

Private name1 As String

Public Property Name As String
    Get
        Return name
    End Get

    Set(ByVal value As String)
        name = value
    End Set
End Property

Private id1 As Integer

Public Property Id As Integer
    Get
        Return id
    End Get

    Set(ByVal value As Integer)
        id = value
    End Set
End Property
End Class


'''Then we create an Array-list

 Dim data as New ArrayList

'''Then we just use a datareader

  Dim cn As New OleDBConnection("Your connection string")
  Dim cmd As New OleDbCommand("YOur sqlCommand here",cn)
  DIm dr as SqlDataReader = cmd.executeReader
  While dr.read 
    Dim dt as New MyData
    dt.Id = dt(0)
    dt.Name = dt(1)
    dt.Age = dt(2)
    data.Add(dt)
  End while

  '''Then we set the dataGridview's datasource

   dataGridView1.DataSource = data

但如果您真的想使用标签来显示数据,请执行以下操作:

 Dim dt as New DataTable ....
 ....
 ......
 da.Fill(dt)
 Label1.Text = dt(0)(1) '''0 i row count , 1 is column count , change it per your requirement

答案 2 :(得分:0)

我使用TextBoxes设置为只读,因为它们在设计时更容易看到和排队。然后我为您要显示的每列创建了列表。我只做了三个记录,但你需要全部24个,所以每个列表将有24个元素。然后使用DataReader填充文本框来遍历数据。在dr.GetString(1)中,1指的是列的基于零的索引。对于价格,您可能会使用dr.GetDouble(价格列的索引)

Private Sub TestControlArrays_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim PriceBoxes As New List(Of TextBox)
        Dim PieceBoxes As New List(Of TextBox)
        PieceBoxes.Add(TextBox1)
        PriceBoxes.Add(TextBox2)
        PieceBoxes.Add(TextBox3)
        PriceBoxes.Add(TextBox4)
        PieceBoxes.Add(TextBox5)
        PriceBoxes.Add(TextBox6)
        Using cn As New SqlConnection("Your Connection String")
            Using cmd As New SqlCommand("Select * From Price", cn)
                cn.Open()
                Dim dr As SqlDataReader = cmd.ExecuteReader
                For x As Integer = 0 To 23
                    dr.Read()
                    PieceBoxes(x).Text = dr.GetString(1)
                    PriceBoxes(x).Text = CStr(dr.GetInt32(0))
                Next
                cn.Close()
            End Using
        End Using
End Sub