6.1 Using the stored procedure
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 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 ("InsertAuthors", MyConnection)
13: mycommand.commandtype = commandtype.storedprocedure
14:
15: MyCommand.Parameters.Add (New Sqlparameter ("@firstname",
http://aspfree.com/chapters/sams/graphics/ccc.gifsqldbtype.varchar, 30)))
16: MyCommand.Parameters ("@firstname") .value = firstname
17:
18: mycommand.parameters.add (New Sqlparameter ("@lastname",
http://aspfree.com/chapters/sams/graphics/ccc.gifsqldbtype.varchar, 30)))
19: mycommand.parameters ("@lastname") .value = lastname
20:
21: MyCommand.executenonQuery ()
22: MyConnection.Close
23:%>
24: Record Inserted!
25:
26:
6.2 Re-return parameters and return values
1: <% @ Import namespace = "system.data"%>
2: <% @ Import namespace = "system.data.sqlclient"%>
3: Listing 6.4.1 Demonstrates
4: <%
5: DIM MyConnection As SqlConnection
6: DIM MyCommand as Sqlcommand
7: Dim myparam as sqlparameter
8:
9: myconnection = new sqlconnection ("Server = localhost; uid = sa; database = pubs")
10: myconnection.open ()
11:
12: mycommand = new sqlcommand ("getlastname", myconnection)
13: mycommand.commandtype = commandtype.storedProcedure14:
15: myparam = mycommand.parameters.add (New
http://aspfree.com/chapters/sams/graphics/ccc.gifsqlparameter ("Return Value", SqldbType.In))))
16: myparam.direction = parameterDirection.ReturnValue
17:
18: myparam = mycommand.parameters.add (New
http://aspfree.com/chapters/sams/graphics/ccc.gifsqlparameter ("@firstname", sqldbtype.varchar, 50))
19: myparam.direction = parameterdirection.input
20: myparam.value = "robert"
twenty one:
22: myparam = mycommand.parameters.add (New
http://aspfree.com/chapters/sams/graphics/ccc.gifsqlparameter ("@lastname", SqldbType.varchar, 50))
23: myparam.direction = parameterdirection.output
twenty four:
25: MyCommand.executenonQuery ()
26: if Mycommand.Parameters ("Return Value") .value THEN
27: Response.write ("The Last Name IS" &
Mycommand.Parameters ("@lastname") .value)
28: Else
29: Response.write ("No Author Found!")
30: END IF
31: MyConnection.Close ()
32:%>
33:
7.1 Using DataTable (SQLServer)
1: <% @ Import namespace = "system.data"%>
2: <% @ Import namespace = "system.data.sqlclient"%>
3:
4: <%
5: DIM MyConnection As SqlConnection
6: DIM MyDataAdapter as SqldataAdapter
7: DIM MyDataSet As Dataset
8: DIM MyDataTable as DataTable
9: DIM Myrow As DataRow
10:
11: MyConnection = New SqlConnection ("Server = localhost; uid = sa; database = pubs")
12: MyDataAdapter = New SqldataAdapter ("Select * from authors", myconnection)
13: MyDataSet = New Dataset ()
14: MyDataAdapter.Fill (MyDataSet, "Authors")
15:
16: for Each Myrow In MyDataSet.Tables ("Authors") .Rows17: Response.write (MyRow ("au_lname") DataTable
18: NEXT19: 20:%> 21: 7.2 Using DATATABLE (Access) 1: <% @ Import Namespace = "System.data"%> 2: <% @ Import namespace = "system.data.oledb"%> 3: 4: <% 5: Dim myConnection As OleDbConnection6: Dim myDataAdapter As OleDbDataAdapter7: Dim myDataSet As DataSet8: Dim myDataTable As DataTable9: Dim myRow As DataRow10: 11: myConnection = New OleDbConnection ( "PROVIDER = Microsoft.Jet.OLEDB.4.0; DATAhttp : //ASPFree.com/chapters/sams/graphics/ccc.gifsource=c: /authors.mdb ") 12: mydataadapter = new oledbdataadapter (" Select * from authors ", myconnection) 13: mydataset = new dataset () 14 : MyDataAdapter.Fill (MyDataSet, "Authors") 15:16: for Each Myrow in MyDataSet.Tables ("Authors") .Rows17: Response.write (MyRow ("Author") 18: Next19: 20:%> 21 : 7.3 Automatic display of a table 1: <% @ Import namespace = "system.data"%> 2: <% @ Import namespace = "system.data.sqlclient"%> 3: 4: <% 5: DIM MyConnection As SqlConnection6 : Dim MyDataAdapter7: Dim MyDataSet As DataSet8: Dim MyDataTable As DataTable9: 10: Dim Rowcount AS INTEGER11: DIM CO lCount As Integer12: Dim i, k As Integer13: 14: myConnection = New SqlConnection ( "server = localhost; uid = sa; database = Pubs") 15: myDataAdapter = New SQLDataAdapter ( "Select * From Authors", myConnection) 16: MyDataSet = new dataset () 17: MyDataAdapter.Fill (MyDataSet, "Authors") 18:19: RowCount = mydataset.tables ("authors") .Rows.count 20: colcount = myDataSet.tables ("authors") .columns. Count21: 22: Response.write ("