XML and SQL Server 2000

All the virtual root and virtual name configuration that we have done in this chapter by utilizing a GUI also can be carried out programmatically. Microsoft has provided an object model called the Virtual Directory Management Object Model to accomplish this. It consists of the following objects:

  • SQLVDirControl Object

  • SQLVDirs Collection Object

  • SQLVDir Object

  • VirtualNames Collection Object

  • VirtualName Object

The following discussion utilizes VBScript and Active Server Pages to demonstrate some advanced programmatic methods of duplicating what we created earlier with a GUI. Those of you that are not familiar with these technologies may want to skip the remainder of this chapter.

In an object model, the objects contained in an application provide the state, functionality, and characteristics of an application. A collection object, however, contains related objects (a collection). A collection object's Item method is usually employed to gain access to one of its objects.

The Web, which utilizes the HTTP protocol, has several collection objects, one of which is the Form Collection. How many of you have filled out a form on the Internet and then submitted your information by clicking on a button? What's actually going on is that the names of the form elements that you have put data in are passed to a Web server along with the data itself. The Form Collection contains these names and their associated data. After that collection arrives at the server, the data can be accessed and processed as the developer sees fit. Take a look at Listing 3.1, which shows the relationship of these different entities and how to iterate through all the individual objects contained in the Form Collection via Active Server Pages and VBScript (this code assumes that the user has clicked the Submit button).This example contains both a small portion of HTML code and server-side code.

Listing 3.1 Collection Object, Objects, and Item

<% for each Item in Request.Form %> Name = <% =Item %> Value = <% =Request.Form(Item) %> <BR> <% Next %>

In the Virtual Directory Management Object Model, SQLVDirControl is the top-level object or the collection, and the only one that can be directly created. All other objects must be obtained from the SQLVDirControl object, or a derivative of it.

A hierarchy diagram (object model) illustrates the relative arrangement of objects to one another. The diagram for creating a virtual root is given in Figure 3.9.

Figure 3.9. Virtual Directory Management Object Model hierarchy.

SQLVDirControl Object

This is the only object in the object hierarchy that can be directly accessed programmatically. All other objects must be accessed through it or derived from it. Table 3.7 gives the SQLVDirControl methods. The rest of this chapter is dedicated to providing examples to accomplish the manipulation of all the other objects.

Table 3.7. SQLVDirControl Methods

Method

Description

Connect

Connects to a specified IIS server.

Parameters: IIS server name, Web site number in the metabase tree.

Disconnect

Closes the last connection made to an IIS server and Web site. There are no parameters. Disconnect must be called after processing is finished and before attempting to connect to another server or Web site.

Note: Simultaneous connections to multiple servers are not possible. On the other hand, multiple calls to Connect without calling Disconnect are possible when you make more than one connection to different Web sites on the same server.

SQLVDirs

Retrieves the virtual directory collection of the Web site you are connected to. Thus, it provides access to the virtual directory objects.

Listing 3.2 is an example showing server and Web site connection methods and how to connect to the first Web site on a Web server. For a more comprehensive example of the usage of this object, and the other objects in the hierarchy, see the section "Using the Object Model" toward the end of this chapter.

Listing 3.2 Selecting the Server's First Web Site

Set objDirControl = Server.CreateObject("SQLVDir.SQLVDirControl") 'create the 'base object ObjDirControl.Connect "IISServer", "1" 'connect to 'the first web 'site on the 'server 'IISServer Set objDirs = objDirControl.SQLVDirs 'get the 'virtual 'directory 'collection 'object ... ObjDirControl.Disconnect

SQLVDirs Collection Object

As seen in the small example provided in the preceding section, the SQLVDirs , which is the collection object of virtual directories, is returned by the SQLVDirControl.SQLVDirs methods as given in Table 3.8. After you have the SQLVDirs collection you can:

  • Access a virtual directory with the Item method

  • Create a new virtual directory with the AddVirtualDirectory method

  • Remove an existing virtual directory with the RemoveVirtualDirectory method

Table 3.8. SQLVDirs Methods

Method

Description

Next

Retrieves the next virtual directory or directories. An integer specified for Next indicates how many directories to retrieve.

Skip

Skips the virtual directory. An integer specified for Skip indicates how many directories to skip.

Reset

Resets the collection index to the first virtual directory.

Clone

Returns a copy of the SQLVDirs collection object.

Count

Returns the number of virtual directories present.

Item

Retrieves one virtual directory, which can be specified with an integer (0 is the first virtual directory), or with a name.

AddVirtualDirectory

Needs the virtual directory name as a parameter. Creates a new virtual directory with all the default values. Some properties are not set (for example, Default database).

RemoveVirtualDirectory

Deletes the virtual directory from IIS.

Listing 3.3 shows an example of connecting to a Web site and accessing the first virtual directory object.

Listing 3.3 Accessing the First Virtual Directory Object, Item(0)

Set objDirControl = Server.CreateObject("SQLVDir.SQLVDirControl") 'create the 'base object ObjDirControl.Connect "IISServer", "1" 'connect to 'the first 'web site on 'the server 'IISServer Set objDirs = objDirControl.SQLVDirs 'get the 'virtual 'directory 'collection object Set objDir = objDirs.Item(0) 'get the first virtual 'directory object. You 'could say objDirs(0) 'since Item() is the 'default ... ObjDirControl.Disconnect

SQLVDir Object

The SQLVDir object is obtained by calling the Item method, the SQLVDirs object, and the AddVirtualDirectory method when creating a new virtual directory.

The SQLVDir object has the following properties, as given in Table 3.9, all of which are read/write (you can set or get them), except Password . Password is write only (it can only be set).

Table 3.9. SQLVDir Object Properties

Property

Description

Name

