ADO.NET: need help to understand the basics of a “data set”

As a context, I am new to ADO.NET and used David Seppa's “ADO.NET 2.0 Programming” to help me build my knowledge.

I am trying to understand the Dataset object, but I think that I may have completely misunderstood the essence and am looking for guidance.

As an example, I created a very simple combo box to fill in combobox with names in the database ("MyDatabase"). The following code works fine for me:

    Private Sub frmEmployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim strConn, strSQL As String
    strConn = "Data Source=.\SQLExpress;Initial Catalog=MyDatabase;Integrated Security=True;"
    strSQL = "SELECT LastName, FirstName FROM EmployeeTable"

    Dim da As New SqlDataAdapter(strSQL, strConn)
    Dim ds As New DataSet()
    da.Fill(ds, "AllEmployeesList")

    For i As Integer = 0 To ds.Tables("AllEmployeesList").Rows.Count - 1
        Dim row As DataRow = ds.Tables("AllEmployeesList").Rows(i)
        cbAllEmployeesList.Items.Add(row("LastName") & ", " & row("FirstName"))
    Next

End Sub

Now suppose I have a button in my form ("GetAge") that is designed to retrieve the age of the employee selected in the combo box from the "AllEmployeesList" dataset and display it in the text box in the same form.

, , , ? , Load? , ?

, . , , , Dataset . , Load, ?

, , Dataset. - ?

,

+3
5

, , , AllEmployeesList, . .

datagrid , .

, , - , .

+1

, , DataSet. , DataSet.

, WebForms, WinForms - . WinForms, DataSet - . , .

WebForms, . - , .

+1

, . , , - .

(, SqlCommand.ExecuteReader), , , , . - , SqlCommand.ExecuteScalar .

+1

Sub , , Dim, . , , , Sub/Function:

Public Class frmEmployee
    Private dsEmployeeList As DataSet

    Private Sub frmEmployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ...
    dsEmployeeList = New DataSet()
    da.Fill(dsEmployeeList, "AllEmployeesList")
    ...
    End Sub

    Private Sub GetAge_Click(sender As Object, e As EventArgs) Handles GetAge.Click
        Dim iRowIndex As Integer

        iRowIndex = cbAllEmployeesList.SelectedIndex 'In this case the rownumber is the same as the index of the selected item in the combobox

        'Check to see if an item from the combobox has been selected
        If iRowIndex >= 0 Then
            txtEmployeeAge.Text = dsEmployeeList.Tables("AllEmployeesList").Rows(iRowIndex).Item("Age").ToString()
        End If
    End Sub

, . : , .

+1

DataGrid DataSet. reqd DataSet DataGrid.DataSource DataSet.

:

    DataSet ds = new DataSet();
    // Code to populate DataSet from your DB
    ...
    ...

ds

this.dataGridView1.DataSource = ds;

,

DataSet retrievedFromGrid = (DataSet)this.dataGridView1.DataSource;

However, if you need to perform operations on this DataSet several times, and memory is not a problem, I would suggest that you save it in a class variable to avoid unnecessary casting costs in the DataSet from the DataGrid and again.

0
source

Source: https://habr.com/ru/post/1708489/


All Articles