Readme_DB3D

DB3D is an example application that stores, visualizes and manipulates tri-dimensional wireframe models by using SQL Server 2005 and .NET Framework.

The default installation directory is drive:\Program Files\Microsoft SQL Server\90\Samples\Integrated Samples\DB3D

Scenario

The DB3D application illustrates how to take advantage of various SQL Server 2005 features to handle several common application needs.

The application can load tri-dimensional wireframe models from a file or from a database. It can also upload new wireframe models into a database by using various techniques that show different performance characteristics.

Wireframe models are represented by using triangles in space. Triangles are directly stored in the database as user-defined types and also used by the client application to manipulate the rendered wireframes.

Once in the database, wireframe models can be manipulated interactively on the client, or through Transact-SQL in the server by using the SQL-CLR methods included in the sample library. The application also supports reporting on the wireframes that are contained in the database, for which it uses custom aggregates to perform computation close to the data and avoids data-shipping costs.

Languages

Transact-SQL and Visual C#.

Features

The DB3D sample uses the new features of SQL Server 2005 as shown in the following table:

Application Area Features

Representing wireframe model data

Use of SQL Server 2005 user-defined types to appropriately represent pieces of information of the problem domain of the application. In the case of DB3D, two user-defined types are defined, "Triangle" and "Point3D". The example shows how to use the types both from within SQL Server and from a client application through ADO.NET 2.0.

Manipulating wireframe models

The "Triangle" and "Point3D" user-defined types include, in addition to their persistent state, a rich set of methods to manipulate them (for example to rotate a triangle in space). Those methods can be used both from the client and from within SQL Server itself to interact and change models.

Computing summary information for wireframe models

Use of SQL Server 2005 user-defined aggregates to perform processing close to the data (in the database) using custom algorithms. The sample can compute the surface of a given model by using a custom aggregate. This lets the system perform the computation in an efficient environment (CLR) without having to ship the data to another tier.

Uploading wireframe data quickly into the database

The application includes two strategies for uploading data: one based on regular INSERT statements and the other uses the new SqlBulkCopy class included in ADO.NET 2.0. Both are available so their performance characteristics can be compared.

Connect-to-database dialog box

The new SqlConnectionStringBuilder class is used to construct a connection string that is based on user-input in a safe way, that helps protect the application from connection-string-injection attacks.

Connect-to-database dialog box

The new SqlDataSourceEnumerator class is used to obtain the list of the instances of SQL Server that are available in the network.

Connect-to-database dialog box

The new database schema access API is used to retrieve the list of available databases from a connection.

Prerequisites

Before running this sample, make sure that the following software is installed:

  • SQL Server 2005 or SQL Server 2005 Express Edition (SQL Server Express). You can obtain SQL Server Express free of charge from the SQL Server 2005 Express Edition Documentation and Samples Web site.
  • The SQL Server 2005 Database Engine samples. These samples are included with SQL Server 2005. You can download the latest version of the samples at the CodePlex web site.
  • .NET Framework SDK 2.0 or Microsoft Visual Studio 2005. You can obtain .NET Framework SDK free of charge. See Installing the .NET Framework SDK.

Building the Sample

If you have not already created the strong-name key file SampleKey.snk generate the key file by using the following instructions.

To generate a strong name key file

  1. Open a Microsoft Visual Studio 2005 command prompt. Click Start, point to All Programs, point to Microsoft Visual Studio 2005, point to Visual Studio Tools, and then click Visual Studio 2005 Command Prompt.

    —Or—

    Open a Microsoft .NET Framework command prompt. Click Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click SDK Command Prompt.

  2. At the command prompt, use the change directory (CD) command to change the current folder of the Command Prompt window to the Samples folder.

    Note

    To determine the folder where samples are located, click Start, point to All Programs, point to Microsoft SQL Server 2005, point to Documentation and Tutorials, and then click Samples Directory. If the default installation location was used, the samples are located in <system_drive>:\Program Files\Microsoft SQL Server\90\Samples.

  3. At the command prompt, run the following command to generate the key file:

    sn -k SampleKey.snk

    Important

    For more information about the strong-name key pair, see "Security Briefs: Strong Names and Security in the .NET Framework" in the .NET Development Center on MSDN.

To build the sample, do the following:

To build the sample

  1. Compile the sample by using Visual Studio 2005 and the provided Visual Studio solution or by using Microsoft MSBuild, which is included in the .NET Framework SDK 2.0. Run a command similar to the following at the command prompt:

    msbuild /nologo /verbosity:quiet /property:Configuration=Debug CS\db3d.sln

  2. If you did not install the SQL Server Database Engine samples in the default location, modify the path in the CREATE ASSEMBLY part of the script in Scripts\InstallCS.sql to refer to location where the samples were installed.

  3. If you are not an administrator for the SQL Server instance you are using, you must have an administrator grant you CreateAssembly permission to complete the installation.

  4. Open the scripts\installCS.sql in SQL Server Management Studio. Run the script that is contained in the file, or run a command similar to the following at the command prompt:

    sqlcmd -E -I -i Scripts\InstallCS.sql

    This script performs the following actions:

    • Drops the old db3d database if it exists.
    • Registers the db3dsql assembly.
    • Registers some CLR UDTs and an aggregate.

Running the Sample

To run the sample, do the following:

