1. Trang chủ
  2. » Công Nghệ Thông Tin

sams teach Yourself windows Script Host in 21 Days phần 6 ppsx

51 285 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 51
Dung lượng 2,17 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

If you expand a database, you’ll see two folders: Group/Users contains all the valid groups and users for the database, and Objects contains all the components of the database, such as t

Trang 1

This script differs from the previous script because it takes multiple

command-line arguments A For Each loop iterates through the

arguments and creates directories accordingly.

If you run the script using the command line makfldr H:\ROOT

H:\ROOT\BIN H:\ROOT\SRC H:\ROOT\HELP H:\ROOT\HELP\ADMIN,

you’ll see the output illustrated in Figure 11.13, and if you go back to the

Windows Explorer, you’ll see something that looks like Figure 11.14.





Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 2

Deleting a folder is just as simple as the preceding examples Listing 11.15

uses the FileSystemObject.DeleteFolder() method instead of

Trang 3

 On Error Resume Next 

 The flow of delfldr.vbs is identical to the makfldr.vbs script The only

differences are the messages and the call to DeleteFolder().

Trang 4

WSH objects to perform standard administrative tasks You’ve looked at examples that show how you can perform the following functions:

 AThe WshShell object provides a SpecialFolders property that allows you to

determine the path to special Windows folders such as the desktop. 

Trang 5

 Day 12: Automating Microsoft SQL Server

Interestingly enough for you, the management interface uses ActiveX objects exposed

by SQL Server to perform its functions You can use the same ActiveX objects to

perform your own administrative tasks programmatically In this chapter, you'll see how you can use the ActiveX objects with WSH scripts to perform SQL Server

The concepts within this chapter are directly applicable to Microsoft SQL

Server 7.0 The administration interface is different because with SQL Server 7.0, Microsoft incorporated SQL Server administration into the standard

Microsoft Management Console (MMC) interface However, the underlying

objects are very similar.









First, you're going to take a look at some very typical administrative tasks for SQL

Server You'll run through the steps that you would perform using the standard

management interface You're going to focus on the tasks of creating and deleting

devices, databases, and tables.

Finally, you'll look at detailed script examples that will show you how you can use the

administration objects in practice In the examples, you'll find functions that you can cut and paste into your own scripts if you choose.









By the end of this chapter, you should have a clear idea of how administrative tasks can

be performed with SQL Server and WSH scripts You'll cover the basics in this chapter; using it as a foundation, you'll be able to create your own custom scripts. 





Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 6

 Standard SQL Server Administration 

Databases store all the information contained within SQL Server

Each database contains one or more tables, and each table consists of multiple rows and columns If you expand the databases folder, you’ll see a list of the databases on the server If you expand a database, you’ll see two folders: Group/Users contains all the valid groups and users for the database, and Objects contains all the components of the database, such as tables, views, and stored procedures.

Trang 7

To perform an administrative task, you can right-click an object or folder and pick a

selection from the context menu that appears For example, if I right-click on the

Databases folder, as depicted in Figure 12.2, a menu will appear that will give me the option to create a new database If I right-click on a specific database in the databases folder, the context menu gives me the capability to delete the database or edit it Each object in the Enterprise Manager provides an appropriate context menu that gives you quick access to management functions.

There’s yet another way to perform administrative tasks in SQL Server; you can use

SQL commands in the SQL Query Tool To display the SQL query window, you select Tools, SQL Query Tool from the menu You can see the SQL query window illustrated in Figure 12.4; in the illustration, you can see a SQL query that will create a new database device.





Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 8

With the exception of the SQL query tool, all the other tools in the SQL Enterprise

Manager use the SQL Distributed Management Objects (DMO) provided by SQL

Server The Distributed Management Objects provide a high-level, object-oriented

interface for SQL Server administration Fortunately for you, you can access the SQL DMO objects in your own programs to perform administrative tasks In the next section, I’ll discuss the DMO objects and their capabilities.

Trang 9

The Table object represents a table in a database Table objects

contain columns that represent the fields in a table. 

Now, you’re going to see how you can use WSH to script the management

