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

原创 2005年03月02日 10:49:00
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.Close15: %>16:

相关文章推荐

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
  • 下载

Insert, Update, Delete in ASP.NET Gridview, DataSource as SQL Server, MS Access (mdb/accdb), XML and

This demo is a continuation of my previous example “Binding GridView with Data”. If you are not fami...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ado.net数据操作全接触一(insert,update,delete)
举报原因:
原因补充:

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