2013-07-27 36 views
0

我希望能夠傳遞一個或多個字段名稱的字符串。然後該方法將這些列設置爲主鍵。正在進行我想弄清楚如何創建一個方法來設置數據表上的主鍵

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 

代碼,我希望得到任何幫助/建議。

回答

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 
相關問題