SQL Server 2005: Server Management Objects (SMO)



Similar documents
SQL Server 2005: Report Builder

SQL Server 2005: SQL Query Tuning

SQL Server 2005: Introduction to SQL Server Management Studio

Moving the TRITON Reporting Databases

Hands-On Lab. Building a Data-Driven Master/Detail Business Form using Visual Studio Lab version: Last updated: 12/10/2010.

KEYWORDS InteractX, database, SQL Server, SQL Server Express, backup, maintenance.

Working with SQL Server Integration Services

How to Back Up and Restore an ACT! Database Answer ID 19211

QUANTIFY INSTALLATION GUIDE

Technical Bulletin. SQL Express Backup Utility

Implementing Microsoft SQL Server 2008 Exercise Guide. Database by Design

Installing LearningBay Enterprise Part 2

Moving the Web Security Log Database

If you have questions or need assistance, contact PCS Technical Services using the contact information on page 10.

How To Create A Replica In A Database On A Microsoft Powerbook (Ahem) On A Linux Server (A.K.A.A)

Configuring a Custom Load Evaluator Use the XenApp1 virtual machine, logged on as the XenApp\administrator user for this task.

How To Upgrade Your Microsoft SQL Server for Accounting CS Version

Juris Installation / Upgrade Guide

Before you may use any database in Limnor, you need to create a database connection for it. Select Project menu, select Databases:

MSSQL quick start guide

Team Foundation Server 2012 Installation Guide

How to Copy A SQL Database SQL Server Express (Making a History Company)

Automated backup. of the LumaSoft Gas database

Installing GFI MailArchiver

Perceptive Intelligent Capture. Product Migration Guide. with Supervised Learning. Version 5.5 SP3

SQL Server 2005 Integration Services

SonicWALL CDP 5.0 Microsoft Exchange InfoStore Backup and Restore

Creating a New Database and a Table Owner in SQL Server 2005 for exchange@pam

Linko Software Express Edition Typical Installation Guide

SPHOL207: Database Snapshots with SharePoint 2013

How to protect, restore and recover SQL 2005 and SQL 2008 Databases

Install SQL Server 2014 Express Edition

Walkthrough: Creating and Using an ASP.NET Web Service in Visual Web Developer

Sage Intelligence Financial Reporting for Sage ERP X3 Version 6.5 Installation Guide

InformationNOW SQL 2008 Database Backup and Restoration

Video Administration Backup and Restore Procedures

Exploring Manual and Automatic Database Backup Using Microsoft Azure Storage

Microsoft Dynamics GP Audit Trails

Monitoring SQL Server with Microsoft Operations Manager 2005

Preparing a SQL Server for EmpowerID installation

Microsoft SQL Server 2005 How to Create and Restore Database (GRANTH3) Manually

Installing GFI MailArchiver

How To Create An Easybelle History Database On A Microsoft Powerbook (Windows)

Sophos Reporting Interface Creating Reports using Crystal Reports 2008

SELF SERVICE RESET PASSWORD MANAGEMENT BACKUP GUIDE

Sitecore Ecommerce Enterprise Edition Installation Guide Installation guide for administrators and developers

SOFTWARE INSTALLATION INSTRUCTIONS CLIENT/SERVER EDITION AND WEB COMPONENT VERSION 10

Crystal Reports. For Visual Studio.NET. Reporting Off ADO.NET Datasets

Installing and Configuring Login PI

Tutorial. Patch Management

Creating IBM Cognos Controller Databases using Microsoft SQL Server

Visual Studio.NET Database Projects

Secret Server Installation Windows Server 2012

Migrating MSDE to Microsoft SQL 2008 R2 Express

Moving a CIMPLICITY SQL Database using Detach/Attach Proficy HMI/SCADA CIMPLICITY

Secret Server Installation Windows Server 2008 R2

SonicWALL CDP 5.0 Microsoft Exchange User Mailbox Backup and Restore

Secret Server Installation Windows 8 / 8.1 and Windows Server 2012 / R2

Getting Started with the Aloha Community Template for Salesforce Identity

Microsoft SQL Server 2005 How to Create and Restore Database (GRANTH3) Manually

Creating Reports with Microsoft Dynamics AX SQL Reporting Services

Table of Contents SQL Server Option

