A classic example of getting started with ADO. NET

Source: Internet
Author: User

As we all know, the biggest advantage of ADO. NET over ADO is that the data update and modification can be performed when it is completely disconnected from the data source, and then the data update is passed back.

Data source. This greatly reduces the occupation of Database Server resources due to excessive connections. The following is an example in my book ADO. NET practical guide.

The usage of ADO. NET.
Imports System. Data. SqlClient
Imports System. Data
Imports System. Data. Common

Public Class Form1
Inherits System. Windows. Forms. Form

Private Sub button#click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Button1.Click
Dim conn As New SqlConnection ("data source = localhost; initial catalog = StudentCourse ;"&_
"User ID =; Password = ;")
Dim ds As New DataSet
Try
Conn. Open () 'Open conn before forming the SqlDataAdapter
Dim daAuthors As New SqlDataAdapter ("Select * From SC", conn)
Dim bldr As New SqlCommandBuilder (daAuthors)
DaAuthors. Fill (ds, "SC ")
Conn. Close () 'closes the connection After ds is filled, and then performs operations on ds.

Dim tbl As New DataTable
Tbl = ds. Tables ("SC ")

Dim rowVals (3) As Object
RowVals (0) = "5"
RowVals (1) = "00003"
RowVals (2) = "0001"
RowVals (3) = 99
Dim insertedRow As DataRow
InsertedRow = tbl. Rows. Add (rowVals) 'Add a row

Tbl. Rows (0). Delete () 'Delete a row

Tbl. Rows (1). BeginEdit ()
Tbl. Rows (1) ("score") = 89 'modify a row
Tbl. Rows (1). EndEdit ()

Conn. Open ()
DaAuthors. Update (ds. Tables ("SC") 'must enable the connection when the result is returned to the data source. update
Conn. Close ()
Catch ex As Exception
MessageBox. Show (ex. Message)
End Try
End Sub
End Class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.