DOWNLOAD THE CODE:
Download the Code 22096.zip

Automate this vital process and simplify user support by using the SQLVDir object

SQL Server 2000 includes new features that let you use XML to query your database server either directly from a browser or through ADO code. If you've used these features, you know that you must create a Microsoft IIS virtual directory that lets you access the SQL Server data. Virtual directories let you map physical folders to directories that IIS exposes to the public. In SQL Server 2000, you must not only set up virtual directories but also set several properties associated with each directory, including security settings, a data source to use when providing XML data, and permissions that let users access that data. Creating the virtual directories manually—or automating them by using a Windows Script Host (WSH) script with Microsoft Active Directory Service Interfaces (ADSI)—isn't difficult. But walking through the manual process with more than one user will quickly sour you on the concept. Sure, you can instruct the user who is deploying your application to select Start, Programs, Microsoft SQL Server, then select the Microsoft Management Console (MMC) Configure SQL XML Support in IIS snap-in. This tool lets you create the new virtual directory manually. Then, you can walk your user through the necessary virtual directory settings (which let users access SQL Server data by using XML) on the IIS Virtual Directory Management for SQL Server multi-tabbed dialog box. Or, you can take a shortcut by using the SQLVDir component, which lets you automate the entire virtual directory setup process. If you like providing telephone support for hours on end, feel free to skip this information. Most of us, however, would rather write code to automate the creation of virtual directories.

The SQLVDir documentation is clear and concise, so I don't duplicate that information in this article. But to get you started, I explain SQLVDir's various objects and their members and show you how to use Visual Basic (VB) 6.0 to create a virtual directory programmatically.

The SQLVDir Object Model
To use SQLVDir's functionality in your applications, you must set a reference to the Microsoft SQL Virtual Directory Control 1.0 Type Library (sqlvdir.dll) in the References dialog box under VB's Tools menu. This type library contains just a few objects, which Figure 1 shows.

The SQLVDir object model is simple. You can create a new instance of the SQLVDirControl object, which lets you work with the other objects in the object model. Then, you can retrieve a reference to the SQLVDirs collection property. Each member of that collection is an ISQLVDir object that has several properties defining the behavior of a particular virtual directory. One ISQLVDir object property is the VirtualNames collection property. Each member of this collection defines the behavior of one virtual name associated with the virtual directory. (I discuss virtual names later.)

After you set the reference to the SQLVDir type library, you can use the objects that it provides. Using this type library, you can instantiate only one object—the SQLVDirControl object. This object, which lets you interact directly with IIS, provides the members that Table 1 shows. Neither of the object's properties returns anything useful until you've successfully connected to a running installation of IIS by using the Connect method. For example, you might write code like the following to connect to IIS running on your local computer:

Dim svdc AS SQLVDirLib.SQLVDirControl
SET svdc = New SQLVDirLib.SQLVDirControl
svdc.Connect

To connect to a specific server and a specific Web site on that server, modify the final line as follows:

svdc.Connect {<servername>}, {<sitenumber>}]

where servername is the name of the server and sitenumber specifies the Web site you want to connect to on the server. (Because a Web server might host more than one Web site, you can specify by number which site you want to connect to.) If you try to connect to an invalid IIS server name, the Connect method returns an error message that reports the failure to connect.

Working with Virtual Directories
After you connect to IIS, you can use the SQLVDirs property to retrieve a collection containing all the information about the virtual directories installed on the specified server, then start interacting with SQL Server 2000's virtual directories. By retrieving a reference to the SQLVDirs property of the SQLVDirControl object you've created, you can work with any existing virtual directory or create new ones.

The collection object that the SQLVDirs property returns provides the Item and Count members, with Item as the default member. In addition, the collection object provides the AddVirtualDirectory and RemoveVirtualDirectory methods, letting you create and delete virtual directories on your server. Table 2 shows members of the collection object that the SQLVDirs property returns. In SQL Server 2000 Books Online (BOL), you'll find that the SQLVDirs object also provides several other methods: Clone, Next, Reset, and Skip. These methods don't appear in VB's object browser because they're hidden in the type library. And VB developers don't need these methods—you can simply iterate through the collection, just as you would with any collection, by using a For...Each...Next loop. But scripting languages that don't provide this type of loop (e.g., JScript) can move through the collection by using these hidden SQLVDir methods. Listing 1 shows a code example that demonstrates the SQLVDirControl object and its SQLVDirs property.

   Prev. page   [1] 2     next page



You must log on before posting a comment.

If you don't have a username & password, please register now.