Step 1: Loading models from files

  • The first time to run the application, it will not have any models or "wireframes" in the database. You must load them from files. The "models" subdirectory contains several model files you can use.

  • Click File, click Open and select one of the models from the "models" subdirectory. The model will display in the screen, and you can use the X, Y and Z sliders to rotate the model. You can see the user-defined types being used in the client to represent triangle information and to perform the transformations in the Triangle.cs and Point3D.cs files in the db3dsql project.

Step 2: Connecting to a database

  • To connect to a database, click Database and then click Connect. A dialog box will pop up where you can enter the name of the server, the database and authentication information. You should specify db3d as the name of the database to use. Once you are done click OK to connect. You can see how to build a connection string that is based on user input in the see the connectToolStripMenuItem_Click method in the MainFrm.cs file in the db3dwin project.

  • If you want, you can use the server and the database combo-boxes to browse the available servers and databases. You can see how to obtain the list of servers and/or databases in the file ConnectDlg.cs in the db3dwin project.

  • NOTE: the list of servers will include only those servers in the network that are currently available and that have been specifically enabled for discoverability.

Step 3: Uploading a model to the database

  • Once connected, you can upload a model

  • With a model loaded from a file from step 1, and after you connect to the database as described in step 2, click Database and then click Copy wireframe. This will prompt for a name and upload the triangle information to the database. You can also decide whether to enable "fast upload" using bulk-copy. You can experiment with the difference between doing many inserts one by one and using SqlBulkCopy for it. See the WireframeDbStorage.cs file in the db3dlib project.

  • You can repeat this operation with several other models (or even several copies of the same one with different names), so that you have a database that has many models in it.

Step 4: Viewing more model information

  • The application has a second tab that lets you see a list of all the wireframes in the database. You can also see the number of triangles they are made of and the total surface of the model. The interesting thing about this is that the surface is computed in the server without having to ship the data to the client by using a user-defined aggregate. You can see the user-defined aggregate implementation in the surface.cs file in the db3dsql project. Also see step 6 for information about how to use it in Transact-SQL.

  • To see this information, click the Wireframe Information tab.

Step 5: Rotating a model using Transact-SQL

  • When you use the slider controls in the client to rotate a wireframe, the application calls the "rotate" methods in each of the triangle objects that make up the model. Because these methods are part of the user-defined type that is registered in SQL Server, they can be used directly in the server.

  • Let's say that you can to rotate a model a fixed angle and store it rotated. Instead of bringing the data to the client, applying the transform, and then sending it back to the server, you can do this with a single T-SQL statement. To see this in action follow these steps:

  • Run the db3bwin application so that you can see the models. Connect to the database and select one model; for example, "tiger" (tiger.raw is the file that contains it if you did not upload it already).

  • Press F5 to refresh, and you will see that the tiger is looking towards you

  • Connect to the DB3D database using SQL Server Management Studio or some other query tool (for example, sqlcmd). Execute the following query:

    UPDATE Triangle SET TriangleObj.RotateY(0.6)FROM Triangle t INNER JOIN Wireframe w ON t.WireframeID = w.WireframeID WHERE w.WireframeName = 'tiger'
    
  • Now go back to the application and press F5. You will see that, when the tiger is reloaded from the database, it will appear rotated in the Y axis.

Step 6: Computing the surface of a model using T-SQL

  • The grid in the "Wireframe Information" shows the total surface of a given wireframe. This is done using the "Surface" custom aggregate that aggregates Triangle objects into a scalar value that represents the sum of the areas of all the triangles that matched the criteria of the query. You can use the custom aggregate in Transact-SQL directly. For example, you can execute this query in SQL Server Management Studio or some other query tool (for example, sqlcmd):

    CREATE PROCEDURE ListWireframesDetailed AS    SELECT w.WireframeID, WireframeName, COUNT(*) AS TriangleCount,           dbo.Surface(t.TriangleObj) AS Surface    FROM Wireframe w    INNER JOIN Triangle t ON w.WireframeID = t.WireframeID    GROUP BY w.WireframeID, WireframeName
    

Removing the Sample

Use the following procedure to remove the sample:

To remove the sample

  1. Load and run Scripts\cleanup.sql in Management Studio. Or, run the following command at the command prompt:

    sqlcmd -E -I -i Scripts\cleanup.sql

Comments

The CLR for SQL Server 2005 or SQL Server 2005 Express Edition must be enabled for this sample to work correctly.

Error message during deployment

When running the application from Visual Studio 2005, you might get an error that says that there was an error during deployment. Because you already created the database with all its elements by executing the createdb.sql script, you do not have to worry about that. It is. ok to say "ok to continue". If later, you change the db3dsql assembly, you will have to re-deploy it to SQL Server by using the script (or part of the script if you do not want to lose the existing model data).

About the graphics rendering code

This example is designed specifically to illustrate usage of various new features in SQL Server 2005 and Visual Studio 2005. It is not the goal of the example to provide a real 3-D visualization tool. To avoid unnecessary complexity in the example, the code that renders 3-D models in the screen is very simplistic and does not not perform common optimizations such as double-buffering nor does it do appropriate adjustments such as perspective correction.

Samples are provided for educational purposes only. They are not intended to be used in a production environment and have not been tested in a production environment. Microsoft does not provide technical support for these samples. Sample applications and assemblies should not be connected to or used with your production SQL Server database or your report server without the permission of the system administrator.

See Also

Concepts

CLR Programmability Samples

Help and Information

Getting SQL Server 2005 Assistance

Change History