dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: IIS4,IIS5,SS7,SS2K
Expertise: Intermediate
Feb 8, 2001

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Create a GUID from ASP using SQL Server

At times you may need to create a GUID from ASP, for example when assigning unique IDs to users that are visiting your site for the first time. While you can generate new GUIDs from VB quite easily with a call to the CoCreateGUID API call (as explained elsewhere in the Tip Bank, see below), you can't do the same from ASP because you can't call any Windows API from VBScript.

If you have a SQL Server installed on your network, however, you can easily create a new GUID by calling the T-SQL function named NEWID(). Here's a VBScript routine that creates a new GUID and returns it to the caller:


Function CreateGUID()
    Dim cn
    Dim rs 

    Set cn = Server.CreateObject("ADODB.Connection")
    ' adjust the connection string if required
    cn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Data " _
        & "Source=(local)"
    Set rs = cn.Execute("SELECT NEWID()")
    If Not rs.EOF Then CreateGUID = rs(0)

    ' clean up 
    rs.Close
    cn.Close 
End Function 
Note that the connection string doesn't need to point to a specific database, and that the query string passed as an argument to the Connection's Execute method doesn't have to refer to a specific table. Of course, if you already have a connection open, you can skip the statements that create and open the Connection object.
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date