RDS 2.0 and later versions provide a default handler called MSDFMAP.Handler, which processes only the queries that the administrator has explicitly authorized through an .ini file. This .ini file, which is called msdfmap.ini and is in the Windows directory on the server, designates all the operations that the RDS.DataFactory object can execute on that server. In the .ini file, each authorized operation must have a name and has four sections: connect, sql, userlist, and logs. If the same operation name appears more than once in the .ini file, the handler considers only the first occurrence and ignores all the subsequent occurrences.
The sample msdfmap.ini file in Listing 2, page 52, contains an operation called EmployeeDB. The names of the first three sections in the script are composed of the keyword plus the name of the operation; the logs keyword doesn't need the operation name. If the file you specify in the logs section already exists, RDS appends any new errors to that file. Otherwise, RDS creates a new file to log errors to.
The connect section specifies the access type and the connection string. Note that if you plan to use ODBC to access data, you need to use a system Data Source Name (DSN), not a user DSN. The sql section contains the command text to execute, which can contain as many parameters as you need. Each parameter must have a question-mark placeholder in the text:
[sql EmployeeDB]
Sql="SELECT * from Employees where (lastname = ?" and firstname = ?)
The mapping between the actual and the formal parameters is established by position: The first actual parameter maps to the first occurrence of the question mark, and so forth. You use the userlist keyword to change for certain users the default permissions set in the connect section. In Listing 2, when users execute the EmployeeDB operation, all users except Administrator and Bibi have read-only access to the data. To ascertain the user credentials, RDS relies on a powerful IIS authentication method, which I describe in the sidebar "RDS Clients' User Credentials," page 52.
Coding to Use RDS Handlers
Let's look at how to write VBScript code that obtains a recordset through ADO and RDS with and without handlers. In an unsafe context, the following code would work:
server = "http://expoware"
strLastName = frm.empName.value
sql = "select * from Employees where lastname='" & strLastName & "'"
set rds = CreateObject("RDS.DataSpace")
set df = rds.CreateObject("RDSServer.DataFactory", server)
set rst = df.Query("DSN=NW", sql)
But if you run the same code on a machine where RDS is set to work safely, you'll get the error message box that Figure 2 shows.
To work in safe mode, RDS always needs a handler. If you don't specify a handler, RDS uses the default or raises an exception. You can set the default handler's ProgID in the DefaultHandler subkey under the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ DataFactory\HandlerInfo Registry subkey.
To use your own handler, register it as the default; then, you don't need to change anything else in your client code. If you want to use the RDS default handler, you must change all client code slightly to make the code comply with the authorized operations listed in the .ini file. To return a recordset with all the employees from the Northwind database, issuing a command such as
SELECT * FROM Employees
or a connection string such as
DSN=NW
isn't enough, and raises an error like the one in Figure 2. When you're working with the standard handler, you need to refer to the names of the operations instead of passing the raw command and connection strings. To make your application support the EmployeeDB operation, add the following lines to the msdfmap.ini file:
[connect EmployeeDB]
Access=ReadOnly
Connect="DSN=NW; uid=sa;pwd=;"
[sql EmployeeDB]
Sql="SELECT * from Employees where (lastname = ? and firstname = ?)"
Now modify the unsafe block of code from the beginning of this section in this way:
server = "http://expoware"
strLastName = frm.empName.value
sql = "EmployeeDB('" & strLastName & "')"
set rds = CreateObject("RDS.DataSpace")
set df = rds.CreateObject("RDSServer.DataFactory", server)
set rst = df.Query("Data Source=EmployeeDB", sql)
Prev. page
1
[2]
3
next page