VB.Net参数计数不匹配

时间:2019-06-11 00:10:30

标签: vb.net

我正在编写一个将数组列表转换为数据表的函数,但出现错误“参数计数不匹配”。

我一直在寻找和搜索问题,但是找不到任何可以帮助我解决问题的东西。

```
   'Function that convert ArrayList to DataTable
Public Function ConvertArrayListToDataTable(ByVal MyAList As ArrayList) As DataTable

    Dim dt As New DataTable()
    For i As Integer = 0 To MyAList.Count - 1

        'create a Generic Object
        Dim values As Object = MyAList.Item(i)
        Dim properties() As PropertyInfo =  
   values.GetType().GetProperties()


    'Loop through each property, and add it as a column to the datatable

        For Each prop As PropertyInfo In properties
            Try
                Dim dc As DataColumn = New DataColumn(prop.Name)

                'Add the column definition to the datatable
                dc.DataType = prop.PropertyType
                dt.Columns.Add(dc)
            Catch ex As Exception
                MessageBox.Show(" Can't add column" & ex.Message)
            End Try

        Next

        'for each object in the list, loop through and add
        'the data to the database
        For Each o As Object In MyAList
            Try
                'create new row
                Dim row As DataRow = dt.NewRow()
                Dim pf() As PropertyInfo = o.GetType().GetProperties()

                For Each item As PropertyInfo In pf
                    row(item.Name) = item.GetValue(o, Nothing)
                Next

                dt.Rows.Add(row)
            Catch ex As Exception
                MessageBox.Show(" Conversionerror" & ex.Message)
            End Try
        Next
    Next
    Return dt
End Function
```

我看到代码也获得了表和列数,但是无法写入值。它在getvalue上失败。 该函数假定返回一个包含检索到的数据的数据表并填充datagrid

For Each prop As PropertyInfo In properties
            Try
                Dim dc As DataColumn = New DataColumn(prop.Name)
                dc.DataType = prop.PropertyType
                dt.Columns.Add(dc)
            Catch ex As Exception
                MessageBox.Show(" Can't add column" & ex.Message)
            End Try

Next

2 个答案:

答案 0 :(得分:0)

如果要从数据库填充DataTable,只需让DataTable找出数据类型。新代码未使用ArrayList。您可以创建一个类或结构,然后使用List(Of T)或绝对必要的List(Of Object)。仅测试普通的旧DataTable。

Private Sub FillDataGridView()
    Dim dt = New DataTable()
    Using cn As New SqlConnection(My.Settings.CoffeeConnection)
        Using cmd As New SqlCommand("Select * From Coffees;", cn)
            cn.Open()
            dt.Load(cmd.ExecuteReader)
        End Using
    End Using
    For Each col As DataColumn In dt.Columns
        Debug.Print(col.DataType.Name)
    Next
    DataGridView1.DataSource = dt
End Sub

答案 1 :(得分:0)

谢谢玛丽。这是一个很好的经验。我在挑战自己,看看如何将Datagrid从Arraylist填充到Datatable。我终于听从了您的建议,并按如下方式回顾了我的职能:

      Public Function FillDataGridView(SQLStatement As String) As DataTable
        'create an DataTable to hold the results
        Dim dt As New DataTable()
        Try
            Using con As New SqlConnection(ConnectionString)
                con.Open()
                Using cmd As New SqlCommand(SQLStatement, con)
                    'Get the reader
                    Using Reader = cmd.ExecuteReader()
                        dt.Load(Reader)
                    End Using
                End Using
            End Using
            For Each col As DataColumn In dt.Columns
                Debug.Print(col.DataType.Name)
            Next
        Catch ex As Exception
            Console.WriteLine("SQL retrieve row:" & ex.Message & Err.Number)
        Finally
            Call CloseConn()
        End Try
        Return dt
    End Function