Lemur zaprasza
Chapter 27 SQL OLE Integration SQL Servers Object Model Why Use SQL-DMO? Creating Applications with SQL-DMO Using Visual Basic Required SQL-DMO Files Enhancing the SQL Server DBA Assistant Whats in the SQL Server DBA Assistant? Connecting to SQL Server Listing 27.1. Procedure main: creating a SQL Server object. Listing 27.2. SQL Server connection. Estimating Memory Listing 27.3. Memory estimate code. Filling a Combo Box with Databases Listing 27.4. Populating a combo box with database names. Listing 27.5. Populating a list box with table names using a database object and the tables collection. Performing Table Maintenance Listing 27.6. Performing update statistics on selected tables. Performing Table Exports Using BulkCopy Listing 27.7. Performing a BCP export on selected tables. Summary by Mark Spenik Have you ever felt that Microsoft left out a utility or window of information you thought would really make your life easier? With SQL Server 6.x, you may be able to write that utility yourself! "How?" you ask. The answer is an exciting new feature added to SQL Server 6.0 and extended with SQL Server 6.5--OLE! OLE stands for Object Linking and Embedding, but in the past few years, it has come to stand for so much more. A few years ago, Microsoft and several integrated system vendors created an open specification for application intercommunication called OLE. The OLE specifications defined more than applications communicating with one another; it also specified how applications can expose parts of their functionality as objects to be used by other applications. Application developers could then create applications that used parts of other applications to further enhance their own applications. For example, you could create an application that used the charting capabilities of another application or included a spell checker into a text editor application. What does OLE have to do with SQL Server? In SQL Server 6.x, SQL Server is an OLE object application (also called an OLE server). That is, SQL Server exposes several objects, methods, and properties that can be easily controlled programmatically to perform database administrative tasks. Microsoft calls the objects SQL-DMO (SQL Distributed Management Objects). Using SQL-DMO, you can easily create applications that perform many DBA tasks for you! NOTE: This chapter almost seems out of place in a book on DBA survival. However, it introduces a technology that truly empowers the DBA, enabling the DBA to create his or her own powerful database utilities. Even if you think this chapter seems too much like a programming chapter, hang in there! The explanations in this chapter are geared toward DBAs, not programmers. Even if you don't know how to program, you will at least understand what can be done and you may be able to have someone program your utility for you! Before going into more detail, here is a quick review of some OLE terminology: Container/controller/client application: An application that can create and manage OLE objects. Visual Basic is an example of a container appli-cation. Server/object application: An application that creates OLE objects. SQL Server is an object application. OLE automation: OLE automation is a standard that enables applications to expose their objects and methods so that other applications can use them. Object: Defining an object is a bit difficult. If the OLE definition of an object is used, the discussion gets into many other aspects of OLE, that are covered in detail in other Sams books, but that confuse the topic of this book. This chapter uses a simpler definition: an object represents some sort of data with properties and methods. In SQL Server terms, for example, a database is an object and a stored procedure is an object. Figure 27.1 shows the case of a database object. The object has attributes (in OLE terminology, they are called properties). In Figure 27.1, some of the properties of a database object are listed: Name, CreateDate, Size, and Status are all examples of properties of the database object. The Name property for the database shown in Figure 27.1 is Pubs. A property tells you something about the object. You can read properties, and in some cases, you can also set properties. Objects also have methods. A method is an action the object takes on the data it represents. Examples of the database objects methods are shown in Figure 27.1. The dump method, for example, can be used on the database object. If you invoked the dump method of the database object named pubs, what do you think would happen? If you said, "a database backup," you are correct. Figure 27.1. An example of a database object. Collections: A collection is an object that consists of items that can be referred to as a group (see Figure 27.2). In Figure 27.2, there are several standard SQL Server databases: master, pubs, model, and tempdb. If you group all the databases shown into one large group called databases, you have a collection. Figure 27.2. An example of a collection object. Collections enable you to easily perform tasks on each item in the collection. To perform a DBCC CHECKDB command on every database on your SQL Server, for example, you can use the collection object to get each database on the server and invoke a method that performs that DBA task. SQL Servers Object Model To use SQL-DMO, you must understand the SQL Server object model. The object model is the hierarchy of exposed SQL Server objects you can use programmatically. SQL Server's object model, taken from the Distributed Management help file, is shown in Figure 27.3. Figure 27.3. SQL Server distributed management object model. Follow the object model just like you would a file directory tree. The top level of the object model, for example, is the Application object. NOTE: It is standard practice when creating OLE object models from a stand-alone application to include an Application object. Follow the tree to the next level to find the primary object you will use: the SQL Server object. If you look at Figure 27.3, you will see that the Database, Device, Login, Language, RemoteServer, and Configuration objects are all below the SQL Server object. These objects are said to be dependent on the SQL Server object; that is, you must have a SQL Server object before you can "get to" (that is, use) any of the dependent objects. Why Use SQL-DMO? What benefits can you get from learning to use SQL-DMO? The real gain is that you can easily create custom solutions for your database administration environment, allowing you more free time to perform other tasks. For example, you can create a user wizard that performs a series of tasks, such as adding the user to every database based on the user's group. Using SQL-DMO, you can create applications that you normally have to perform manually. Currently, you can automate many tasks you perform regularly by using stored procedures. The advantage SQL-DMO has over stored procedures for performing administrative tasks is simplicity. By using collection objects, you can easily perform DBCC commands on every database on the server, using only a few lines of code. Many Transact SQL commands have been simplified. The DBCC command and the many different DBCC options become methods of different objects. Another advantage SQL-DMO has over stored procedures is that you can take advantage of true programming languages that have more powerful programming features than Transact SQL. Not to mention that you can easily integrate your applications into other desktop applications (such as word processors or spreadsheets) to enhance your customized database administration applications. Following is brief list of some of the many administrative tasks you can perform (this is a brief list; SQL-DMO enables you to perform almost any system administrative task): Back up/restore a database Generate scripts for stored procedures Perform the UPDATE STATISTICS command Perform DBCC commands such as CheckTable, CheckCatalog, and so on Grant and revoke privileges Add alerts Perform BCP Transfer data from one server to another Manage users Creating Applications with SQL-DMO Using SQL-DMO requires a 32-bit programming language that can create OLE controller applications. Such tools as Microsoft Visual C++, Borland Delphi, or Microsoft Excel for Windows NT with 32-bit VBA (Visual Basic for Applications) can easily be used. The examples and code samples shown in this chapter are based on Microsoft Visual Basic 4.0. The choice of Visual Basic is easy because it is the most popular and rapid application development tool available. The core language of Visual Basic 4.0 is VBA (Visual Basic for Applications) and can be found in the Windows 95 releases of Access, Project, and Excel. The remainder of the chapter focuses on using SQL-DMO objects to perform a variety of database administration tasks using Visual Basic. Using Visual Basic TIP: If you are not familiar with Visual Basic, pick up a beginner's book and learn the Visual Basic basics. Once you know the basics, you can use the suggestions and examples in this chapter effectively to create your own applications. The following discussion of Visual Basic is brief and is given primarily for those who are not familiar with Visual Basic so that they can understand SQL-DMO. Following is a very brief introduction to Visual Basic to help you understand the terminology used when creating an application that takes advantage of SQL-DMO. The main screen of Visual Basic 4.0 is shown in Figure 27.4. Figure 27.4. Visual Basic 4.0. Creating Visual Basic applications consists of creating forms, adding controls to the forms using the toolbar, and adding code to modules and forms that make up the application. The forms and code modules that make up a Visual Basic application are called a project and can be found in the project window shown in Figure 27.4. The next sections step you through some Visual Basic basics you will need to create your own SQL-DMO applications or to enhance the application provided with this book (the DBA Assistant). Adding a Control to a Form and Setting Properties A Visual Basic control is similar to a SQL-DMO object in that they both have properties and methods. Understanding the properties and methods used with a Visual Basic control can help you better understand the concepts of SQL-DMO objects and properties. To add a control to a form, perform the following steps: Click once on the icon on the toolbar of the control you want to add to the form. Common controls used on the toolbar are shown in Figure 27.5. Figure 27.5. The Visual Basic toolbar. Place the mouse cursor on the form; while holding the left mouse button, drag the mouse down. Visual Basic begins to draw a control on the form. Release the left mouse button. You now have added a control to the form. To set properties (such as color, name, height, width, and so on) for the control or the form, click the form or control to make it the active object and press F4. The Properties window for the object appears (see Figure 27.6). To change a property, select the property field and enter a new value. For example, if you want to change the name of the control, edit the Name property. Declaring a SQL-DMO Object in Visual Basic To use a SQL-DMO object, you must first declare the object in your code. With Visual Basic, you can use the generic object type, which can hold any type of OLE object, or you can declare an object of a specific SQL-DMO object type by using the type library. To create a variable using a generic object, use the following syntax: Dim Variable_Name As Object To create a specific SQL-DMO, use the following syntax: Dim Variable_Name As SQLOLE.SQL_DMO_OBJECT In this syntax, SQL_DMO_OBJECT is the specific SQL-DMO object (such as SQL Server, Database, Table, and so on). For example, to define a SQL-DMO SQL Server object using the type library, enter the following: Dim MySqlServer As SQLOLE.SQLServer TIP: Declare SQL-DMO variables by using the type library and declaring specific SQL Server objects rather than using the generic object. Using specific objects is faster and enables Visual Basic to perform "early binding" (checking that you are using proper objects and methods) during compilation rather than at run time. Creating a SQL-DMO Object with Visual Basic After you declare a variable to be a SQL-DMO object, you must create the object before you can use the methods and properties of the object. NOTE: Creating an object is also referred to as getting an instance of the object. You can create the object with either the keyword New or the function CreateObject. Following is an example that uses the New keyword when declaring a variable: Dim MySqlServer As New SQLOLE.SQLServer You also can use the New keyword in code, as follows: Set MySqlServer = New SQLOLE.SQLServer The CreateObject function has the following syntax: CreateObject("application_name.object_type") The following code creates a new SQL Server SQL-DMO object with CreateObject: Set MySqlServer = CreateObject("SQLOLE.SQLServer") After you create an object, you can use the objects, properties, and methods to perform DBA tasks. Releasing Objects Just as important as creating an object is releasing the object when you are finished with it. Objects in Visual Basic are released when they go out of scope. If the object is declared in a procedure, the object is released when the procedure completes. If the object is declared in a form, the object is released when the form unloads. Global objects are not released until the application closes. It is always good Visual Basic coding practice to release your objects in code when you are finished with them by using the keyword Nothing. The following code, for example, releases a SQL-DMO table object called MyTable: Set MyTable = Nothing Required SQL-DMO Files To create SQL-DMO objects using Visual Basic, you must have the following files, which are included with the 32-bit versions of SQL Server client utilities for Windows NT and Windows 95. You can find the following files in the SQL Server 6.5 home directory (C:\MSSQL) in the directory \BINN: SQLOLE.HLP SQL-DMO help files, including object hierarchy SQLOLE65.DLL In-process SQL-DMO server (in SQL Server 6.0, the filename is SQLOLE32.DLL) SQLOLE65.TLB Type library for OLE Automation Controllers (in SQL Server 6.0 the filename is SQLOLE32.DLL) NOTE: SQL-DMO is available only in 32-bit Windows environments (Windows NT and Windows 95). You can use the following checklist when creating SQL-DMO applications. Use this first checklist to ensure that you have the proper files and utilities required to use SQL-DMO: Have Windows NT or Windows 95. Have installed a 32-bit OLE automation controller (Visual Basic). Install the proper SQL-DMO files from the 32-bit SQL Server Client utilities. The following checklist includes the steps required to create SQL-DMO objects from Visual Basic: Include the SQL-DMO type library in the Visual Basic environment by adding "Microsoft SQLOLE Object Library" to the Visual Basic references. Declare a SQL-DMO SQLServer object. Create the SQLServer object. Connect the SQLServer object to SQL Server. Use the SQL Server objects, properties, and methods, and declare and create any other required SQL-DMO objects to accomplish your required DBA task. Release SQL-DMO objects using the keyword Nothing when you are done using them. Disconnect the SQLServer object. Release the SQLServer object. Enhancing the SQL Server DBA Assistant Now comes the real value-added part of the chapter. As you probably know by now, examples that use SQL-DMO are hard to find. SQL Server ships with a few SQL-DMO samples that are not well documented; the overall SQL-DMO documentation contains very few descriptive examples, concentrating instead on describing the objects and methods. On the CD-ROM included with this book is a Visual Basic project titled samsdb.vbp. The project contains all the source code for the application and is called the SQL Server DBA Assistant. The source code is included as a foundation that you can modify and enhance to meet your own needs. The following sections discuss the most important parts of the SQL Server DBA Assistant. NOTE: The source code for the application is included on the CD-ROM that accompanies this book. The following sections concentrate on the code that uses SQL-DMO, not the Visual Basic code that does not deal with SQL-DMO. The Visual Basic code is well documented so that you can use the code and form to easily add your own functionality to the project. Whats in the SQL Server DBA Assistant? Before getting started on developing the SQL Server DBA application, you must decide what type of functionality you are going to put in the application. First, because the purpose of the utility is for actual DBA work and learning, you should create an application that uses several different SQL-DMO objects. What do I think is missing from Microsoft SQL Server 6.5? Memory configuration for SQL Server is very important and yet there is no screen that graphically shows how much memory is currently allocated to SQL Server, the procedure cache, data cache, or SQL Server overhead. Although you can always use the DBCC MEMUSAGE command to get this information, the report is not very graphical. To fix this oversight, the first task your SQL Server DBA Assistant will accomplish is to perform SQL Server memory estimates and breakouts using the formulas published in Chapter 19, "Which Knobs Do I Turn?," and in the SQL Server documentation. To perform this task, you must use the SQL Server object and the configuration object. You concentrate on setting up a program that enables you to perform table maintenance on several different databases. To perform these tasks, you must use the SQL-DMO database object and the table object. Following is a list of the functionalities of the SQL Server DBA Assistant: Estimate and graph SQL Server memory breakout List all the databases in a combo box for selection Perform table maintenance on selected tables Perform BCP export on selected tables Connecting to SQL Server NOTE: This chapter skips a few steps here that are Visual Basic related, such as creating a new project called samsdba and adding controls to the logon form. Assuming that you have all the proper files and have added the Microsoft SQLOLE Object Library references to Visual Basic, it is now time to declare a SQL Server object and connect to SQL Server. For logon purposes, use the form shown in Fig-ure 27.7 (frmLogon). Figure 27.7. The SQL Server DBA Assistant Logon form. Using your checklist for creating a SQL-DMO application, perform step 2: declare a SQL-DMO object, as follows in the Visual Basic module globals.bas: Public MySqlServer As SQLOLE.SQLServer `Global SQL Server Object The next step is to create a SQL Server object. The code to create a SQL Server object is located in the Visual Basic module sqlserv.bas in the procedure main. The code for the main procedure is shown in Listing 27.1. Listing 27.1. Procedure main: creating a SQL Server object. Public Sub main() `SAMS -MicroSoft SQL Server DBA Survival Guide ` `Main - The procedure main creates an OLE SQL Server Object ` and then prompts the user to enter the correct SQL Server ` name. If the user properly connects to the SQL Server ` the main form of the application is shown. ` `Set up Error handling On Error GoTo Err_Main ` `Check if the application is already running ` If App.PrevInstance > 0 Then MsgBox "SQL Server DBA Assistant already running on this machine.", _ vbCritical, "Already Running" End End If ` `Create a New SQL Server OLE Object ` Set MySqlServer = CreateObject("SQLOLE.SQLServer") Connected = False `Set Global to Not Connected ` `Set SQL Server Connection Timeout Value ` MySqlServer.LoginTimeout = 15 `Set for 15 seconds ` `Display the Logon Screen ` frmLogon.Show 1 Set frmLogon = Nothing `Reclaim Object Memory `If We established a Connection Display the Main form ` Otherwise exit the application If Connected = True Then frmSplash.Show `Display Splash Screen DoEvents `Allow time to Paint the Splash Screen Load frmMain `Load the Main Form frmMain.Show `Make it Appear Unload frmSplash `Make it disappear Set frmSplash = Nothing `Reclaim Memory Exit Sub End If ` `Exit - If not Connected Quit_App: If Not (MySqlServer Is Nothing) Then `Release SQL Server Object Set MySqlServer = Nothing End If End `End the program ` `Error Handler ` Err_Main: ` `Display Error Message MsgBox Err.Description, vbCritical, "Connection Error" Resume Next End Sub The following line creates a SQL-DMO SQL Server object using the function CreateObject (as specified in step 3 of the SQL-DMO checklist): Set MySqlServer = CreateObject("SQLOLE.SQLServer") When this line of code executes, the variable MySqlServer contains a SQL Server object. Before you try to connect to a SQL Server by logging on, you set the login timeout value by setting the SQL Server object property LoginTimeout, as follows: MySqlServer.LoginTimeout = 15 `Set for 15 seconds You now are ready to perform step 4 of the checklist: establish a connection to SQL Server. The logon form appears (refer to Figure 27.7). A user enters the SQL Server, user name, and password and clicks the Logon button on the form. The code shown in Listing 27.2 executes to establish a connection to the SQL Server. Listing 27.2. SQL Server connection. Private Sub cmdLogon_Click() `Set up the Error Handler ` On Error GoTo Err_Logon ` `Connect to the SQL Server ` If txtServer <> "" Then Me.MousePointer = vbHourglass `Turn Cursor to HourGlass ` `Invoke Connect Method of the SQL Server Object ` MySqlServer.Connect ServerName:=txtServer.TEXT, _ Login:=txtLogon.TEXT, _ Password:=txtPassword.TEXT ` `Sql Server Connected Correctly - Unload the form ` Connected = True `Set Global Connection Variable Me.MousePointer = vbDefault `Turn Mousepointer back to default Unload Me `Unload the Logon form Else MsgBox "You must enter a SQL Server Name to Connect", _ vbCritical, "Invalid Entry" End If ` `Exit the routine - If Not Logged In Try Again ` Exit_Logon: Exit Sub ` ` Error handler ` Err_Logon: Me.MousePointer = vbDefault MsgBox "Error Connection to Server. Error: " & Err.Description, _ vbCritical, "Error Connection" Resume Exit_Logon End Sub The following lines of code establish a connection with SQL Server using the Connection method of the SQL Server object: ` `Invoke Connect Method of the SQL Server Object ` MySqlServer.Connect ServerName:=txtServer.TEXT, _ Login:=txtLogon.TEXT, _ Password:=txtPassword.TEXT After you establish a successful connection to SQL Server, you are ready to perform steps 5 and 6 of the SQL-DMO application checklist: perform various tasks by creating objects, invoking methods, and setting properties. Estimating Memory To configure the memory breakout for SQL Server, you must read the memory configuration parameter to get the total amount of memory. You also must get the configuration value for the procedure cache and subtract that value from 100 to get the percentage of memory used for the data cache. Before the procedure and data cache values can be computed, you must compute the SQL Server overhead. To compute SQL Server overhead, you must read configuration values, compute the total amount of memory used by each configuration object, and add the value to SQL Server static memory requirements. Refer back to the SQL-DMO object model shown in Figure 27.3 to see the SQL Server configuration object. Using the configuration object, you can easily obtain the configuration values. To create the configuration object, first declare the configuration object, as follows: Dim MyConfig As SQLOLE.Configuration, ConfigV As SQLOLE.ConfigValue To create the configuration object, use your SQL Server object by executing the following code (the configuration object depends on the SQL Server object): `Get a configuration object Set MyConfig = MySqlServer.Configuration The variable MyConfig now contains a SQL-DMO configuration object. Using the ConfigValues collection of the configuration object, the configuration values can easily be obtained. To get the running configuration value for the memory configuration parameter, for example, execute the following code: TotalMemory = CInt((DATA_PAGE * MyConfig.ConfigValues("memory").RunningValue) / MEGA_BYTE) The memory configuration value is in 2K data pages. For the memory estimation graph, all the values are converted to megabytes. Instead of using the ConfigValues collection, you can create an instance of a specific ConfigValue object and then retrieve the values. The following example creates a ConfigValue object for locks and then retrieves the value: `Locks Set ConfigV = MyConfig.ConfigValues("locks") TempValue = ConfigV.RunningValue * MEM_LOCKS NOTE: To compute the memory requirements for configuration objects to estimate SQL Server overhead, constants were used; if Microsoft publishes more accurate object memory requirements, you can easily modify the constants located in the global.bas module. To estimate the SQL Server memory breakdown, the various configuration values are read using the configuration object and ConfigValues collection. The SQL Server overhead, procedure, and data cache are then computed and graphed. The breakout of SQL Server memory is computed during the loading process of the main form (frmMain) of the SQL Server DBA Assistant. The Memory Estimation page of the SQL Server DBA Assistant dialog box is shown in Figure 27.8. Figure 27.8. The Memory Estimation page of the SQL Server DBA Assistant dialog box. The code to perform the memory estimate and build the graph shown in Figure 27.8 is shown in Listing 27.3. Listing 27.3. Memory estimate code. Public Sub EstimateMemory() ` `Define Values to Compute Memory Allocation ` Dim MyConfig As SQLOLE.Configuration, ConfigV As SQLOLE.ConfigValue Dim MemoryOverhead As Single, DataCache As Single, ProcCache As Single Dim TempValue As Single, TotalMemory As Integer On Error GoTo Memory_Estimate_Error ` `Use the standard formula to compute Memory usage estimates Set MyConfig = MySqlServer.Configuration ` `Get Memory ` ` Note: We will use two different methods to get at the configuration ` values (for learning purposes). ` Method 1 uses the Configuration Configvalues collection ` Method 2 creates a ConfigValue Object ` Method 1 TotalMemory = CInt((DATA_PAGE * MyConfig.ConfigValues("memory").RunningValue) / MEGA_BYTE) ` `Get Procedure Cache and Data Cache Values ` ProcCache = MyConfig.ConfigValues("procedure cache").RunningValue DataCache = 100 - ProcCache ` `Do SQL Server Overhead ` MemoryOverhead = MEM_DEVICES + MEM_STATIC_OVERHEAD ` `Method 2 ` `Locks Set ConfigV = MyConfig.ConfigValues("locks") TempValue = ConfigV.RunningValue * MEM_LOCKS MemoryOverhead = MemoryOverhead + TempValue ` `Users Set ConfigV = MyConfig.ConfigValues("user connections") TempValue = ConfigV.RunningValue * MEM_USER MemoryOverhead = MemoryOverhead + TempValue ` `Databases Set ConfigV = MyConfig.ConfigValues("open databases") TempValue = ConfigV.RunningValue * MEM_DATABASE MemoryOverhead = MemoryOverhead + TempValue ` `Objects ` Set ConfigV = MyConfig.ConfigValues("open objects") TempValue = ConfigV.RunningValue * MEM_OBJECTS MemoryOverhead = (MemoryOverhead + TempValue) / (MEGA_BYTE) ` `Compute Values ` TempValue = TotalMemory - MemoryOverhead ProcCache = TempValue * (ProcCache / 100) DataCache = TempValue * (DataCache / 100) ` `Release the Objects ` Set ConfigV = Nothing Set MyConfig = Nothing ` `Setup the Graph With Information ` Setup Data Points With grphMemory .AutoInc = 1 `Turn Auto Increment On .DrawMode = 0 `Disable drawing until the end .NumPoints = 3 `Set total number of points .ThisPoint = 1 `Start with Point 1 `OverHead .GraphData = CInt(MemoryOverhead) `Set graph point - Using Integer `Procedure Cache .GraphData = CInt(ProcCache) `Data Cache .GraphData = CInt(DataCache) End With ` Setup Colors for the Graph With grphMemory .ColorData = 7 `Red .ColorData = 14 .ColorData = 12 End With `Setup labels for each graph With grphMemory .LabelText = Format(MemoryOverhead, "######.00 MB") .LabelText = Format(ProcCache, "######.00 MB") .LabelText = Format(DataCache, "######.00 MB") End With `Setup The legend and the title on the bottom With grphMemory .BottomTitle = "Total Memory " & Str$(TotalMemory) & " MB" .LegendText = "SQL Server Overhead" .LegendText = "Procedure Cache" .LegendText = "Data Cache" .DrawMode = 2 `Draw the graph End With ` Memory_Estimate_Exit: Exit Sub ` ` Error handler ` Memory_Estimate_Error: Me.MousePointer = vbDefault MsgBox "Error estimatin memory configuration." _ & "Error: " & Err.Description, _ vbCritical, "Memory Configuration Error" Resume Memory_Estimate_Exit End Sub Filling a Combo Box with Databases To make the SQL Server DBA Assistant a useful tool during database table maintenance, you will add the capability to select a database from a combo box and then read all the nonsystem tables associated with the database into a Visual Basic list box control. To read all the databases on the selected server into a combo box, you use the SQL Server SQL-DMO object and the databases collection. The code shown in List- ing 27.4 populates a Visual Basic combo box with all the database names in your SQL Server object collection. Listing 27.4. Populating a combo box with database names. Dim Db As SQLOLE.DATABASE CenterForm frmMain ` `Fill the Combo Box on the form with the `available databases by using the SQL Server databases collection ` For Each Db In MySqlServer.Databases ` Make sure the database is not currently being loaded ` If Db.Status <> SQLOLEDBStat_Inaccessible Then cmbDatabase.AddItem Db.Name Else MsgBox "Database: """ + Db.Name _ + " "" can not be accessed at this time.", _ vbCritical, "Database Loading" End If Next Set Db = Nothing To populate a list box with the tables in the database, you read the tables collection of the selected database. The code to populate the list box using the selected database is shown in Listing 27.5. Listing 27.5. Populating a list box with table names using a database object and the tables collection. Private Sub cmbDatabase_Click() Dim WorkTable As SQLOLE.TABLE `SQL-DMO Table Object On Error GoTo Get_Tables_Error ` `Database changed - Modify Database Object ` Set WorkDb = Nothing `Clear the Work Database object lstTables.Clear `Clear tables list box lstOperateTables.Clear `Clear the operate tables list box `Get the currently selected database object ` Set WorkDb = MySqlServer.Databases(cmbDatabase.TEXT) ` `Fill the list box with the table names using the database `tables collection exclude any system tables. ` For Each WorkTable In WorkDb.Tables `Do For Each table in the database If Not (WorkTable.SystemObject) Then lstTables.AddItem WorkTable.Name `Add to the list Box End If Next WorkTable Exit_Get_Tables: Set WorkTable = Nothing Exit Sub `Leave the Procedure ` ` Error handler ` Get_Tables_Error: Me.MousePointer = vbDefault MsgBox "Error reading tables collection " & Err.Description, _ vbCritical, "Filling Combo Box Error" Resume Exit_Get_Tables End Sub TIP: You can begin to see that using SQL-DMO is quite simple once you become familiar with the SQL-DMO object model. Study the model and become familiar with the collections, objects, and the hierarchy. Getting a list of objects is simple using the Visual Basic FOR EACH - NEXT statement. FOR EACH - NEXT is used to read through all items of an array or collection. Examples of the FOR EACH - NEXT statement can be found in Listings 27.4 and 27.5. Performing Table Maintenance Once a database has been selected, a database object can easily be created using the selected database name and the SQL Server object, as follows: `Get the currently selected database object ` Set WorkDb = MySqlServer.Databases(cmbDatabase.TEXT) Once the line of code executes, you have a SQL-DMO database object for the selected database. If you remember the object model for SQL-DMO, you can easily create a table object using the database object. Once the table object has been created, you then can perform a variety of table maintenance tasks using the different table methods. Following are some examples of the table object methods and the tasks they perform: CheckTable Performs the DBCC CheckTable command. Grant Grants table privileges to a list of SQL Server users or groups. RecalcSpaceUsage Recalculates the space information for the table. Script Generates the Transact SQL statements to create the table. UpdateStatistics Updates the data distribution pages used by the Query Optimizer to make proper index selection. For the SQL Server DBA Assistant, you can select the tables on which you want to perform a table maintenance operation and then click a button to perform the appropriate action. The code that scans through the list of selected tables and invokes the method is as follows: `Execute Update Statistics command on selected tables ` For X = 0 To lstOperateTables.ListCount - 1 ProgressBar1.VALUE = X Set WorkTable = WorkDb.Tables(lstOperateTables.List(X)) ` `Update Statistics on the Table - using the UpdateStatistics Method ` WorkTable.UpdateStatistics `Release the Work Table object Set WorkTable = Nothing Next X The Table Maintenance page of the SQL Server DBA Assistant dialog box is shown in Figure 27.9. Figure 27.9. The Table Maintenance page of the SQL Server DBA Assistant dialog box. Listing 27.6 shows the code used behind the Update Statistics button (shown in Figure 27.9). Listing 27.6. Performing update statistics on selected tables. Private Sub cmdUpdate_Click() Dim WorkTable As SQLOLE.TABLE `SQL-DMO Table Object Dim X As Integer On Error GoTo Up_Stats_Error SSPanel1.Enabled = False frmMain.MousePointer = vbHourglass ` `Setup The Progress Bar ProgressBar1.MAX = lstOperateTables.ListCount - 1 ProgressBar1.VALUE = 0 lblStatus.Caption = "Updating Statistics" frmStatus.Visible = True `Turn On Progress Bar DoEvents `Allow Screen to repaint ` StatusBar1.Panels("status").TEXT = "Updating Statistics - Please Wait..." `Execute Update Statistics command on selected tables ` For X = 0 To lstOperateTables.ListCount - 1 ProgressBar1.VALUE = X Set WorkTable = WorkDb.Tables(lstOperateTables.List(X)) ` `Update Statistics on the Table - using the UpdateStatistics Method ` WorkTable.UpdateStatistics `Release the Work Table object Set WorkTable = Nothing Next X `Cleanup and Exit Up_Stats_Exit: ` frmStatus.Visible = False StatusBar1.Panels("status").TEXT = "" SSPanel1.Enabled = True frmMain.MousePointer = vbDefault Exit Sub ` ` Error handler ` Up_Stats_Error: Me.MousePointer = vbDefault MsgBox "Error Updating statistics on table " & lstOperateTables.List(X) _ & "Error: " & Err.Description, _ vbCritical, "Update Statistics Error" Resume Up_Stats_Exit End Sub TIP: We have provided you with the following three table maintenance functions already programmed and ready to use. Look on the CD-ROM that accompanies this book; they're with the SQL Server DBA Assistant. Update Statistics Recompile References DBCC CheckTable As stated earlier, the purpose of the SQL Server DBA Assistant is to provide you with a foundation from which you can create your own application. If you look behind each of the buttons, you will notice that the code is almost identical except for the methods added. You can easily add more functionality by cutting and pasting the code into new buttons and adding new methods. You also can optimize the application by reducing the code behind the buttons by using a shared function or procedure. The list is endless--what are you waiting for? Performing Table Exports Using BulkCopy One of the new objects added to SQL Server 6.5 is the BulkCopy object ( BCP for short). Because the DBA Assistant is all about tools left out of the Enterprise Manager, the ability to perform graphical BCP is very important--especially if you are in environments I find myself in quite often: where the data fed into SQL Server is from mainframe flat files (or vice versa). To take advantage of graphical BCP, you must use the 4.2x Object Manager or create your own graphical BCP tool. For the SQL Server DBA Assistant, we start you down the road of creating your own graphical BCP tool by adding the ability to export data using the BulkCopy object in the following formats: Tab delimited (default) Comma delimited Native format The BulkCopy object differs from the objects used so far in these example applications because the BulkCopy object does not depend on other objects. To use a BulkCopy object, you create the BulkCopy object, set the various parameters of the BulkCopy object, and then pass the BulkCopy object as a parameter to a table or view object's ImportData or ExportData method. The BulkCopy object has a single method, Abort, to abort a running BCP; this method must be executed from another thread. For the SQL Server DBA Assistant, the BulkCopy object is created when the object parameter oBcp is declared using the keyword New as follows: Dim oBCP As New SQLOLE.BulkCopy `Note BCP object is created here using New Keyword Once the BulkCopy object has been created, the next step is to set the desired properties (such as the import batch size or the number of errors to ignore before halting the bulk copy). Here is an example of setting the BulkCopy object's MaximumErrorsBeforeAbort property: `Max Number of errors before BCP quits If IsNumeric(txtMaxErrors.Text) Then oBCP.MaximumErrorsBeforeAbort = CInt(txtMaxErrors.Text) Else `Use Default oBCP.MaximumErrorsBeforeAbort = 1 End If When you set the properties of the BulkCopy object to import or export data, you pass the BulkCopy object as a parameter to a table or view object's ImportData or ExportData method. The following example shows the ExportData method being used: iNumRows = BCPTable.ExportData(oBCP) The Bulk Copy page of the SQL Server DBA Assistant dialog box is shown in Figure 27.10. Figure 27.10. The Bulk Copy page of the SQL Server DBA Assistant dialog box. Listing 27.7 shows the code used behind the Export Data button (shown in Figure 27.10). Listing 27.7. Performing a BCP export on selected tables. Private Sub cmdExportData_Click() Dim BCPTable As SQLOLE.Table `SQL-DMO Table Object Dim oBCP As New SQLOLE.BulkCopy `Note BCP object is created here using New Keyword Dim iNumRows As Long `Stores number of rows returned from BCP Dim oOutputFile As CFile `Used for file I/O Dim sTempBuf As String, sTemp As String `Temp variables `Set up a simple error handler On Error GoTo Export_Error `Setup display SSPanel1.Enabled = False frmMain.MousePointer = vbHourglass ` StatusBar1.Panels("status").Text = "Exporting Data - Please Wait..." `Step 1 - Get an instance of the table object to perform the BCP ` `Get the table object - to perform the export ` Set BCPTable = WorkDb.Tables(lstBCPTables.Text) `Step 2 - Set up the BCP objects properties ` ` (Note: An instance of the BCP object was created above in DIM statement ` using the keyword New) ` `Set the BulkCopy input/output file parameter oBCP.DataFilePath = Dir1.Path & "\" & txtFile `Set the error log and log file parameters oBCP.LogFilePath = App.Path & "\sams_bcp.log" oBCP.ErrorFilePath = App.Path & "sams_err.log" ` `Do some validation checking and set optional paramters ` ` Batch Size (Not used for exporting - only used in imports ` added here for your convenience - should you modify ` the program to do imports). ` If IsNumeric(txtBatchSize.Text) Then oBCP.ImportRowsPerBatch = CInt(txtBatchSize.Text) Else `Use Default oBCP.ImportRowsPerBatch = 1000 End If `Max Number of errors before BCP quits If IsNumeric(txtMaxErrors.Text) Then oBCP.MaximumErrorsBeforeAbort = CInt(txtMaxErrors.Text) Else `Use Default oBCP.MaximumErrorsBeforeAbort = 1 End If `First Row to start BCP If txtFirstRow <> "" Then If IsNumeric(txtFirstRow.Text) Then oBCP.FirstRow = CInt(txtFirstRow.Text) End If End If `Last Row to end BCP If txtLastRow <> "" Then If IsNumeric(txtLastRow.Text) Then oBCP.LastRow = CInt(txtLastRow.Text) End If End If `Set the output type for the BCP- based on combo box sTemp = cmbType.Text Select Case sTemp Case "(*.CSV) Comma Delimited" oBCP.DataFileType = SQLOLEDataFile_CommaDelimitedChar Case "(*.TAB) Tab Delimited" oBCP.DataFileType = SQLOLEDataFile_TabDelimitedChar Case "(*.DAT) Native" oBCP.DataFileType = SQLOLEDataFile_NativeFormat End Select `Step 3 - Export the Data ` `Here is the part you have been waiting for - `Pass the BCP object to the table ExportData method and `away it goes! ` iNumRows = BCPTable.ExportData(oBCP) `The output results are written to a file. `Create a file object to read the contents `of the file and display the output file results in the status text box. ` Set oOutputFile = New CFile oOutputFile.FileName = oBCP.LogFilePath oOutputFile.IOMode = "INPUT" oOutputFile.IOType = "SEQUENTIAL" oOutputFile.OpenFile If oOutputFile.Status = 1 Then oOutputFile.ReadAll sTempBuf txtStatus = sTempBuf Else txtStatus = "Error reading BCP output file." End If oOutputFile.CloseFile `Report the number of rows exported MsgBox Str(iNumRows) & " rows exported.", vbInformation, "Bulk Copy" `Cleanup and Exit Export_Exit: ` Set oBCP = Nothing Set BCPTable = Nothing Set oOutputFile = Nothing frmStatus.Visible = False StatusBar1.Panels("status").Text = "" SSPanel1.Enabled = True frmMain.MousePointer = vbDefault Exit Sub Export_Error: MsgBox Err.Description Resume Export_Exit End Sub NOTE: The BCP code provided does not import data, but you can modify the code to add this feature. The value of the BulkCopy object property ImportRowsPerBatch is set and is included in the export data code--even though it is not used during BCP export--in case you want to modify the code to import data. The log and error files created when performing BCP default to the DBA Assistant application directory (that is, the directory where the DBA Assistant is executing) with the filenames sams_bcp.log and sams_bcp.err. Summary For the non-Visual Basic DBAs in the crowd, I hope the explanations and code examples in this chapter were easy for you to follow and that they motivated you to learn Visual Basic. If you use SQL-DMO, you can easily create powerful DBA tools that can even be integrated in applications such as Microsoft Word and Excel. Study the various Visual Basic examples that ship with SQL Server and review the code on the CD-ROM that accompanies this book; soon, you will have the power and ability to write your own tools to simplify your job! DISCLAIMER To order books from QUE, call us at 800-716-0044 or 317-361-5400. For comments or technical support for our books and software, select Talk to Us. © 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company. |