当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 一个经典的ADO.NET入门例子

ASP.NET
Asp.Net 通用数据操作类 (附通用数据基类)
asp.net汉字转拼音和获取汉字首字母的代码
asp.net 多字段模糊查询代码
OpenCms 带分页的新闻列表
URLRewriter最简单入门介绍 URLRewriter相关资源
asp.net Repeater取得CheckBox选中的某行某个值
asp.net清空Cookie的两种方法
asp.net一些很酷很实用的.Net技巧
asp.net生成高质量缩略图通用函数(c#代码),支持多种生成方式
asp.net TripleDES加密、解密算法
Asp.net中防止用户多次登录的方法
asp.net Repeater取得CheckBox选中的某行某个值的c#写法
asp.net DataGridView导出到Excel的三个方法[亲测]
C#,winform,ShowDialog,子窗体向父窗体传值
asp.net学习中发现的比较完整的流程
ASP.net 页面被关闭后,服务器端是否仍然执行中?
asp.net Context.Handler 页面间传值方法
asp.net xml序列化与反序列化
asp.net实例代码protected override void Render(HtmlTextWriter writer)
asp.net(c#)捕捉搜索引擎蜘蛛和机器人

ASP.NET 中的 一个经典的ADO.NET入门例子


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 137 ::
收藏到网摘: n/a

众所周知,ADO.NET相对于ADO的最大优势在于对于数据的更新修改可以在与数据源完全断开联系的情况下进行,然后再把数据更新情况传回到
数据源。这样大大减少了连接过多对于数据库服务器资源的占用。下面是我在《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