ADO.NET Data Operation Full Contact 1 (Insert, Update, Delete)

xiaoxiao2021-03-06  49

1.1 Creating a database connection (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 Creating a Database Connection (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 Add a record (SQL Server)

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 Add Record (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/graphhors/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 Update Data (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 Update Data 1: <% @ Import Namespace = "System.data"%> 2: <% @ Import name "%>" 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.close13:%> 14: Record Updated! 15: 3.3 Update data affected record 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 / chapter / sams / graphics / ccc.gifrecordsaffected.toString () & "Records!"

14: MyConnection.Close15:%> 16: 4.1 Delete data (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; data = pubs") 9: myConnection.Open () 10: MyCommand = New SQLCOMMAND ("Delete TestTable WHERE COL1 = 'FRED'", MyConnection 11: MyCommand.executenonQuery () 12: MyConnection.Close () 13:%> 14: Record Deled! 15: 4.2 Delete Data (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 Deleting records in the record number 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 ("

转载请注明原文地址:https://www.9cbs.com/read-51915.html

New Post(0)