Project management practices [6] automatic Database Synchronization [using Visual Studio with source control system to synchronize database automaticall

Source: Internet
Author: User
Tags mssqlserver

In the previous project management practice [5] automatic compilation and publishing website, we explained how to use msbuild + robocopy + webdeployment to automatically compile and deploy the website. Today, let's take a look at it, how to Use msbuild + SVN to automatically synchronize databases.

First, put the database files and database log files in our project to a directory, put them in the startkitdb directory, and then create a text file named startkitdb under the directory, modify the extension to proj. In theory, any extension can be used, and then use notepad or otherProgramOpen the file, copy the following content to it, and save it.

<?XML
Version= "1.0"Encoding= "UTF-8"?>

<Project
Defaulttargets= "All"Xmlns= "Http://schemas.microsoft.com/developer/msbuild/2003">

<! --Msbuild community tasks needs to be introduced-->

<Import
Project= "$ (Msbuildextensionspath) \ msbuildcommunitytasks \ msbuild. Community. Tasks. Targets"/>

<Propertygroup>

<! --Database connection string, which can be modified as needed-->

<Connectionstring> Server =.; Integrated Security = true </Connectionstring>

</Propertygroup>

 

<Itemgroup>

<! --Database Files-->

<Dbfiles
Include= "Startkitdb. MDF; startkitdb_log.ldf"/>

</Itemgroup>

<Target
Name= "All">

<! --Restart sqlserver Service-->

<Servicecontroller
Servicename= "MSSQLServer"Action= "Restart"/>

<! --Detaching a database-->

<Exec
Command= "Osql-S.-e-n-Q& Quot;Exec sp_detach_db 'startkitdb', 'true'& Quot;"Ignoreexitcode= "False"/>

<! --Stop sqlserver Service-->

<Servicecontroller
Servicename= "MSSQLServer"Action= "Stop"/>

<! --Delete old database files-->

<Delete
Files= "C: \ startkitdb \ $ (dbfiles)"/>

<! --Copy the latest database file to the specified location-->

<Copy
Sourcefiles= "@ (Dbfiles )"Destinationfolder= "C: \ startkitdb"/>

<! --Start sqlserver Service-->

<Servicecontroller
Servicename= "MSSQLServer"Action= "Start"/>

<! --Append the copied latest database file to the database.-->

<Exec
Command= "Osql-S.-e-n-Q& Quot;Exec sp_attach_db @ dbname = n'startkitdb', @ filename1 = n'c: \ startkitdb. MDF ', @ filename2 = n'c: \ startkitdb \ startkitdb_log.ldf'& Quot;"Ignoreexitcode= "False"/>

</Target>

</Project>

Of course, if we want to: When we submit the latest database files and log files to the server, the old database will be automatically detached from the server, and the latest database will be appended, therefore, we need to use ccnet and SVN servers, so we must first store database files, log files, and startkitdb. the startkitdb folder of the proj file is included in our version control. However, if we simply add this file toCodeLibrary, when we submit database updates, the entire startkit project will be automatically compiled. At this time, our project may not be updated, therefore, we need to separate the database and project into two code libraries.

Tutorial 2. source code control
Add a code library [repository] startkitdb using visualsvn server. Then, use tortoisesvn to migrate the above three files to the svn server. Finally, according to the project management practice [3], add a project [project] On ccnet, and modify the corresponding file path according to the actual situation, configure the projectfile node value under Task msbuild as the startkitdb created above. proj file.

Note: In ccnet, do not move the database files on the svn server out of the same location as the precedingStartkitdb. projIn ProcessThe location is the same. Why? You can think about it first!
OK! You can submit the database to the svn server once. How can this problem be solved? Succeeded! Hey hey...

This method is suitable for use in the development process. If we have deployed a project or have already delivered it to the user, the user may have added and updated a lot of data, at this time, it is inconsistent with the latest version of SVN during development. Considering the security of user data, we may need to update the database script for the user, what should we do at this time to ensure the security of user data? In Visual Studio 2008, I compared the architecture [schema] AND DATA [data] of the two databases and explained them in detail. You can make a reference!

If you are not familiar with the content I have explained, we recommend that you read this series of tutorials from the beginning: the Project Management Practices series.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.