objects provided by SQL Server First, you’ll see how you can connect to

SQL Server to perform changes, and then you’ll work with scripts that affect

database devices Database devices are the physical files that contain

databases and their tables; you’ll see how you can create and delete them.









Next, you’ll look at creating and deleting databases Databases contain one

or more tables that store your information Finally, you’ll see how you can

use WSH to create, modify, and delete database tables. 







If you have SQL Server version 6.5 installed, you can follow along and

execute the scripts as you go If you have a different version, you might have

problems because the management objects might be different. 

If you are working with the SQL Server Distributed Management Objects,

you need to begin by connecting to a SQL Server You do that by creating an

instance of the SQLOLE.SQLServer object Here’s a code snippet that

shows how it’s done:

Trang 10

including devices, databases, tables, and columns When you have the

object instance, you connect to the SQL Server using the

SQLServer.Connect() method, which takes the server name, user login

ID, and user password as parameters Most of your scripts that use the SQL

Server objects will begin with similar code so that you can connect to SQL

Devices contain databases on SQL Server Before you can create a

database, you must have one or more devices to contain it A typical

configuration is to put the data portion of a database on one device and the

transaction log for the database on a separate device Doing so provides a

significant performance enhancement In either case, each device

corresponds to a single file in the NT file system.









To create devices, you use the Device object To create the object, you use

Wscript.CreateObject("SQLOLE.Device") After you’ve created the

Device object, you can change its settings by using its properties When

you’ve finished configuring it, you tell SQL Server to create it by adding it to

the SQLServer.Devices collection The code example in Listing 12.1

shows the details.

Trang 12

The code example begins with constants that are specific to my

environment Before you try to run the scripts in this chapter, you

must change the constants to suit your environment. 







The example contains some constants that set up the name for SQL Server,

the login ID and password that will be used to connect to the server, and the

path for the physical file that will be used to store the new device that I

create This is followed by a bunch of constants for SQL Server devices I

pulled the constants from the type library that is associated with the SQL

Server objects using the OLE View tool that comes with Visual Studio.









The OLE View tool is very useful for finding out information about the objects

on your system When you run it, it shows you a list of categories of

information you can view organized as file folders like the Windows Explorer

If you scroll down and expand the category named Type Libraries, you’ll see

a list of the type libraries on your system, as shown in Figure 12.6.

If you scroll down through the list of type libraries, you should find Microsoft

SQLOLE Object Library, as illustrated in Figure 12.7 If you double-click on

the library entry, the full Microsoft SQLOLE Object Library will come up in a

type library viewer, which is shown in Figure 12.8 On the left side of the

screen, you have categorized segments of the type information On the right

side of the screen, you see the full listing for the type library.

Trang 13

To get the constants for the SQL Server device types, I move down in the

categorized type library on the left side of the screen until I find the entry

labeled typedef enum SQLOLE_DEVICE_TYPE If I click on the entry, I

see the constants for the device types in the right side of the screen as

Trang 14

Unfortunately, I can’t use the constants verbatim from the type library listing

First I have to reformat them into valid VBScript code I do that manually

using a standard text editor—in my case, Notepad Figure 12.10 the shows

the reformatted code in Notepad.

That's a very handy technique that you can use to pull constants for your

VBScript scripts I used it throughout this chapter to get listings of constants

that I use within the sample scripts. 







Getting back to the script again, after the constants I create a SQLServer

object that I use to connect to my database server Next, I create a Device

object I configure the object by setting its Name, PhysicalLocation,

Size, and Type properties Finally, I add the Device object to the

SQLServer.Devices collection using the SQLServer.Devices.Add()

method That's all there is to it! You'll notice the same coding pattern

throughout the code examples.







 If you run the script using cscript makedev.vbs from the command line 

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 15

device named NEWDEV has been created as illustrated in Figure 12.11.

For your next example, you’re going to see the VBScript code that is required

to create a new database using the SQL Server objects You’ll notice that the

flow of the code is very similar to the previous example Take a look at the

Listing 12.2, which shows the code required to create a database.

