Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 将记录行访问到标签中,或将特定数据字段访问到价目表的特定标签中_Database_Vb.net_Ms Access_Labels_Data Retrieval - Fatal编程技术网

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

Database 将记录行访问到标签中,或将特定数据字段访问到价目表的特定标签中,database,vb.net,ms-access,labels,data-retrieval,Database,Vb.net,Ms Access,Labels,Data Retrieval,我有一个名为“Contacts”的access数据库,里面有:“PriceTable”,这个表有3列:/Piece/Price/Material/(显然也是“ID”),上面有24条预加载记录(每种材料6条:-塑料、木材、钢材、蜡-)。我想将每条记录加载到标签中,以形成价格表 第三列“material”只是供try组使用,在运行时以某种方式将类似的记录类型分为4个屏幕部分,但我也不知道 为了检索标签上的记录,我多次研究并发现了这段代码,在运行时创建了标签,但我没有找到工作 Try

我有一个名为“Contacts”的
access
数据库,里面有:“PriceTable”,这个表有3列:/Piece/Price/Material/(显然也是“ID”),上面有24条预加载记录(每种材料6条:-塑料、木材、钢材、蜡-)。我想将每条记录加载到标签中,以形成价格表 第三列“material”只是供try组使用,在运行时以某种方式将类似的记录类型分为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个(独立的
标签
1个用于工件,1个用于每个项目/记录的价格),然后问题是:如何获取特定标签上的特定记录?使用ID

遗憾的是,这并不像:

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

有人有一些代码吗?

很难使用你不懂的代码。尝试在表单上放置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

查找我用来了解如何在代码中使用数据库的更多信息的对象。

您可以使用
数据表
,但是
数据读取器
被证明比
数据适配器
更快。请尝试以下代码片段:

  '''''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

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


如果您决定使用单个
标签
控件而不是
DataGridView
,我建议您在表单中添加一个
表格布局面板
,然后添加其他控件。那样的话,你不需要;不要担心每个控件的定位。@zackraiyan什么?OleDBConnection构造函数有一个重载,它接受一个字符串。该字符串就是连接字符串。它们也是括号而不是括号。括号看起来像[]对不起,我的坏:(…希望你理解:)。。。。为什么要写这么多行,比如
cmd.connection=cn
??虽然我很欣赏你的知识,但我把它逐行地说了出来,以便更有启发性,而不是把所有的东西都扔进各种各样的构造器中。当我写我自己的时,我确实使用了构造函数。希望OP发现我们的解决方案有帮助:)@zackraiyan@Mary这是一个很好的建议,但我确实需要将特定的值放入特定的标签中,而不是一个
Datagridtable
,以显示
标签按照我想要的方式排列在表单中。为此,是否需要添加datagridview?(我必须隐藏它,我不想要网格!),我知道如何做,以及如何从数据库填充该表,问题是如何从
DatagridView
或直接从数据库(没有datagrid)获取特定值(如果可能的话)。现在,这不会编译。Dim cn As New OleDBConnection=“Your connection string”@zackraiyan该类在每次声明的第二次声明时都会出错,表示:`“年龄已在该类中声明为‘private Age As integer’。``您将C#和vb连接在一起。VB不区分大小写。为什么marry,我做错了什么:(?C#区分大小写,而不是VB.net中的VBin.net
String
s区分大小写我表格的正确顺序是:ID/Piece/Price/MAterial那么正确的代码必须是:
pieceboxs(x)。Text=dr.GetString(1)PriceBoxes(x)。Text=CStr(dr.GetDecimal(2))
访问“Price”colum具有货币格式,但出现错误<“转换无效”,然后我将列属性更改为数字,但错误相同,最后我将格式设置为“text”,但相同。如果使用
PriceBoxes(x)。text=dr.GetString(2)
i get“index out out of range”我的代码:问题在于:“Precio”格式我们正在查看哪种形式?“Precio”表单是我在第131-150行实现代码的地方,我只尝试了3个项目。您的代码看起来很好。只需添加其余标签并检查价格的数据类型,以确保它是字符串。
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