Load 메서드를 사용하여 데이터 소스의 행과 함께 DataTable을 로드할 수 있습니다. 이 메서드는 매우 간단한 형식으로 DataReader 단일 매개 변수를 승인하는 오버로드된 메서드입니다. 이 양식에서는 DataTable를 행과 함께 단순히 로드합니다. 필요에 따라 데이터를 LoadOption에 추가하는 방법을 제어하는 매개 변수를 지정할 수 있습니다.
매개 변수는 LoadOption 데이터 원본에서 들어오는 데이터가 테이블에 이미 있는 DataTable 데이터와 결합되는 방법을 설명하므로 이미 데이터 행이 포함된 경우에 특히 유용합니다. 예를 들어 기본값인 PreserveCurrentValues는 Added에서 행이 로 표시된 경우, 각 열의 값이 데이터 원본의 일치하는 행의 내용으로 설정되도록 지정합니다. 값은 Current 행이 추가 RowState 될 때 할당된 값을 유지하고 행의 값은 변경됨으로 설정됩니다.
다음 표에서는 LoadOption 열거형 값에 대해 간략하게 설명합니다.
| LoadOption 값 | 설명 |
|---|---|
| OverwriteRow | 들어오는 행의 값이 PrimaryKey에 이미 있는 행과 동일한 Original 경우 각 열의 값은 Current 들어오는 행의 값으로 바뀌고 RowState 속성은 변경되지 않음으로 설정됩니다.데이터 원본에 없는 DataTable 행은 RowState 값으로 추가됩니다.이 옵션은 실제로 데이터 원본의 DataTable 내용과 일치할 수 있도록 내용을 새로 고칩니다. |
| PreserveCurrentValues(기본값) | 들어오는 행의 값이 PrimaryKey에 이미 있는 행과 동일한 Original 경우 해당 값은 들어오는 행의 내용으로 설정되며 Current 값은 변경되지 않습니다.RowState가 Added 또는 Modified인 경우, Modified로 설정됩니다.RowState 삭제된 경우 삭제된 상태로 유지합니다.데이터 원본에 없는 DataTable 행이 추가되고 RowState변경되지 않음으로 설정됩니다. |
| UpdateCurrentValues | 들어오는 행에 있는 PrimaryKey 값이 DataTable에 이미 있는 행의 PrimaryKey 값과 동일하면, Current 값이 Original 값으로 복사되고, Current 값이 들어오는 행의 내용으로 설정됩니다.RowState와 DataTable에 추가된 경우, RowState가 추가된 상태로 남아 있습니다. 로 Modified 표시되거나 삭제된 행의 RowState 경우 수정됨입니다.데이터 원본에 없는 DataTable 행이 추가되고 RowState추가됨으로 설정됩니다. |
다음 샘플에서는 이 메서드를 사용하여 Load 데이터베이스에 있는 Northwind 직원의 생일 목록을 표시합니다.
Private Sub LoadBirthdays(ByVal connectionString As String)
' Assumes that connectionString is a valid connection string
' to the Northwind database on SQL Server.
Dim queryString As String = _
"SELECT LastName, FirstName, BirthDate " & _
" FROM dbo.Employees " & _
"ORDER BY BirthDate, LastName, FirstName"
' Open and fill a DataSet.
Dim adapter As SqlDataAdapter = New SqlDataAdapter( _
queryString, connectionString)
Dim employees As New DataSet
adapter.Fill(employees, "Employees")
' Create a SqlDataReader for use with the Load Method.
Dim reader As DataTableReader = employees.GetDataReader()
' Create an instance of DataTable and assign the first
' DataTable in the DataSet.Tables collection to it.
Dim dataTableEmp As DataTable = employees.Tables(0)
' Fill the DataTable with data by calling Load and
' passing the SqlDataReader.
dataTableEmp.Load(reader, LoadOption.OverwriteRow)
' Loop through the rows collection and display the values
' in the console window.
Dim employeeRow As DataRow
For Each employeeRow In dataTableEmp.Rows
Console.WriteLine("{0:MM\\dd\\yyyy}" & ControlChars.Tab & _
"{1}, {2}", _
employeeRow("BirthDate"), _
employeeRow("LastName"), _
employeeRow("FirstName"))
Next employeeRow
' Keep the window opened to view the contents.
Console.ReadLine()
End Sub