The name of the virtual directory.

PhysicalPath

The full physical path to the physical directory associated with the virtual directory.

ServerName

The name of the server running SQL Server 2000 (the data source).

DatabaseName

The default database used in queries against the virtual directory.

UserName

The user login that connects to the data source.

Password

The user password that connects to the data source.

SecurityMode

The login authentication method used with the virtual directory.The following values can be used:

 

Value

Description

 

1

SQL Server login

 

2

Microsoft Windows anonymous login

 

4

Basic authentication

 

8

Windows Integrated Authentication

AllowFlags

Provides the type of access allowed through this virtual directory.The following values can be used:

 

Value

Description

 

1

URL queries

 

8

Template access

 

4

XPath queries

 

Programmatically, to specify more than one type, simply add the values together. To allow both XPath queries and template access, the value specified would be 64 + 8, or 72. (This is a programmer preference, and I don't want to start a religious war here.) See the complete example at the end of the chapter.

Caution: If you are changing the connection settings (server name, database name, user name, password, or the security mode), disallow virtual directory access by setting the AllowFlags property to 0. After you are finished, be sure to re-enable access by setting the AllowFlags property back to its original value.

EnablePasswordSync

Specifies whether or not IIS handles anonymous password synchronization. I recommend that this be enabled.

DLLPath

Provides the full path to the Sqlisapi.dll file.

AdditionalSettings

User defined settings appended to the OLE DB connection string.

In addition to these properties, the SQLVDir object supports the VirtualNames method. This method returns the collection of virtual names for the virtual directory.

Listing 3.4 shows an example of connecting to a Web site and setting the PhysicalPath property of the first virtual directory object.

Listing 3.4 Setting the PhysicalPath Property

Set objDirControl = Server.CreateObject("SQLVDir.SQLVDirControl") 'create the 'base object ObjDirControl.Connect "IISServer", "1" 'connect to 'the first web 'site on the 'server 'IISServer Set objDirs = objDirControl.SQLVDirs 'get the 'virtual 'directory 'collection 'object Set objDir = objDirs.Item(0) 'get the first 'virtual 'directory 'object. You 'could say 'objDirs(0) 'since Item() 'is the 'default objDir.PhysicalPath = "C:\" 'these two lines 'just show two objDir.PhysicalPath = objDir.PhysicalPath & "inetpub" 'different ways 'of setting the 'value. It can 'be done either 'way ObjDirControl.Disconnect

VirtualNames Collection Object

The VirtualNames collection object is a collection of virtual names in the virtual directory object. It is similar to the SQLVDirs object (a collection of virtual roots).

The VirtualNames collection object contains the following methods given in Table 3.10.

Table 3.10. VirtualNames Methods

Method

Description

Next

Retrieves the next virtual name or names. An integer specified for Next indicates how many virtual names to retrieve.

Skip

Skips the virtual name or names. An integer specified for Skip indicates how many names to skip.

Reset

Resets the collection index to the first virtual name.

Clone

Returns a copy of the VirtualNames collection object.

Count

Returns the number of virtual names present.

Item

Retrieves one virtual name that can be specified with an integer (0 is the first virtual name), or with the name of the virtual name.

AddVirtualName

Requires the name, type, and directory path of the virtual name to create as parameters. The AddVirtualName method or Item method returns an interface to a VirtualName object that represents the virtual name.

RemoveVirtualName

Removes the specified virtual name.

Listing 3.5 provides the steps required to access a VirtualNames collection object.

Listing 3.5 Accessing a VirtualNames Collection Object

Set objDirControl = Server.CreateObject("SQLVDir.SQLVDirControl") 'create the 'base object ObjDirControl.Connect "IISServer", "1" 'connect to 'the first 'web site on 'the server 'IISServer Set objDirs = objDirControl.SQLVDirs 'get the 'virtual 'directory 'collection 'object Set objDir = objDirs.Item(0) 'get the first 'virtual 'directory 'object. You 'could say 'objDirs(0) 'since Item() 'is the 'default Set objNames = objDir.VirtualNames 'get the 'Virtualnames 'collection ObjDirControl.Disconnect

VirtualName Object

The VirtualName object is obtained by calling the Item method (or the AddVirtualName object if you are creating a new virtual name). Listing 3.6 demonstrates this.

The VirtualName collection object contains the following properties given in Table 3.11.

Table 3.11. VirtualName Properties

Property

Description

Name

The name of the virtual name being created.

Path

The directory path (absolute or relative) of the virtual name.

Type

The virtual name type that can have one of the following values.

 

Value

Description

 

1

Virtual name of type dbobject.

 

2

Virtual name of type schema.

 

4

Virtual name of type template.

Listing 3.6 Accessing a VirtualName Object and Setting Attributes

Set objDirControl = Server.CreateObject("SQLVDir.SQLVDirControl") 'create the base 'object ObjDirControl.Connect "IISServer", "1" 'connect to the 'first web site 'on the server 'IISServer Set objDirs = objDirControl.SQLVDirs 'get the virtual 'directory 'collection 'object Set objDir = objDirs.Item(0) 'get the first 'virtual 'directory 'object. You 'could say 'objDirs(0) 'since Item() is 'the default Set objNames = objDir.VirtualNames 'get the 'Virtualnames 'collection Set objName1 = objNames.Item(0) 'get the first 'virtual name 'object ObjNamem1.Type = 2 'set the 'properties of 'the virtual ObjNamem1.Name = "MySchema" 'name object 'obtained above ObjNamem1.Path = "C:\inetpub\schema" ... ObjDirControl.Disconnect

To create a new virtual name, do something similar to this:

Set NewVName = objNames.AddVirtualName "MyNewSchema", 2, "C:\inetpub\schema"

Категории