简体   繁体   中英

How to store selected data in a list in vb.net?

I have filtered data in a myRawData table where the resulting query will be inserted in myImportedData table.

The situation is that I am going to have some formatting in the filtered data before I will insert it into myImportedData.

My question is how to store the filtered data in a list? Because that is the easiest way for me to reiterate over the filtered data.

So far here is my code, It only store 1 data in the list.

    Public Sub ImportData()

    Dim con2 As MySqlConnection = New MySqlConnection("Data Source=server;Database=dataRecord;User ID=root;")

    con2.Open()
    Dim sql As MySqlCommand = New MySqlCommand("SELECT dataRec FROM myRawData WHERE dataRec LIKE '%20130517%' ", con2)

    Dim dataSet As DataSet = New DataSet()
    Dim dataAdapter As New MySqlDataAdapter()
    dataAdapter.SelectCommand = sql
    dataAdapter.Fill(dataSet, "dataRec")
    Dim datTable As DataTable = dataSet.Tables("dataRec")

    listOfCanteenSwipe.Add(Convert.ToString(sql.ExecuteScalar()))

    'ListBox1.Items.Add(listOfCanteenSwipe(0))

    End Sub

Example of data in the myRawData table is this:

    myRawData Table
     --------------------------
    ' id    '    dataRec
     --------------------------
    '  1    '    K10201305170434010040074A466
    '  2    '    K07201305170434010040074UN45   

Please help. Thank you.

EDIT:

What i just want to achieve is to store my filtered data in a list. I used list to loop over the filtered data - and I have no problem with that.

After storing in a list, i will now segragate the information in the dataRec field to be imported in the myImportedData table.

To add some knowledge, i will format the dataRec field just like below:

K07        ----> Loc
20130514   ----> date
0455       ----> time
010        ----> temp
18006D9566 ----> id

Try this

dim x as integer

for x = 0 to datTable.rows.count - 1
  listOfCanteenSwipe.Add(datTable.rows(x).item("datarec"))
next

Why not change your SQL statement to split the field out for you ?

You shuold use the power of SQL to perform any data manipulation you can perform at the server, It is far quicker, has less overhead on the server and was designed for this very purpose

SELECT 
SUBSTRING(dataRec,1,3) as [Loc]
,SUBSTRING(dataRec,4,8) as [date]
,SUBSTRING(dataRec,12,4) as [time]
,SUBSTRING(dataRec,16,3) as [temp]
,SUBSTRING(dataRec,19,10) as [Loc]
FROM myRawData WHERE dataRec LIKE '%20130517%' 

then load this directly into your .NET datatable "myImportedData"

After getting the data in Data Table you can convert it to a generic list. Then you can use this list for further operations:

List<MyType> list = dataTable.Rows.OfType<DataRow>()
    .Select(dr => dr.Field<MyType>(columnName)).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM