ADO.NET: DataSet or DataTable and Data Retrieval Methods

SO community

I am starting out with ADO.NET and am fairly new to programming. I had some support already from this community, which was very helpful and had a different question.

Simply put, I create a simple window form with the name of the employee and the button, so that when the user clicks the button, the selected email address of the employee is displayed in the text box of the form.

In my project, I have a database and upload a form that I connect to my database and create a datatable, as shown below:

Public Class GetEmployeeDataset

Private tbl As New DataTable

Public Sub New()
    Dim strConn, strSQL As String
    strConn = "Data Source=.\SQLExpress;Initial Catalog=MyDatabase;Integrated Security=True;"
    strSQL = "SELECT * FROM EmployeeTable"

    Dim da As New SqlDataAdapter(strSQL, strConn)
    da.Fill(tbl)
End Sub

End Class

At this point, I have two questions:

  • DataTable DataSet, , , DataSet DataTables, DataTable (5 , 100 ), DataTable, - ?
  • , DataTable ('tbl' ), DataTable , .. EmailAddress ? SQL "SELECT EmailAddress WHERE EmployeeName = SelectedItem". DataTable, , DataView, - .

- ?

+2
5

DataSet DataTables. , , - " ", , .

, , , DataTable.

: , :

  • , .
  • Select DataTable ( DataRows, where)
+1

ADO, DataView...

  Dim dv As DataView
  Dim strFilter As String
  Dim strEmail As String = ""  

  strFilter = "EmployeeName = '" & cbo.Text & "'"  

  dv = tbl.DefaultView
  dv.RowFilter = strFilter  

  If dv.Count > 0 Then
     strEmail = dv(0).Item("EmailAddress").ToString
  End If
+1

:

string email = string.Empty;
foreach (Row row in tbl.Rows)
{
    string employeeName = (string)row["EmployeeName"];
    if (employeeName == "John")
    {
        email = (string)row["Email"];
        break;
    }
}

LINQ:

var email = (from row in tbl.Rows
            where (string)row["EmployeeName"] == "John"
            select (string)row["Email"]).First ();

VS, " ". #, VB.

0
  • DataSet DataTables , :: shudder:: DataTable.

  • , . "SELECT * FROM EmployeeTable", - THEN . , , , , .

, Linq to SQL , ADO.NET. , , ( ). Linq to SQL, , SQL ( "SELECT * FROM Employees" , "WHERE Name" = "", ).

, ADO.NET , :

yourDataTable.Select("EmployeeName='" + yourSelectedValue + "'")
0

100 ( 1 000 000, ) ExecuteScalar SqlCommand. , , - . * where, , , , .

EmailAddress .

Using cn as new SqlConnection("Data Source=.\SQLExpress;Initial Catalog=MyDatabase;Integrated Security=True;")
    Using cmd as new SqlCommand("SELECT EmailAddress From Employees WHERE EmployeeName = '" & employeeName & "'", cn)
        Return TryCast(cmd.ExecuteScalar(), String)
    End Using
End Using

, DataTables , . , DataSet, ( ) .

0

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


All Articles