Trang 16

’ Finally, we add the new database object to the SQL

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 17

The first part of the code listing essentially calls a bunch of helper functions

that you’ve created that make it easier to work with databases You can

reuse these functions within the code that you write Those functions are

The next function, CreateDevice(), is particularly long However, you’ll

notice that much of the code consists of error handling logic, so it really isn’t

Trang 18

The example begins with the standard SQL Server constants, the creation of

an instance of the SQLServer object, the creation of two devices using a

CreateDevice() subroutine, and the creation of a Database object After

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 19

the Database object is created, it is configured by setting some of its

properties and calling some of its methods If you would like more

information about the properties and methods of the object, please refer to

the SQL Server books online Finally, the Database object is added to the

SQLServer.Databases collection using the

SQLServer.Databases.Add() method Again, you notice a pattern that is

similar to what you saw when you created a new device.







The example is actually more complex than what is necessary to simply

create a database because it also creates two new devices One device will

contain the data for the database; the other device will contain the database

transaction log You’ll notice that the code to create the devices has been

packaged in a convenient CreateDevice() subroutine If you want, you

can reuse that subroutine and other functions and subroutines for your own

Trang 20

 ’ Next, we remove the database from the SQL Server databases 

’ PhysicalLocation property we’ll use it later to

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 21

The aforementioned code example shows another pattern that is very

common when using the SQL Server objects To delete something, you

typically remove it from a collection For example, close to the beginning of

the script, you see a call to sqlsrv.Databases.Remove "NEWDB" That

call removes the NEWDB database from the SQLServer.Databases

collection, which causes it to be deleted.









Next, the script uses a DeleteDevice() subroutine to delete the devices

that were associated with the NEWDB database I’ve created

DeleteDevice() because deleting a device is a little more complicated

than deleting other types of objects in SQL Server For most objects,

removing them from a collection is sufficient to delete them However, when

you remove a device from the Devices collection, the actual physical file for

the device is not deleted Consequently, my DeleteDevice() subroutine is

intelligent enough to find the physical file for the device and delete it using

the standard WSH File systemObject.

 The next step that you need to create a fully functional database is to create

a database table Listing 12.4 shows how you can create a database table. 

If you want to run this example, it assumes that you have created a

database named NEWDB using the aforementioned makedb.vbs

Trang 23

The example connects to SQL Server and then accesses a specific

database using the SQLServer.Databases collection Accessing a

database is easy because you can index into the SQLServer.Databases

collection using the name of the database Next, I create a new Table

object using Wscript.CreateObject("SQLOLE.Table") The Table

object contains Column objects that represent the fields in the table To add

the columns, I need to create individual Column objects, change the settings

for each Column, and then add the Column to the Table object using the

Table.InsertColumn() method After I’ve finished adding columns to the

table, I create the table in the database by adding it to the

Database.Tables collection.







 If you run the sample script, you will see a new the Products table created in

NEWDB database, as illustrated in Figure 12.12. 

What if you want to add a new column to the Products table? Doing so is

very easy All that you need to do is retrieve the Table object for the

Products table and then use its methods to make changes In Listing 12.5,

you add a ProductURL column to the Products table.

Trang 25

To change the table, you connect to SQL Server, retrieve the database from

the SQLServer.Databases collection, and then retrieve the Table object

from the Database.Tables collection Next, you create a new Column

object, set the name of the column to ProductURL, and set the

characteristics of the column—including the fact that it should allow nulls,

should be a variable-length character field, and should have a maximum

Before you can add the column to the table, you call the

Table.BeginAlter() method Table.BeginAlter() prepares the table

for a batch of changes Then, you insert the new column into the table using

the Table.InsertColumn() method, and finally, you tell the table to go

ahead and make the changes using the Table.DoAlter() method.









Deleting a table is very easy You might guess—correctly—that deleting a

table probably involves the Database.Tables collection and the

Remove() method Listing 12.6 shows how you can delete the Products

table that you created using a previous script.

Ngày đăng: 13/08/2014, 08:21

TỪ KHÓA LIÊN QUAN