Username: Password:

ado.net数据操作全接触一(insert,update,delete)-.net教程,数据库应
来源:作者: 发布时间:2007-12-26 02:13:08

1.1创建数据库连接(sqlserver)
1: <%@ import namespace="system.data" %>
2: <%@ import namespace="system.data.sqlclient" %>
3: <%
4: dim myconnection as sqlconnection
5: myconnection = new sqlconnection( "server=localhost;database=pubs;uid=sa" )
6:
7: myconnection.open()
8: %>
9: connection opened!
1.2创建数据库连接(access)
1: <%@ import namespace="system.data" %>
2: <%@ import namespace="system.data.oledb" %>
3: <%
4: dim myconnection as oledbconnection
5: myconnection = new oledbconnection( "provider=microsoft.jet.oledb.4.0;data
http://aspfree.com/chapters/sams/graphics/ccc.gifsource=c:\authors.mdb" )
6:
7: myconnection.open()
8: %>
9: connection opened!
2.1添加纪录(sqlserver)
 1: <%@ import namespace="system.data" %> 
2: <%@ import namespace="system.data.sqlclient" %> 
3: 
4: <% 
5: dim myconnection as sqlconnection 
6: dim mycommand as sqlcommand 
7: 
8: myconnection = new sqlconnection( "server=localhost;uid=sa;database=pubs" ) 
9: myconnection.open()
10: mycommand = new sqlcommand( "insert testtable ( col1 ) http://aspfree.com/chapters/sams/graphics/ccc.gifvalues ( hello )", myconnection )
11: mycommand.executenonquery()
12: myconnection.close()
13: %>
14: new record inserted!
15:
2.2添加纪录(access)
1: <%@ import namespace="system.data" %> 
2: <%@ import namespace="system.data.oledb" %> 
3: 
4: <% 
5: dim myconnection as oledbconnection 
6: dim mycommand as oledbcommand 
7: 
8: myconnection = new oledbconnection( "provider=microsoft.jet.oledb.4.0;data http://aspfree.com/chapters/sams/graphics/ccc.gifsource=c:authors.mdb" ) 
9: myconnection.open()
10: mycommand = new oledbcommand( "insert into authors ( author ) values http://aspfree.com/chapters/sams/graphics/ccc.gif( simpson )", myconnection )
11: mycommand.executenonquery()
12: myconnection.close()
13: %>
14: new record inserted!
15:
3.1更新数据(sqlserver)
1: <%@ import namespace="system.data" %> 
2: <%@ import namespace="system.data.sqlclient" %> 
3: 
4: <% 
5: dim myconnection as sqlconnection 
6: dim mycommand as sqlcommand 
7: 
8: myconnection = new sqlconnection( "server=localhost;uid=sa;database=pubs" ) 
9: myconnection.open()
10: mycommand = new sqlcommand( "update authors set lastname=smith http://aspfree.com/chapters/sams/graphics/ccc.gifwhere lastname=bennett", myconnection )
11: mycommand.executenonquery()
12: myconnection.close()
13: %>
14: record updated!
15:
3.2更新数据(access)
1: <%@ import namespace="system.data" %>
 2: <%@ import namespace="system.data.oledb" %> 
3: 
4: <% 
5: dim myconnection as oledbconnection 
6: dim mycommand as oledbcommand 
7: 
8: myconnection = new oledbconnection( "provider=microsoft.jet.oledb.4.0;data http://aspfree.com/chapters/sams/graphics/ccc.gifsource=c:\authors.mdb" ) 
9: myconnection.open()
10: mycommand = new oledbcommand( "update authors set author=bennett http://aspfree.com/chapters/sams/graphics/ccc.gifwhere author = simpson", myconnection )
11: mycommand.executenonquery()
12: myconnection.close
13: %>
14: record updated!15:
3.3更新数据中受影响的记录数
1: <%@ import namespace="system.data" %> 
2: <%@ import namespace="system.data.sqlclient" %> 
3: 
4: <% 
5: dim myconnection as sqlconnection 
6: dim mycommand as sqlcommand 
7: dim recordsaffected as integer
8:
9: myconnection = new sqlconnection( "server=localhost;uid=sa;database=pubs" )
10: myconnection.open()
11: mycommand = new sqlcommand( "update testtable set col1=hello http://aspfree.com/chapters/sams/graphics/ccc.gifwhere col1=fred", myconnection )
12: recordsaffected = mycommand.executenonquery()
13: response.write( "the update statement modified " & http://aspfree.com/chapters/sams/graphics/ccc.gifrecordsaffected.tostring() & " records!" )
14: myconnection.close
15: %>
16:
4.1删除数据(sqlserver)
1: <%@ import namespace="system.data" %>
2: <%@ import namespace="system.data.sqlclient" %>
3:
4: <%
5: dim myconnection as sqlconnection
6: dim mycommand as sqlcommand
7:
8: myconnection = new sqlconnection( "server=localhost;uid=sa;database=pubs" )
9: myconnection.open()
10: mycommand = new sqlcommand( "delete testtable where col1=fred", myconnection )
11: mycommand.executenonquery()
12: myconnection.close()
13: %>
14: record deleted!
15:
4.2删除数据(access)
 1: <%@ import namespace="system.data" %> 
2: <%@ import namespace="system.data.oledb" %>
3:
4: <%
5: dim myconnection as oledbconnection
6: dim mycommand as oledbcommand
7:
8: myconnection = new oledbconnection( "provider=microsoft.jet.oledb.4.0;data http://aspfree.com/chapters/sams/graphics/ccc.gifsource=c:\authors.mdb" )
9: myconnection.open()
10: mycommand = new oledbcommand( "delete from authors http://aspfree.com/chapters/sams/graphics/ccc.gifwhere author = simpson", myconnection )
11: mycommand.executenonquery()
12: myconnection.close()
13: %>
14: record deleted!
15:
16:
4.3删除纪录中受影响的记录数
1: <%@ import namespace="system.data" %> 
2: <%@ import namespace="system.data.sqlclient" %>
3: 
4: <% 
5: dim myconnection as sqlconnection 
6: dim mycommand as sqlcommand 
7: dim recordsaffected as integer 
8: 
9: myconnection = new sqlconnection( "server=localhost;uid=sa;database=pubs" )
10: myconnection.open()
11: mycommand = new sqlcommand( "delete authors2 http://aspfree.com/chapters/sams/graphics/ccc.gifwhere lastname=smith", myconnection )
12: recordsaffected = mycommand.executenonquery()
13: response.write( "the delete statement modified " http://aspfree.com/chapters/sams/graphics/ccc.gif& recordsaffected.tostring() & " records!" )
14: myconnection.close
15: %>
16:

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