English 中文(简体)
将“努力清单”改为数据集或数据表
原标题:Converting List of String to either a Dataset or Datatable

I m目前从事一项涉及CSV档案的项目。 我最初使用微软。 提取所有材料。 然而,大量人口有时没有拖网,而且“空白”。

因此,在进行了一些研究之后,我找到了一个例子,说明我可以把一切都列入一个清单。 例如:

Dim lstExample As New List(Of String)().  

从这里,我可以使用1个蒸汽炉来 gr取和储存。 由于头3条线是垃圾,因此,我跳了这些车,阅读了所有线的其余部分,并阅读了约30列 com数据。

My problem now is that I m unable to get the lstExample into either a dataset or datatable. I even manually created a list and it still errors out.

The error occurs on this line:

dataRow(i) = itemProperties(i).GetValue(item, Nothing) saying "Parameter count mismatch."

任何将清单输入数据集/数据表的想法?

   Public Shared Sub ManualReadCSVFile(ByVal strFilePath As String)
    Dim lstExample As New List(Of String)()

    Using reader = New StreamReader("c:SFTP_TargetATL-536437.csv")
        For i As Integer = 0 To 2
            reader.ReadLine()
        Next

        While Not reader.EndOfStream
            Dim line = reader.ReadLine()
            Dim values = line.Split(",")

            lstExample.Add(values(0))

        End While

        reader.Dispose()
    End Using

     Dim list As New List(Of String)(New String() {"nile", _
         "amazon", _
         "yangtze", _
         "mississippi", _
         "yellow"})

    Dim dsTest As New DataSet
    dsTest = CreateDataSet(lstExample)
     dsTest = CreateDataset(list)

End Sub

Public Shared Function CreateDataSet(Of T)(ByVal list As List(Of T)) As DataSet
     list is nothing or has nothing, return nothing (or add exception handling)
    If list Is Nothing OrElse list.Count = 0 Then
        Return Nothing
    End If

     get the type of the first obj in the list
    Dim obj = list(0).[GetType]()

     now grab all properties
    Dim properties = obj.GetProperties()

     make sure the obj has properties, return nothing (or add exception handling)
    If properties.Length = 0 Then
        Return Nothing
    End If

     it does so create the dataset and table
    Dim dataSet = New DataSet()
    Dim dataTable = New DataTable()

     now build the columns from the properties
    Dim columns = New DataColumn(properties.Length - 1) {}
    For i As Integer = 0 To properties.Length - 1
        columns(i) = New DataColumn(properties(i).Name, properties(i).PropertyType)
    Next

     add columns to table
    dataTable.Columns.AddRange(columns)

     now add the list values to the table
    For Each item In list
         For Each item As T In list
         create a new row from table
        Dim dataRow = dataTable.NewRow()

         now we have to iterate thru each property of the item and retrieve it s value for the corresponding row s cell
        Dim itemProperties = item.[GetType]().GetProperties()

        For i As Integer = 0 To itemProperties.Length - 1
            dataRow(i) = itemProperties(i).GetValue(item, Nothing)
        Next

         now add the populated row to the table
        dataTable.Rows.Add(dataRow)
    Next

     add table to dataset
    dataSet.Tables.Add(dataTable)

     return dataset
    Return dataSet
End Function
最佳回答
问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签