ado.net数据操作全接触四(表关联,DataAdapter)

原创 2005年03月02日 10:52:00
8.1创建一个(主/祥)表关联
1: <%@ Import Namespace="System.Data" %>
2: <%@ Import NameSpace="System.Data.SqlClient" %>
3:
4: <%
5: Dim myConnection As SqlConnection
6: Dim myDataAdapter As SqlDataAdapter
7: Dim myDataSet As DataSet
8: Dim myDataTable As DataTable
9: Dim Publisher As DataRow
10: Dim Title As DataRow
11:
12: myConnection = New SqlConnection( "server=localhost;uid=sa;pwd=secret;database=Pubs" )
13: myDataSet = New DataSet()
14: myDataAdapter = New SQLDataAdapter( "Select * From Publishers",
http://aspfree.com/chapters/sams/graphics/ccc.gifmyConnection )
15: myDataAdapter.Fill( myDataSet, "Publishers" )
16: myDataAdapter.SelectCommand = New SqlCommand( "Select * From Titles",
http://aspfree.com/chapters/sams/graphics/ccc.gifmyConnection )
17: myDataAdapter.Fill( myDataSet, "Titles" )
18:
19: myDataSet.Relations.Add( "PubTitles",
http://aspfree.com/chapters/sams/graphics/ccc.gifmyDataSet.Tables( "Publishers" ).Columns( "pub_id" ),
http://aspfree.com/chapters/sams/graphics/ccc.gifmyDataSet.Tables( "Titles" ).Columns( "pub_id" ) )
20:
21: For Each Publisher in myDataSet.Tables( "Publishers" ).Rows
22: Response.Write( "<p>" & Publisher( "pub_name" ) & ":" )
23: For Each Title In Publisher.GetChildRows( "PubTitles" )
24: Response.Write("<li>" & Title( "title" ) )
25: Next
26: Next
27:
28: %>
29:
9.1使用DataAdapter UPDATE模式
1: <%@ Import Namespace="System.Data" %>
2: <%@ Import NameSpace="System.Data.SqlClient" %>
3:
4: <%
5: Dim myConnection As SqlConnection
6: Dim myDataAdapter As SqlDataAdapter
7: Dim myBuilder As SqlCommandBuilder
8: Dim myDataSet As DataSet
9: Dim myDataTable As DataTable
10: Dim Author As DataRow
11:
12: ' Create the DataSet and DataAdapter
13: myConnection = New SqlConnection( "server=localhost;uid=sa;pwd=secret;database=Pubs" )
14: myDataSet = New DataSet()
15: myDataAdapter = New SqlDataAdapter( "Select * From Author", myConnection )
16: myDataAdapter.Fill( myDataSet, "Authors" )
17:
18: ' Change value of first row
19: myDataSet.Tables( "Authors" ).Rows( 0 ).Item( "au_fname" ) = "Jane"
20:
21: ' Update the Database Table
22: myBuilder = New SqlCommandBuilder( myDataAdapter )
23: myDataAdapter.Update( myDataSet, "Authors" )
24:
25: ' Display the Records
26: For Each Author in myDataSet.Tables( "Authors" ).Rows
27: Response.Write( "<p>" & Author( "au_fname" ) & " "
http://aspfree.com/chapters/sams/graphics/ccc.gif& Author( "au_lname" ) )
28: Next
29: %>
30:
9.2使用DataAdapter UPDATE模式(C#)


1: <%@ Page Language="C#" %>
2: <%@ Import Namespace="System.Data" %>
3: <%@ Import NameSpace="System.Data.SqlClient" %>
4:
5: <%
6: // Create the DataSet and DataAdapter
7: SqlConnection myConnection = new
http://aspfree.com/chapters/sams/graphics/ccc.gifSqlConnection( "server=localhost;uid=sa;pwd=secret;database=Pubs" );
8: DataSet myDataSet = new DataSet();
9: SqlDataAdapter myDataAdapter = new SqlDataAdapter(
http://aspfree.com/chapters/sams/graphics/ccc.gif"Select * From Authors3", myConnection );
10: myDataAdapter.Fill( myDataSet, "Authors" );
11:
12: // Change value of first row
13: myDataSet.Tables[ "Authors" ].Rows[ 0 ][ "au_fname" ] = "Jane";
14:
15: // Update the Database Table
16: SqlCommandBuilder myBuilder = new SqlCommandBuilder( myDataAdapter );
17: myDataAdapter.Update( myDataSet, "Authors" );
18:
19: // Display the Records
20: foreach ( DataRow Author in myDataSet.Tables[ "Authors" ].Rows )
21: {
22: Response.Write( "<p>" + Author[ "au_fname" ] + " "
http://aspfree.com/chapters/sams/graphics/ccc.gif+ Author[ "au_lname" ] );
23: }
24: %>
25:

相关文章推荐

使用 DataAdapter 更新数据源 (ADO.NET)

调用 DataAdapter 的 Update 方法可以将 DataSet 中的更改解析回数据源。与 Fill 方法类似,Update 方法将 DataSet 的实例和可选的 DataTable 对象...

【ADO.net】 DataReader与DataAdapter辨析

当我们与数据源建立连接并定义了查询命令后,就要从数据源中检索数据了。不同的数据检索形式,自然适应不同的情况,其性能自然也是不同的,这篇文章主要来区分一下DataReader与DataAdapter两种...

ADO.NET数据操作常见错误总结

1-数据库服务器无法连接问题凡是出现以上问题时,使用如下方法。1检查SQLServer服务是否打开:2检查连接字符串中的Server对应的服务器名称是否正确(注意默认实例,命名实例的名称)3如果示连接...

ADO.NET基本数据操作(增删改查)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D...

ADO.NET中数据操作浅述

上次我们

C#ADO.NET数据操作汇总

C#ADO.NET操作数据代码汇总 一.用SqlConnection连接SQL Server 1..加入命名空间 using System.Data.SqlClient; 2.连接数据库 S...

Ado.Net操作Excel文件数据常见问题及解决

经常需要在数据库与Execl之间互导数据。net时代,ADO.NET可以使用使用Microsoft.Jet.OleDb访问访问Excel,网上已经有很多类似的资源,最典型也是最简单的可能如下:(asp...

ADO.NET数据操作

  • 2008年04月28日 01:46
  • 200KB
  • 下载

ADO.NET数据操作 PDF

  • 2007年06月12日 15:16
  • 160KB
  • 下载

ADO.net实现数据查询表的分页

首先要引入引用一个外部的aspnetPager.dll文件Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="asp" %>写代码对...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ado.net数据操作全接触四(表关联,DataAdapter)
举报原因:
原因补充:

(最多只允许输入30个字)