Home > OS >  How we can get a data table raw data into a Single type Array variable?
How we can get a data table raw data into a Single type Array variable?

Time:07-30

How we can get a data table raw data into a Single type Array variable?

Like;

Dim price_range1() As Single = {10.4, 9.6, 6.8, 5.6, 4.4}
Dim price_range2() As Single = {5.2, 4.8, 3.4, 2.8, 2.2}
Dim price_range3() As Single = {2.6, 2.4, 1.7, 1.4, 1.1}

I'm already getting all data into the DataGrid. But I need to get those raw data as a variable.

DataGridView Table

Like that kind of variables

Imports MySql.Data.MySqlClient

Public Class showitems

    Public commmand As New MySqlCommand

    Public adapter As New MySqlDataAdapter

    Public datatable As New DataTable

    Private Sub showitems_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim var As New ArrayList

        If Db_conn() = True Then

            Dim sql As String

            sql = "SELECT id, range1,range2,range3,range4,range5 FROM `pioltprice` WHERE id = 5 OR id = 6 OR id = 7 "

            command.CommandText = sql
            command.Connection = conn

            adapter.SelectCommand = command

            adapter.Fill(datatable)

            DataGridView1.DataSource = datatable

        Else

            MsgBox("Error occurred")

        End If

    End Sub

End Class

CodePudding user response:

Use MySql DataTable, MySql DataAdapter

CodePudding user response:

You will need to take the values and include them in your array.

If you want to reuse the DataTable that you're already filling, then you could do the following after adapter.Fill(datatable):

  1. Get the Row by its index
  2. Get the ItemArray of the selected DataRow
  3. Skip the first cell in the ItemArray (which is the Id)
  4. Convert the values to a Single
Dim price_range1() As Single
If (datatable.Rows().Count > 0) Then
    price_range1 = datatable.Rows().Item(0).ItemArray().Skip(1).Select(Function(cell) Single.Parse(cell)).ToArray()
End If

Dim price_range2() As Single
If (datatable.Rows().Count > 1) Then
    price_range2 = datatable.Rows().Item(1).ItemArray().Skip(1).Select(Function(cell) Single.Parse(cell)).ToArray()
End If

Dim price_range3() As Single
If (datatable.Rows().Count > 2) Then
    price_range3 = datatable.Rows().Item(2).ItemArray().Skip(1).Select(Function(cell) Single.Parse(cell)).ToArray()
End If

Live Demo: https://dotnetfiddle.net/pdEAkz

  • Related