As i have share before the step by step to deploy MOSS SP2 blog have share about the great coding to help us to generate the share file. After try on this , i feel great!! is really very simple and usefull code for us to generate the batch file for script. Thanks to CJG.
The reasons i like to use to detach and attach the DB becuase :
- Is a very faster way to get the latest update information at our farm
- Auto generate the latest script ( no need manually create the script one by one)
- with the simple coding we can edit it accordingly to our need
You can also create a command line utility to do this:
- Open Visual Studio
- Click “File->New Project”
- Select “Console Application”
- For name, type “ContentDetachAttachScript”
- Copy the following into the program.cs file:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TextWriter tw = File.CreateText("C:/detachall.bat");
TextWriter tw2 = File.CreateText("C:/attachall.bat");
SPFarm farm = SPFarm.Local;
SPAlternateUrlCollectionManager mgr = farm.AlternateUrlCollections;
foreach (SPAlternateUrlCollection altColl in mgr )
{
foreach (SPAlternateUrl url in altColl)
{
if (url.UrlZone == SPUrlZone.Default)
{
try
{
SPSite site = new SPSite(url.IncomingUrl);
SPWeb root = site.RootWeb;
if (root.WebTemplate != "CENTRALADMIN")
{
//get the web application for the site collection
SPWebApplication webApp = site.WebApplication;
foreach (SPContentDatabase cd in webApp.ContentDatabases)
{
tw.WriteLine("stsadm -o deletecontentdb -url " + url.IncomingUrl + " -databasename " + cd.Name + " -databaseserver " + cd.Server);
tw2.WriteLine("stsadm -o addcontentdb -url " + url.IncomingUrl + " -databasename " + cd.Name + " -databaseserver " + cd.Server);
//Console.WriteLine("Content Database [" + cd.Name + "] was detached");
//cd.Delete();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
tw.Close();
tw2.Close();
Console.WriteLine("Press enter to close");
Console.ReadLine();
}
}
}
- Compile the program, press F6
- Copy the executable to your SharePoint Farm
- Run the executable
- Open the C:\detachall.bat file , this file will contain all the stsadm commands that will detach all your content databases
- Open the C:\attachall.bat file, this contains all the stsadm commands to reattach your databases (NOTE: you should attach one at a time in the later steps).
No comments:
Post a Comment