• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-01 11:09 | 作者: admin | 来源: | 查看: 16次 | 进入软件测试论坛讨论

领测软件测试网 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:

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网