This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.
|
Automatically Backing Up Your Files in Visual SourceSafe
By Johnny Papa |
Developing a site without backing up your work is like skydiving without a parachute. With Visual SourceSafe and task scheduling software, you can automate your code check-in process even when you're not around. |
Every office has a horror story about a developer who lost several days worth of work because their workstation crashed. But this harrowing experience won't happen to you, right? You back up your development work every day using a tool like Visual SourceSafe™. (If you don't, then you definitely want to look into it.) But even if you do employ a source code library tool, you still have to manually check in or out the files you are developing. Wouldn't it be nice if every night all of your source code were checked in and back out of Visual SourceSafe for you? Then you wouldn't have to worry about remembering to do it yourself each night before you go home.
You can use the technique discussed here to automatically check source code in and back out every night of the week, refreshing the current project image in the version control system. My code can be morphed into many variations to check files in, out, or both in and out, and even choose which files get checked in and out. I will leave the customization to your imagination, as the possibilities are numerous. I'll show you how to automatically schedule a Visual Basic®-based program that generates object creation scripts for a SQL Server™ database and then checks them in and out of Visual SourceSafe automatically. I'll be using a few neat technologies here such as the SQL Distributed Management Objects (SQL-DMO) in SQL Server, the Visual SourceSafe object library, and the Windows NT® Schedule service's AT command. First, let's discuss what this technique does for you, then I'll break it down into three main components:
Visual SourceSafe Saves the Day
Diving In
Scripting the Database with SQL-DMO
|
Figure 1: SQLOLE Object Library |
Keep in mind that the SQL-DMO object library is actually called SQLOLE in the references list. These are just different names for the same object library. After you select the SQL-DMO object library, click the OK button. Now that you have linked to the SQL-DMO object library, set up some module-level constants and variables to accomplish the scripting goal. |
|
|
|
This module-level code is pretty well documented, but let's go through it briefly anyway. Remember that you are going to manage SQL Server from Visual Basic. To do that, you need to know the name of the SQL Server (cSQLServer), the name of the database you want to script (cDatabaseName), and the user ID (cSQLServerUserID) and password (cSQLServerPassword) of a valid user. That gives you enough information to know which SQL Server you want to manipulate, but you also need to know where to put the script files (cScriptingPath). Nope, I didn't forget the variable mobjDatabase, which will be used to represent the pubs database throughout the code. Armed with this information, you are ready to proceed with the main scripting code.
Connecting to SQL Server
|
|
Later, you can insert more code to check files in and out of Visual SourceSafe, too.
Next, code the ScriptAllObjects subroutine as shown in Figure 2. First, declare and then create a new instance of the SQLOLE.SQLServer object so you can interface with the SQL-DMO object library. And since you cannot manage SQL Server if its service isn't started, check to see if it is. You can do this by evaluating the Status method of the SQLOLE.SQLServer object. If it is not running, issue the Start method and pass the name of the SQL Server and a valid user name and password combination to it. This starts the SQL Server service and then connects to it. If the service was already running, then issue the Connect method to connect to the SQL Server instance. Now that you are connected to the machine running SQL Server, set your module-level variable (mobjDatabase) to your project's database (I'm using the pubs database as an example). Create the path where you will store the database scripting files by calling the CreatePath subroutine (which I'll cover later). Once the path is created, script the database objects by calling the various scripting routines. Finally, wrap up the scripting code by closing the SQL Server connection and destroying the object reference. Let's go over some of the subroutines I skipped, starting with CreatePath. |
|
This routine's sole purpose is to create the path where you will store your database scripting files if it doesn't already exist. Here, I use the On Error Resume Next statement to ignore the error if the path already exists.
The Dirty Work
Connecting to Visual SourceSafe
|
|
Before getting into the code for the CheckProjectInAndOut subroutine, there are two steps you must take:
Now go back to the general declarations section of the code and add the module-level constants. |
Figure 5: SourceSafe Type Library |
|
First, declare the Visual SourceSafe root project (cVSSRootProject), which is always defined as $/. Then create a constant (cVSSSubProject) to represent the project where you will store the database scripts. Create a constant (cSourceSafeINI) to point to the Visual SourceSafe .ini file. This is important because the object library uses it to know where the Visual SourceSafe database resides. On your PC it is most likely found in C:\Program Files\Microsoft Visual Studio\
Common\Vss. Finally, define the user name (cVSSUserName) and password (cVSSPassword) that you will use to connect to the Visual SourceSafe database.
Checking Code In and Out
Schedule Your Scripts
|
Figure 7: Automatic Scheduling |
Now go to a command prompt and schedule the DatabaseScripting.exe. Let's make it run every night at 11:00 pm. Simply enter the following command into your command window, making sure you substitute your computer's name and the correct path to your executable file where appropriate: |
|
This line of code schedules DatabaseScripting.exe to run on the PC named YourComputerName every weeknight at 11:00 pm. To check your schedule, run the AT command by itself. Your results should look similar to Figure 8. |
Figure 8: Running the Backup Automatically |
Where to Go from Here
|
http://msdn.microsoft.com/library/devprods/vs6/ssafe/ssusexp/ssugegetting_started.htm and http://msdn.microsoft.com/library/psdk/sql/bkprst.htm |
From the September 1999 issue of Microsoft Internet Developer.
|