Multithreaded SQL Server Demo

This example demonstrates how to create a multithreaded SQL Server application. Before you can run the demo, you must run Adduser.sql, included on the CD-ROM provided with this resource guide, from ISQL/w or SQL Enterprise Manager. This script will create 16 new users and grant them access to the various tables in the pubs database.

This multithreaded application will create up to 15 threads. Each thread will open a connection to SQL Server. None of the threads begins executing SQL statements until a manual reset event is signaled. In this case, the Win32 event acts like a traffic light. When the traffic light is "green," or signaled, each user starts sending SQL statements to the server.

By running your user interface on a separate thread with a higher priority, your users will perceive that your application is fast. Even with 15 users working on SQL Server, you can instantly and gracefully disconnect all threads by clicking Stop.

Clicking Stop resets the Win32 event object to the nonsignaled state and each thread disconnects from SQL Server and terminates by calling _endthreadex. The following illustration shows SQL Enterprise Manager.

The SQL Enterprise Manager Current Activity dialog box makes it easy to see who is using the server. To watch the SQL statements change while the demo runs, click Refresh.

To begin developing 32-bit applications for SQL Server, see the Microsoft SQL Server development libraries and documentation for ODBC, DB-Library, and Open Data Services, available with the SQL Workstation product, which also includes a complete single-user SQL Server, or MSDN Professional.