MS SQL Server
  Home arrow MS SQL Server arrow Page 8 - Source Code Management and Database Deploy...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MS SQL SERVER

Source Code Management and Database Deployment
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2005-06-16

    Table of Contents:
  • Source Code Management and Database Deployment
  • Administering the Visual SourceSafe Database
  • Managing Create Scripts in Visual Studio .NET
  • Visual SourceSafe Explorer
  • Labels and Versions
  • Database Deployment
  • Deployment of Individual Objects
  • Scripting Data in Visual Studio .NET
  • Deploying Create Scripts in Visual Studio .NET

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Source Code Management and Database Deployment - Scripting Data in Visual Studio .NET


    (Page 8 of 9 )

    Alternatively, you can use Visual Studio .NET to script data and add it to Visual SourceSafe:

    1. Open Server Explorer, navigate through the nodes, and expand the Tables node in the Asset database.

    2. Select the tables with seed data (such as AcquisitionType, EqType, OrderStatus, and OrderType).

    3. Right-click the selection and select Export Data from the menu.

    4. The program prompts you for Locations For Exported Data File and to confirm that you want to export the selected data. The default location will be the folder that contains the Create scripts you generated earlier.

    5. When you confirm the export operation, the program generates a set of DAT files. You typically need to select the files in Solution Explorer and Check (them) In.

    These files are not SQL Server scripts but simple binary files (see the content of a file in Figure 11-8).


    Figure 11-8.  Content of DAT file

    Deploying Scripts: Traditional Approach

    The deployment scripts can then be executed manually one by one in Query Analyzer, but I have created a stored procedure that allows me to automate execution of a set of scripts—prBatchExec.

    To prepare for deployment, I need to create a list of scripts and save it in a text file. The procedure executes the scripts in the order in which they are listed in the text file:

    -- list of deployment scripts for Asset database
    Asset - database.DBS
    Asset - UDT.sql
    Asset - Table.sql
    Asset - DRI.sql
    Asset - Functions.sql
    Asset - sp.sql
    Asset - Views.sql

    Although Deferred Name Resolution allows you to ignore the order of creation of stored procedures, there are still some dependencies that must be followed. For example, indexes must be created after tables, tables after user-defined data types, and all of them after the database has been initiated. With this in mind, one of the main advantages of prBatchExec is that it preserves the order of execution of files. No human intervention is required and the opportunity for error is reduced.

    The procedure uses SQL Distributed Management Objects (SQL-DMO) to execute individual scripts against the database server. SQL-DMO is a set of COM objects that encapsulate the functionality needed for administering SQL Server. To use SQL-DMO from SQL Server, you have to use the system stored procedures for OLE Automation (COM), described in Chapter 10:

    create proc prBatchExec
    -- Execute all sql files in the specified folder using the alphabetical order.
    -- Demonstration of use of OLE Automation.
         
    @ServerName sysname = '(local)\ss2k2',
          @UserId sysname = 'sa',
          @PWD sysname = 'my,password',
          @DirName varchar(400)='C:\dbScripter\test',
          @File varchar(400) = 'list.txt',
          @UseTransaction int = 0
    as
    set nocount on
    declare @FileSystemObject int,
            @objSQL int,
            @hr int,
            @property varchar(255),
            @return varchar(255),
            @TextStream int,
            @BatchText varchar(8000),
            @FilePath varchar(500),
            @ScriptId varchar(200),
            @Cmd varchar(1000)
    --- Get list of files
    create table #FileList (ScriptId int identity(1,1), 
                            FileName varchar(500))
    select @Cmd = 'cd ' + @DirName + ' & type ' + @File
    insert #FileList (FileName)
    exec master..xp_cmdshell @Cmd
    -- remove empty rows and comments
    delete #FileList where FileName is null
    delete #FileList where FileName like '--%'
    -- prepare COM to connect to SQL Server
    EXEC @hr = sp_OACreate 'SQLDMO.SQLServer', @objSQL OUTPUT IF @hr < 0
    BEGIN
      print 'error create SQLDMO.SQLServer'
      exec sp_displayoaerrorinfo @objSQL, @hr
      RETURN
    END
    EXEC @hr = sp_OAMethod @objSQL, 'Connect', NULL, @ServerName, @UserId, @PWD
    IF @hr < 0
    BEGIN
     
    print 'error Connecting'
      exec sp_displayoaerrorinfo @objSQL, @hr
      RETURN
    END
    EXEC @hr = sp_OAMethod @objSQL, 'VerifyConnection', @return OUTPUT
    IF @hr < 0
    BEGIN
     
    print 'error verifying connection'
      exec sp_displayoaerrorinfo @objSQL, @hr
      RETURN
    END
    -- prepare file system object
    EXEC @hr = sp_OACreate 'Scripting.FileSystemObject', @FileSystemObject OUTPUT
    IF @hr < 0
    BEGIN
     
    print 'error create FileSystemObject'
      exec sp_displayoaerrorinfo @FileSystemObject, @hr
      RETURN
    END
    -- begin transaction
    if @UseTransaction <> 0
    BEGIN
     
    EXEC @hr = sp_OAMethod @objSQL, 'BeginTransaction '
      IF @hr < 0
      BEGIN
       
    print 'error BeginTransaction'
        exec sp_displayoaerrorinfo @objSQL, @hr
        RETURN
      END
    END
    -- iterate through the temp table to get actual file names select @ScriptId = Min (ScriptId) from #FileList
    WHILE @ScriptId is not null
    BEGIN
      select @FilePath = @DirName + '\' + FileName
      from #FileList where ScriptId = @ScriptId
       
    if @FilePath <> ''
       BEGIN
        print 'Executing ' + @FilePath
        
    EXEC @hr = sp_OAMethod @FileSystemObject, 'OpenTextFile',
                              @TextStream output, @FilePath
        IF @hr < 0
        BEGIN
          print 'Error opening TextFile ' + @FilePath
          exec sp_displayoaerrorinfo @FileSystemObject, @hr  
          RETURN
       
    END
       
    EXEC @hr = sp_OAMethod @TextStream, 'ReadAll', @BatchText output
        IF @hr < 0
        BEGIN
           
    print 'Error using ReadAll method.'
            exec sp_displayoaerrorinfo @TextStream, @hr
          RETURN
        END
       
    -- print @BatchText
        -- run it.
        EXEC @hr = sp_OAMethod @objSQL, 'ExecuteImmediate', Null , @BatchText
        IF @hr <> 0
        BEGIN
         
    if @UseTransaction <> 0
          BEGIN
              EXEC @hr = sp_OAMethod @objSQL, 'RollbackTransaction '
            IF @hr < 0
             
    BEGIN
                print 'error RollbackTransaction'
                exec sp_displayoaerrorinfo @objSQL, @hr
             
    RETURN
            
    END
          END
          print 'Error ExecuteImmediate.' --Transaction will be rolled back.'
          exec sp_displayoaerrorinfo @objSQL, @hr
          RETURN
         
    END
          EXECUTE sp_OADestroy @TextStream
        END
       
    print 'Finished executing ' + @FilePath
       
    select @ScriptId = Min(ScriptId) from #FileList where ScriptId > @ScriptId
    end
    print 'Finished executing all files.'
    drop table #FileList
    EXECUTE sp_OADestroy @FileSystemObject
    if @UseTransaction <> 0
    BEGIN
     
    EXEC @hr = sp_OAMethod @objSQL, 'CommitTransaction '
     
    IF @hr < 0
     
    BEGIN
       
    print 'error CommitTransaction'
       
    exec sp_displayoaerrorinfo @objSQL, @hr
       
    RETURN
     
    END
    END
    RETURN 0

    Before you can use the prBatchExec stored procedure, you must locate in SQL Server Books Online sp_displayoaerrorinfo and sp_hexadecimal and store them in the same database with prBatchExec (in other words, the Asset database).

    To execute the prBatchExec procedure, you need to specify values for the parameters for the SQL Server instance, login, password, folder that contains your deployment scripts, and the name of the file containing the list of deployment scripts. You also need to decide whether deployment is to be performed as a transaction. Transactions cannot be used for initial deployment because database creation cannot be performed by a transaction. However, using transactions is very useful for incremental builds.

    The prBatchExec procedure has one limitation. It can process only short (up to 8000 characters) scripts. I have decided to include it in this the book for two reasons. First, 8000 characters is probably enough for running an incremental build. Second, it’s educational—it demonstrates use of COM objects from Transact-SQL. For full builds, you have to use an updated version—prBatchExec3.

    You can also download BatchExec.exe program from www.Trigonblue.com/sqlxml/sqlxml_download.htm. This is a console C# application and you can run it on computers that have the .NET Framework installed using

    BatchExec (local)\ss2k2 sa my,password c:\script\test list.txt

    More MS SQL Server Articles
    More By McGraw-Hill/Osborne


     

    Buy this book now. This article is excerpted from SQL Server 2000 Stored Procedure & XML Programming, second edition, written by Dejan Sunderic (McGraw-Hill/Osborne, 2004; ISBN: 0072228962). Check it out at your favorite bookstore today. Buy this book now.

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway