我试图弄清楚如何创建一个方法来设置数据表上的主键

时间:2013-07-27 22:00:07

标签: vb.net methods datatable primary-key

我希望能够传递一个或多个列名称的字符串。然后,该方法将这些列设置为主键。

代码正在进行中

public sub setPK(ParamArray columnNames As String())
    Dim primaryKeyColumns As List(Of System.Data.DataColumn)
    For Each s As String In columnNames
        primaryKeyColumns.Add(dataTable.Columns(s))
    Next
    dataTable.PrimaryKey = New System.Data.DataColumn() {primaryKeyColumns}
End Sub

我将不胜感激任何帮助/建议。

1 个答案:

答案 0 :(得分:0)

Public Sub setPK(ParamArray columnNames As String())
    Dim primaryKeyColumns(columnNames.Count - 1) As System.Data.DataColumn
    For i As Integer = 0 To columnNames.Count - 1 Step 1
        primaryKeyColumns(i) = dataTable.Columns(columnNames(i))
    Next
    dataTable.PrimaryKey = primaryKeyColumns
End Sub