Use SQL Server to provide a Web browser with Image 3 (end)

Source: Internet
Author: User
Tags dsn microsoft sql server connect session id sql server example
Set up a Web project and set up a database connection

After registering the Imageloader dynamic link library, we can build the Web project. We use Microsoft Visual InterDev 1.0 to build an ASP page to
and a data connection to the Microsoft SQL Server database.

First, open Microsoft Visual InterDev. In the File menu, select the New menu item. In the dialog box that appears, select the Web project to
Guide, enter imageload in the Project Name field. Click OK to complete the next two steps to end the wizard.

As you can see, Visual InterDev automatically generates a file in the workspace (Workspace) window, called Global.asa. The first time the user
When you view a page in a Web application, the file is automatically generated. To connect to a database in SQL Server, select an item | Add Item (Project |
Add to Project), and then select the data Connection (Connection). Here, select the Data Source dialog box to appear, we must define a data source name
Called (DSN).

Click the New button, select SQL Server in the list of available data drivers listed, and then click Next. Next, in the input box
Enter Grocevb as DSN. The next few steps are easy to complete and no longer detailed.

Below, the SQL Server logon dialog box appears. Enter the server name, login ID, and password, and click the option button. From the available data listed
Library list, select Grocevb and click OK. After you have completed all of the above steps, you are returned to the Select Data Source dialog box. Here, from DSN
List click the file name Grocevb.dsn and complete the next few steps to end the operation.

Now, you can double-click Global.asa to see the code generated in this file in the above steps. The code defines some of the changes necessary to connect to the SQL Server database
Amount

In order to initialize the class Clsimageloader, two lines of code must be placed at the beginning of the global.asp. We have listed the code in Global.asa in List C. Note
Meaning Clsimageloader is a session-wide object.

List C:global.asa

<object runat=server scope=session id=objimageloader progid= "Imageloader.clsimageloader" >
</OBJECT>
<script Language=vbscript runat=server>
Sub Session_OnStart
Session ("grocevb_connectionstring") = "Driver=sql Server;" server= (local);
Uid=sa; pwd=; App=microsoft (R) Developer Studio; Wsid=jia;
Database=grocevb; Useprocforprepare=0 "
Session ("grocevb_connectiontimeout") = 15
Session ("grocevb_commandtimeout") = 30
Session ("grocevb_runtimeusername") = "sa"
Session ("Grocevb_runtimepassword") = ""
End Sub

Sub Session_OnEnd
Set Objimageloader = Nothing
End Sub

</SCRIPT>




Building an ASP page

In order to connect to the database and communicate with Imageloader this dynamic link library, we built an ASP file in the Web project. method is in the file
menu, select the New menu item, select the ASP page icon in the File tab, and enter the file name in the input box. We put this page
Named Default.asp. The code contained in Default.asp is shown in Listing D.

List d:default.asp

<% Set dbconn = Server.CreateObject ("ADODB. Connection ")
Dbconn.connectiontimeout = Session ("Grocevb_connectiontimeout")
Dbconn.commandtimeout = Session ("Grocevb_commandtimeout")
Dbconn.open session ("Grocevb_connectionstring"), Session ("Grocevb_runtimeusername"), _
Session ("Grocevb_runtimepassword")

Set cmdtemp = Server.CreateObject ("Adodb.command")
Set rstproduct = Server.CreateObject ("ADODB. Recordset ")
Cmdtemp.commandtext = "Select prod_id, Prod_name, Prod_desc, Prod_unitprice, & _
"Prod_unitquant, Prod_imagetype" & _
"From the products order by Prod_name"
Cmdtemp.commandtype = 1
Set cmdtemp.activeconnection = Dbconn
Rstproduct.open cmdtemp, 0, 1

Objimageloader.dbname = "Grocevb"
Objimageloader.tablename = "Products"
Objimageloader.nameofimagecolumn = "Prod_image"
Objimageloader.nameofimagetypecolumn = "Prod_imagetype"
Objimageloader.nameofimageidcolumn = "prod_id"
Objimageloader.openconnection
%>

<HTML>
<HEAD>
<TITLE> loading a picture from SQL Server Example </TITLE>
</HEAD>
<body bgcolor= "#FFFFFF" Topmargin=8 leftmargin=4>
<%do While not rstproduct.eof%>
<%prodid = Rstproduct ("prod_id")%>
<% objimageloader.createtempimagefile (ProdId)%>
<% FileName = "images/" & Objimageloader.imagefile (ProdId)%>

<table border=1 cellpadding=0 cellspacing=0 width=100%>
<table border=0 cellpadding=5 cellspacing=0 width=100% cols=2>
&LT;TD width=30%>
>
<p> </p>
<font size=2>image Type: <% = rstproduct ("Prod_imagetype")%>
</td>
&LT;TD width=70%>
<p align=center><font size=4><% =rstproduct ("Prod_name")%></font></p>
<p align=center><% =rstproduct ("Prod_desc")%></p>
</td>
</TABLE>
<p> </p>
<table border=0 cellpadding=5, cellspacing=0 width=100% cols=2>
&LT;TD width=50%>
<p align=left>unit Quantity: <% = Rstproduct ("prod_unitquant")%></p>
</td>
&LT;TD width=50%>
<p align=left>unit Price: <% =rstproduct ("Prod_unitprice")%></p>
</td>
</TABLE>
</TABLE>
<p> </p>
<% Rstproduct.movenext%>
<% Loop%>
</BODY>
</HTML>


Note: The Createtempimagefile method defined in the class Imageloader and the ImageFile property (read-only) require a picture ID as an argument. In this example
, the picture ID is the primary key for the table products (prod_id). Therefore, the value of the prod_id must be passed to the dynamic link library.


Summarize

In this article, we demonstrate a method that uses the ActiveX dynamic link library and ASP to mount pictures from Microsoft SQL Server servers to web browsing
Manager It has been shown that this approach has an advantage in the flexibility of rapid development, ease of use, and customization of Web pages. The dynamic link library is used to connect to the database, mentioning
Take picture data, put the picture data into the file. The ASP page is responsible for communicating with the SQL database and the dynamic link library. We've covered setting up SQL databases and web
The operation process of the project.

Although the dynamic link library and database functionality in this article has been simplified, the steps described here are valid for any other powerful and complex Web project
Of



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.