|
數據源。這樣大大減少了連接過多對于數據庫服務器資源的占用。下面是我在《ADO.NET實用指南》這本書上看到的一個例子,比較清楚的講解
了ADO.NET的使用方法。
Imports System.Data.SqlClient
Imports System.Data
Imports System.Data.Common
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_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() '在形成SqlDataAdapter前打開conn
Dim daAuthors As New SqlDataAdapter("Select * From SC", conn)
Dim bldr As New SqlCommandBuilder(daAuthors)
daAuthors.Fill(ds,"SC")
conn.Close() '在填充完ds后關閉連接,接著對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) '添加一行
tbl.Rows(0).Delete() '刪除一行
tbl.Rows(1).BeginEdit()
tbl.Rows(1)("score") = 89 '修改一行
tbl.Rows(1).EndEdit()
conn.Open()
daAuthors.Update(ds.Tables("SC")) '須將結果傳回數據源時打開連接,update
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
AspNet技術:一個經典的ADO.NET入門例子,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。