Click here to Skip to main content
6,304,948 members and growing! (18,273 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate

SQL Server objects in SourceSafe using SMO and Visual SourceSafe automation

By Levent Soyalp

Useful tool to script database objects using SMO and chek in Visual Source Safe
C#, Windows, .NET, Visual Studio, DBA, Dev
Posted:3 Apr 2006
Views:29,352
Bookmarked:31 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
6 votes for this article.
Popularity: 3.11 Rating: 4.00 out of 5
1 vote, 16.7%
1
1 vote, 16.7%
2

3
2 votes, 33.3%
4
2 votes, 33.3%
5

Introduction

It is important to backup database for reliability and continuity of database administration. In addition to

that, change management of schemas of database should not be ignorable event in administration tasks.

In generally, there are two types of database, these are production and development. Both of them can

be controlled changing schemas of structure. Change management is basic foundation for security

Administration. Finally, To do work, I used SQL Server 2005 and Visual Source Safe. I also use SMO

for scripting database. I found some work on DMO and Visual Source Safe. However, No work is

made for VSS using SMO. I wrote some extra codes for adaptation of new features.

http://www.codeproject.com/cs/database/DBScriptSafe.asp is be of help to do my study.

 

 

Demo Application

  Sample screenshot

 Requirements:

  • SQL Server 2005

  • Visual Source Safe

  • .Net 2.0

 

 

On startup,

  • Left panel contains SQL connection parameters.
  • Using NT Integrated security or sql user to connect database.
  • To get server list in your network press �getserver� button.
  • After writing the name database server, press �connect� button to connect SQL Server
  • After successfully connection, right panel would be active with list of database.
  • After filling VSS parameters and select database that is being scripted and sent to VSS.
  • Finally, press �Script and checkin selected databases� button to check in source to VSS.

Using the Code

SMO and Visual Source Safe automation library are added to references of the project before using these objects.

Add below dll to references of the project.

  • Microsoft.SqlServer.ConnectionInfo;
  • Microsoft.SqlServer.Smo;
  • Microsoft.SqlServer.SmoEnum;
  • Microsoft.SqlServer.SqlEnum;
  • Microsoft.VisualStudio.SourceSafe.Interop;

and deletion temp folder add

  • System.Management

After script button,

 

if (m_dbList.Count > 0)
            {
                m_scriptEngine.DatabaseName = (string)m_dbList[0];
                m_dbList.RemoveAt(0);
                backgroundWorker1.RunWorkerAsync();
                System.Windows.Forms.Application.DoEvents();
            }

 

 

Program runs the backgroundworker to call script

m_scriptEngine.Script();

backgroundworker component is used to script table. Because scripting table is slow process.

In ScriptEngine Class,

 

ReadandWriteObjectsFromDatabasetoFile(db, workingFolder);

To script file,

 

foreach (Rule rule in db.Rules)
            {
                filename = rule.Name;
                oname.Text = "_Progressing....:" + filename;
                FileStream file = new FileStream(workingFolder + filename + "_rules.sql", FileMode.CreateNew, FileAccess.Write);
                StreamWriter sw = new StreamWriter(file);
                StringCollection sc = rule.Script();
                foreach (string s in sc)
                    sw.WriteLine(s);
                sw.Close();
                file.Close();
            }

calls to every object of database to script temp folder. After locating script to temp folder,
every files are sent to the spesified VSS.


 

VssDbItem.Checkout("DBScriptManager Automatic Checkout",
            workingFolder,
            (int)(VSSFlags.VSSFLAG_GETNO | VSSFlags.VSSFLAG_RECURSYES));

    VssDbItem.Checkin("DBScriptManager Automatic Checkin",
            workingFolder,

            (int)(VSSFlags.VSSFLAG_DELTAYES| VSSFlags.VSSFLAG_RECURSYES|
                    VSSFlags.VSSFLAG_DELYES));
    VssDbItem.UndoCheckout(workingFolder, 
            (int)(VSSFlags.VSSFLAG_GETNO | VSSFlags.VSSFLAG_DELYES| 
                VSSFlags.VSSFLAG_RECURSYES));


    VssDbItem.Add(workingFolder, "Created by DBScriptManager",
            (int) (VSSFlags.VSSFLAG_DELYES | VSSFlags.VSSFLAG_RECURSYES));


 

To delete temp folder that includes script files. 

 

 

string dirObject = String.Format("win32_Directory.Name='{0}'", targetDirectory);
            using (ManagementObject dir = new ManagementObject(dirObject))
            {
                dir.Get();
                ManagementBaseObject outParams = dir.InvokeMethod("Delete", null,
                null);
                // ReturnValue should be 0, else failure
                if (Convert.ToInt32(outParams.Properties["ReturnValue"].Value) != 0)
                {

                }
            }

 

Finally,

Accoring to new features of the SMO, Code can be improved. For the development environment,scheduled task could be used with proper changes of the program.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Levent Soyalp


Member

Occupation: Web Developer
Location: Turkey Turkey

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
GeneralError when running PinmemberRoel Schreurs0:52 22 Jul '08  
GeneralRe: Error when running Pinmembergetabdul22:47 9 Sep '08  
AnswerRe: Error when running Pinmemberx303:03 30 Sep '08  
GeneralSource code build failed PinmemberRoel Schreurs0:44 22 Jul '08  
GeneralA small bug PinmemberPhilipos Sakellaropoulos23:23 1 Mar '07  
Generalcan any one tell me the link to do all this work in c# Pinmembermian ghous3:37 23 Dec '06  
GeneralWe got a simpler way Pinmemberyonision16:53 14 Nov '06  
QuestionCompatibility with other SCC Pinmembernirisarri7:02 20 Apr '06  
AnswerRe: Compatibility with other SCC PinmemberLevent Soyalp22:17 30 Apr '06  
Generalwonderful work Pinmemberdenny.regehr@acxiom.com6:43 13 Apr '06  
GeneralRe: wonderful work PinmemberLevent Soyalp22:19 30 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Apr 2006
Editor:
Copyright 2006 by Levent Soyalp
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project