4cast Server Specification and Installation

Restoring Sage Data Sage 200

User Setup for SQL Security

Microsoft Corporation. Project Server 2010 Installation Guide

Backup & Restore. Backing Up and Restoring STX Databases For Non-OLS Subscribers

Windows SharePoint Services Installation Guide

Crystal Reports Payroll Exercise

Sync Appointments from the Schedule Certifications Screen

How to Set Up a Shared SQL Express Database with ManagePro 7 Standard version

Microsoft Project Server Integration with SharePoint 2010

SonicWALL CDP Local Archiving

Setup and configuration for Intelicode. SQL Server Express

InformationNOW SQL 2005 Database Backup and Restoration

GETTING STARTED WITH SQL SERVER

Hands-On Lab. Client Workflow. Lab version: Last updated: 2/23/2011

Knowledgebase Article

Hands-On Lab. Embracing Continuous Delivery with Release Management for Visual Studio Lab version: Last updated: 12/11/2013

How To Use Gfi Mailarchiver On A Pc Or Macbook With Gfi From A Windows 7.5 (Windows 7) On A Microsoft Mail Server On A Gfi Server On An Ipod Or Gfi.Org (

System Area Management Software Tool Tip: Integrating into NetIQ AppManager

Reconfiguring VMware vsphere Update Manager

Exploring Organizational Security and Auditing

How to Connect to Berkeley College Virtual Lab Using Windows

Exploring SQL Server Data Tools in Visual Studio 2013

SPHOL325: SharePoint Server 2013 Search Connectors and Using BCS

UNICORN 6.4. Administration and Technical Manual

E-Notebook SQL13.0 Desktop Migration and Upgrade Guide

Acronis Backup & Recovery 10 Advanced Server Virtual Edition. Quick Start Guide

There are several ways of creating a PDF file using PDFCreator.

TECHNICAL TRAINING LAB INSTRUCTIONS

HELP DOCUMENTATION E-SSOM BACKUP AND RESTORE GUIDE

Server Installation: ServerTools

Installing Cobra 4.7

Installation Manual Version 8.5 (w/sql Server 2005)

CRM Setup Factory Installer V 3.0 Developers Guide

A SharePoint Developer Introduction. Hands-On Lab. Lab Manual SPCHOL306 Using Silverlight with the Client Object Model VB

Transcription:

SQL Server 2005: Server Management Objects (SMO)

Table of Contents SQL Server 2005: Server Management Objects (SMO)...3 Lab Setup...4 Exercise 1 Creating Databases and Tables...5 Exercise 2 Backups and Verification...7 Exercise 3 Scripter...9

SQL Server 2005: Server Management Objects (SMO) Objectives NOTE: This lab focuses on the concepts in this module and as a result may not comply with Microsoft security recommendations. After completing this lab, you will be able to: Create Tables and Databases with Server Management Objects (SMO) Back up databases with SMO Script out database objects with SMO NOTE: The SQL Server 2005 labs are based on beta builds of the product. The intent of these labs is to provide you with a general feel of some of the planned features for the next release of SQL Server. As with all software development projects, the final version may differ from beta builds in both features and user interface. For the latest details on SQL Server 2005, please visit http://www.microsoft.com/sql/2005/. Scenario Prerequisites Estimated Time to Complete This Lab This lab will show you how to use SMO to execute various tasks on SQL Server 2005 such as creating a database, creating tables, and backing up a database. Prior working experience with SQL Server 7.0 or SQL Server 2000 Prior working experience with T-SQL Basic Microsoft Windows networking concepts A basic understanding of the.net Framework 30 Minutes Page 3 of 10

Lab Setup 1. Log in. 1. Log in using the Administrator user account. The password is Pass@word1. Page 4 of 10

Exercise 1 Creating Databases and Tables Scenario In this exercise, you will learn how to connect to the local SQL Server and how to create a database and associated tables using SMO objects. 1. Create a Windows Forms application. NOTE: All of the code for this exercise is contained in the Microsoft Visual Studio solution located at C:\SQL Labs\Lab Projects\SMO\SMO.sln. You can copy and paste from these files to complete any portion of the exercise. 2. Add a reference to the SMO Assembly. 3. Connect to the Local SQL Server. 1. From the Start All Programs menu, load Microsoft Visual Studio 2005. 2. Click File New Project. 3. In the New Project dialog box, create a new Microsoft Visual Basic project, using the Windows Application template. 4. For the project Name, enter SMO. For the project location, enter C:\SQL Labs\User Projects. Click OK to create the project. 1. Select Project Add Reference. 2. Click Microsoft.SqlServer.Smo in the list, and click OK. 3. Repeat step 1 and select the Microsoft.SqlServer.ConnectionInfo assembly. 1. Drag a Button control from the toolbox onto the form. 2. Select the new button control. 3. In the Properties window, set the (Name) property to btnconnect. 4. In the Properties window, set the Text property to Connect. 5. Double-click the new button to create an event handler for the Click event. 6. At the top of the code window, add the following code. Imports Microsoft.SqlServer.Management.Smo 7. Inside the btnconnect click event handler add the following code. Dim srv As New Server(Environment.MachineName) MsgBox("The AdventureWorks database was created at " & _ srv.databases("adventureworks").createdate.tostring()) The first line of code creates a server object that references the local server by passing the name of the local machine to its constructor. Next, the code accesses the Databases collection of the Server and displays the creation date of the AdventureWorks database in a message box. 8. Press F5 to run the Windows Forms application. 9. Click Connect. 10. Click OK in the message box. Page 5 of 10

4. Create Databases and Tables with SMO objects. 11. Close the window when you re done. 1. Switch back to the design view of the form by right-clicking Form1.vb in the Solution Explorer and clicking View Designer. 2. Drag a second Button control from the toolbox onto the form. 3. Select the new button control. 4. In the Properties window, set the (Name) property to btndbtblcreation. 5. In the Properties window, set the Text property to DB-Table. 6. Double-click the new DB-Table button to create an event handler for the button s Click event. 7. Inside the btndbtblcreation click event handler add the following code. Dim srv As New Server(Environment.MachineName) Dim smodb As New Database(srv, "SMO_DB") smodb.create() Dim smotbl As New Table(smoDB, "SMO_TBL") Dim smocol1 As New Column(smoTBL, "c1", DataType.Int) smotbl.columns.add(smocol1) Dim smocol2 As New Column(smoTBL, "c2", _ DataType.NVarChar(30)) smotbl.columns.add(smocol2) smotbl.create() MsgBox("Database and Table Created!") The first line of code is similar to the code used before to establish a connection to the local server. Next, the code creates a new database object by calling the AddNew() method of the Server object s Databases collection. To create a new database on the server, the code then calls the new Database object s Create method. Next, the code follows a similar pattern to add a new table named "SMO_TBL" and two columns with differing data types. After that, a check constraint is created and bound to the first column. The last line of code creates the table and associated columns on the server by calling the Table object s Create method. 8. Press F5 to run the Windows Forms application. 9. Click the DB-Table button. 10. Close the form when you re done. 5. Verify the creation of the SMO_DB database. 1. From the Windows Start menu, load SQL Server Management Studio. 2. When asked to connect, make sure the server name is localhost, and that Windows Authentication is selected in the Authentication dropdown. Click Connect. 3. In the Object Explorer window, expand the Databases node, and find the new SMO_D B database. Expand the Tables node to view the new table, and drill down into the columns to view the new columns you created. Page 6 of 10

Exercise 2 Backups and Verification Scenario In this exercise, you will learn how to back up a SQL Server database using SMO and verify the resulting backup files. 1. Add a new button. NOTE: All of the code for this exercise is contained in the Visual Studio solution located at C:\SQL Labs\Lab Projects\SMO\SMO.sln. You can copy and paste from these files to complete any portion of the exercise. 1. Switch back to the design view of the form by right-clicking Form1.vb in the Solution Explorer and clicking View Designer. 2. Drag a third button from the toolbox onto the form. 3. Select the new button. 4. In the Properties window, set the (Name) property to btnbackup. 5. In the Properties window, set the Text property to Backup. 6. Double-click the new Backup button to create an event handler for the button click event. 7. Inside the btnbackup click event handler add the following code. Dim srv As New Server(Environment.MachineName) Dim backdeviceitem As New BackupDeviceItem( _ "C:\SQL Labs\User Projects\SMO_BACKUP.bak", _ DeviceType.File) Dim back As New Backup() back.database = "AdventureWorks" back.devices.add(backdeviceitem) back.action = BackupActionType.Database back.sqlbackup(srv) MsgBox("Backup Complete!") The first line of code establishes a connection to the local server. Next, the code creates a backup object. The backup object encapsulates the functionality that allows you to perform database backups of SQL Server 2005 databases. Next, the code sets several properties on the database object: the name of the database to be backed up, the type of backup device to create, and the location of the backup device. The code also specifies the type of database backup action to perform. In this example, the code backs up only the database. Another option would be to perform a transaction log backup only. Finally, the code performs the backup by calling the SqlBackup method of the backup object and passing in the server object. 8. Below the code you just added, add the following code, which verifies the backup. Dim rest As New Restore Dim restdeviceitem As New BackupDeviceItem( _ "C:\SQL Labs\User Projects\SMO_BACKUP.bak", _ DeviceType.File) rest.devices.add(restdeviceitem) Page 7 of 10

Dim verified As Boolean = rest.sqlverify(srv) If verified Then MsgBox("The backup was verified succesfully") Else MsgBox("The backup is corrupt") End If To verify the backup, the code creates a Restore object. Next, the code specifies the type of backup device, and adds its location to the Restore object s Devices collection. The code then calls the Restore object s SqlVerify method, passing in the Server object. The return value from this method call, which is of type Boolean, lets the caller know whether the backup is valid or not. This is similar to executing the T-SQL statement RESTORE VERIFYONLY. 9. Press F5 to run the application. The following step takes several minutes, so you may want to skip to the next exercise if you are short on time. 10. Click the Backup button and wait for the backup to complete. This process may take a few minutes. 11. When a message box appears, click OK. 12. Close the application (but not the solution) when you re done. Page 8 of 10

Exercise 3 Scripter Scenario In this exercise, you will learn how to script out a set of database objects using the Scripter object. 1. Add a reference to the SmoEnum Assembly. NOTE: All of the code for this exercise is contained in the Visual Studio solution located at C:\SQL Labs\Lab Projects\SMO\SMO.sln. You can copy and paste from these files to complete any portion of the exercise. 1. In the Solution Explorer, right-click the SMO project and click Add Reference 2. Click Microsoft.SQLServer.SmoEnum. 3. Click OK to add the reference to the project. 2. Add a new button. 1. Switch back to the design view of the form by right-clicking Form1.vb in the Solution Explorer and clicking View Designer. 2. Drag a fourth button from the toolbox onto the form. 3. Select the new button. 4. In the Properties window, set the (Name) property to btnscripter. 5. In the Properties window, set the Text property to Scripter. 6. Double-click the new Scripter button to create an event handler for the button click event. 7. Inside the btnscripter click event handler add the following code. Dim srv As New Server(Environment.MachineName) Dim scr As New Scripter(srv) scr.options.filename = _ "C:\SQL Labs\User Projects\scripter.txt" scr.options.appendtofile = True scr.options.includeheaders = True Dim db As Database = srv.databases("adventureworks") Dim smoobjects(db.tables.count - 1) As SqlSmoObject Dim i As Integer = 0 For Each tbl As Table In db.tables If Not tbl.issystemobject Then If tbl.schema = "Sales" Then smoobjects(i) = tbl i += 1 End If End If Next ' Resize the array to the correct size. Page 9 of 10

ReDim Preserve smoobjects(i - 1) scr.script(smoobjects) MsgBox("Scripting complete!") The first line of code establishes a connection to the local server. Next, the code creates a Scripter object, passing in the Server object. The code specifies a value for Options.FileName to indicate where the script should be saved, as well as specifying that text should be appended to the file and that descriptive headers should be included in the script. Then, the code uses SMO to retrieve an array containing information about all the tables whose schema is "Sales". The code passes this array to the Script method of the Scripter object. This results in the objects being scripted out to the specified file. 8. Run the sample project, and click the Scripter button to script the Sales tables to a text file. This process may take a few moments. 9. Open Windows Explorer. 10. Navigate to the C:\SQL Labs\User Projects folder on the local computer. 11. Double-click scripter.txt. 12. Review the DDL scripts for the tables. 13. Close the application when you re done. Page 10 of 10