2
2obvious
Imagine I have a database with four columns:
"ID" (the key)
"src"
"alt" and
"citation"
I've got images stored on a server, and I use the below ASP example to
pull up different images. (In the example, it only pulls up an image
with an ID of 2.)
<%@ Language = VBscript%>
<% Option Explicit %>
<%
Dim ID
ID = 2
%>
<%
Dim conn, strConnect
'Set connection string variable
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Server.MapPath("pix.mdb")
'Set Connection Properties and Execute Connection
Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = strConnect
conn.Open
Dim strSQL, rs
strSQL = "SELECT * " _
& "FROM Images " _
& "WHERE imgID=" & ID
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strSQL, conn
rs.Close
conn.Close
rs = Nothing
conn = Nothing
%>
<HTML><BODY>
<img src="<%=rs("src")%>.jpg" alt="<%=rs("alt")%>">
<p>
citation: <%=rs("citation")%>
</p>
</BODY></HTML>
I'm trying to make the leap to .NET, but I'm having difficulty finding
an example like this to study. All ADO.NET I've seen today deal with
DataSet spreadsheets and Web Controls that would otherwise be handled
in ASP with loops.
Here's what I've managed to cobble thus far:
<%@ page language="vb" runat="server" debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
Sub Page_Load()
Dim ID As Integer = 2
Dim conn As OleDbConnection
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" _
& "data source=" & server.mappath("pix.mdb"))
conn.Open
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim strSQL As String
strSQL = "SELECT * " _
& "FROM Images " _
& "WHERE imgID=" & ID
cmd = New OleDbCommand(strSQL,conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
image1.Src = rs["src"] & ".jpg"
image1.Alt = rs["alt"]
label1.Text = rs["citation"]
dr.Close
End Sub
</script>
<html><body>
<img id="image1" runat="server" />
<p>
citation: <asp:Label id="label1" runat="server" />
</p>
</body></html>
Clearly, my understanding of .NET starts to fall apart when it comes
to extracting data indexed by column names /and/ the properties (or
objects, even) that I need to run at the server to get what I want.
Please take a look. It ain't that tough, and I'm a quick learner.
"ID" (the key)
"src"
"alt" and
"citation"
I've got images stored on a server, and I use the below ASP example to
pull up different images. (In the example, it only pulls up an image
with an ID of 2.)
<%@ Language = VBscript%>
<% Option Explicit %>
<%
Dim ID
ID = 2
%>
<%
Dim conn, strConnect
'Set connection string variable
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Server.MapPath("pix.mdb")
'Set Connection Properties and Execute Connection
Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = strConnect
conn.Open
Dim strSQL, rs
strSQL = "SELECT * " _
& "FROM Images " _
& "WHERE imgID=" & ID
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strSQL, conn
rs.Close
conn.Close
rs = Nothing
conn = Nothing
%>
<HTML><BODY>
<img src="<%=rs("src")%>.jpg" alt="<%=rs("alt")%>">
<p>
citation: <%=rs("citation")%>
</p>
</BODY></HTML>
I'm trying to make the leap to .NET, but I'm having difficulty finding
an example like this to study. All ADO.NET I've seen today deal with
DataSet spreadsheets and Web Controls that would otherwise be handled
in ASP with loops.
Here's what I've managed to cobble thus far:
<%@ page language="vb" runat="server" debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
Sub Page_Load()
Dim ID As Integer = 2
Dim conn As OleDbConnection
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" _
& "data source=" & server.mappath("pix.mdb"))
conn.Open
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim strSQL As String
strSQL = "SELECT * " _
& "FROM Images " _
& "WHERE imgID=" & ID
cmd = New OleDbCommand(strSQL,conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
image1.Src = rs["src"] & ".jpg"
image1.Alt = rs["alt"]
label1.Text = rs["citation"]
dr.Close
End Sub
</script>
<html><body>
<img id="image1" runat="server" />
<p>
citation: <asp:Label id="label1" runat="server" />
</p>
</body></html>
Clearly, my understanding of .NET starts to fall apart when it comes
to extracting data indexed by column names /and/ the properties (or
objects, even) that I need to run at the server to get what I want.
Please take a look. It ain't that tough, and I'm a quick learner.