5.1 Using SqlDataReader for database query
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 MyDataReader As SqldataReader
8:
9: myconnection = new sqlconnection ("Server = localhost; uid = sa; database = pubs")
10: myconnection.open ()
11: MyCommand = New Sqlcommand ("Select * from authors", myconnection)
12: myDataReader = mycommand.executeReader ()
13: While myDataReader.read ()
14: Response.write (MyDataReader.Item ("au_lname"))
15: End while
16: myDataReader.close ()
17: MyConnection.Close ()
18:%>
19:
5.2 Using SqlDataReader in C # for database query
1: <% @ page language = "c #"%> 2: <% @ Import namespace = "system.data"%> 3: <% @ import namespace = "system.data.sqlclient"%> 4: 5: < % 6: SqlDataReader myDataReader; 7: SqlConnection myConnection = new http://aspfree.com/chapters/sams/graphics/ccc.gifSqlConnection ( "server = localhost; uid = sa; database = Pubs"); 8: myConnection.Open (); 9: SqlCommand myCommand = new SqlCommand ( "Select * from http://aspfree.com/chapters/sams/graphics/ccc.gifAuthors", myConnection); 10: myDataReader = myCommand.ExecuteReader (); 11: while (MyDataReader.read ()) 12: {13: response.write (MyDataReader ["AU_LNAME"] .tostring ()); 14:} 15: MyDataReader.Close (); 16: myconnection.close (); 17:% > 18: 5.3 Using OLEDBDataReader for database query 1: <% @ Import namespace = "system.data"%> 2: <% @ Import namespace = "system.data.oledb"%> 3: 4: <% 5: DIM myConnection As OleDbConnection 6: Dim myCommand As OleDbCommand 7: Dim myDataReader As OleDbDataReader 8: 9: myConnection = New OleDbConnection ( "PROVIDER = Microsoft.Jet.OLEDB.4.0; DATA http://aspfree.com/chap ters / sams / graphics / ccc.gifSource = c: /authors.mdb ") 10: myConnection.Open () 11: myCommand = New OleDbCommand (" Select * from Authors ", myConnection) 12: myDataReader = myCommand.ExecuteReader () 13: While MyDataReader.Read14: Response.Write ("Author")) 15: End while16: mydatareader.close () 17: myconnection.close18:%> 19: 5.5 Using SQL Parameters1: <% @ import namespace = "System.data"%> 2: <% @ Import namespace = "system.data.sqlclient"%> 3: 4: <
% 5: Dim myConnection As SqlConnection 6: Dim myCommand As SqlCommand 7: Dim FirstName As String = "Robert" 8: Dim LastName As String = "Johnson" 9:10: myConnection = New SqlConnection ( "server = localhost; uid = sa PWD = Secret; Database = mydata ") 11: myConnection.Open () 12: mycommand = new sqlcommand (" INSERT Authors (firstname, lastname) http://aspfree.com/chapters/sams/graphics/ccc.gifValues @Firstname, @lastname) "" MyConnection) 13:14: mycommand.parameters.add (New Sqlparameter ("@firstname", http://aspfree.com/chaptersqldbtype.varchar, 30) ) 15: myCommand.Parameters ( "@FirstName") .Value = FirstName16: 17: myCommand.Parameters.Add (New SqlParameter ( "@LastName", http://aspfree.com/chapters/sams/graphics/ccc.gifSqlDbType .Varchar, 30)) 18: Mycommand.Parameters ("@lastname") .value = lastname19: 20: mycommand.executenonQuery () 21: myconnection.close () 22:%> 23: Record Inserted! 5.6 Using SQL Parameters Access) 1: <% @ Import namespace = "system.data"%> 2: <% @ import namespace = "SY Stem.Data.OLEDB "%> 3: 4: <% 5: DIM MyConnection As OledbConnection 6: Dim MyCommand AS OLEDBCOMMAND 7: DIM FIRSTNAME AS STRING =" Robert "8: Dim lastname as string =" johnson "9:10: MyConnection = New OLEDBCONNECTION ("provider = microsoft.jet.OleDb.4.0; http://aspfree.com/chapters/sams/graphics/ccc.gifdata source = c: /author2.mdb") 11: myconnection.Open () 12: mycommand = new oledbcommand ("INSERT INTO AUTHORS (FirstName) http://aspfree.com/chapters/sams/graphics/ccc.gifvalues (@firstname, @