ado.net数据操作全接触一(insert,update,delete)

来源:互联网 发布:淘宝爱逛街报名入口 编辑:程序博客网 时间:2024/04/30 01:17
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
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:
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>