using System;
using System.Collections;
namespace dbutil
{
class Util
{
[STAThread]
static void Main(string[] args)
{
try
{
if (args.Length!=0)
{
Hashtable ht = new Hashtable();
foreach (string s in args)
{
string temp = s.Trim();
if ((s.IndexOf(-)!=0) && (s.IndexOf(/)!=0))
throw new CommandException();
string[] argitem = temp.Split(=);
if (argitem.Length>2)
throw new CommandException();
ht.Add(argitem[0].ToUpper().Remove(0,1), argitem.Length==1?null:argitem[1]);
}
Hashtable cmdArgs = ParseArgs(ht);
RunArgs(cmdArgs);
}
else
WriteHelp();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Press any key to continue.");
Console.Read();
}
static void WriteHelp()
{
Console.WriteLine("Attach/Detach a Database." +ArgHelpMsg);
}
static Hashtable ParseArgs(Hashtable ht)
{
if (ht.ContainsKey("?")) return null;
return GetArguments(ht);
}
static Hashtable GetArguments(Hashtable ht)
{
if (!ht.ContainsKey("DB")) throw new CommandException();
if ( ht.ContainsKey("D") && ht.ContainsKey("A") ) throw new CommandException();
Hashtable args = new Hashtable();
bool IsAttach = ht.ContainsKey("A");
ht.Remove("D");
ht.Remove("A");
if (IsAttach)
{
object filename = ht["F"];
if (filename == null) throw new CommandException();
args.Add("F", filename.ToString().Replace("\"",""));
}
ht.Remove("F");
object instance = ht["I"];
if (instance != null)
args.Add("I", instance.ToString().Replace("\"",""));
ht.Remove("I");
args.Add("DB", ht["DB"].ToString().Replace("\"",""));
ht.Remove("DB");
if (ht.Count != 0) throw new CommandException();
args.Add( IsAttach ? "A" : "D",null);
return args;
}
static void RunArgs(Hashtable args)
{
if (args == null)
{
WriteHelp();
return;
}
DBOperation(args);
}
static SQLDMO._SQLServer CreateServer(Hashtable args)
{
string instance = args["I"] == null ? "" : args["I"].ToString();
SQLDMO._SQLServer result = new SQLDMO.SQLServerClass();
result.LoginSecure = true;
result.Connect( instance,null,null);
return result;
}
static void DBOperation(Hashtable args)
{
SQLDMO._SQLServer srv = CreateServer(args);
string db = args["DB"].ToString();
if (args.ContainsKey("D"))
{
srv.DetachDB( args["DB"].ToString(), true);
Console.WriteLine("Database Detached");
return;
}
else
{
srv.AttachDBWithSingleFile(args["DB"].ToString(), args["F"].ToString());
Console.WriteLine("Database Attached");
}
srv.Close();
}
static public string ArgHelpMsg
{
get
{
string nl = Environment.NewLine;
string exename = AppDomain.CurrentDomain.FriendlyName.Replace(".exe","");
string msg = nl +"Usage:";
string temp = string.Format("{0}{1}{0}[{{-d}} | [-a]] {{-i=instancename}}{2}{0}{0}"+
"[-db=\"databasename\"] {{[-f=\"filename\"]}} {{-?}}", "\t", exename, Environment.NewLine);
msg += temp + nl;
msg += nl;
msg += "Options:"+nl;
msg += string.Format("{0}-d{0}Detach database. (default mode) ", "\t") + nl;
msg += string.Format("{0}-a{0}Attach database file.", "\t") +nl;
msg += string.Format("{0}-i{0}Name of MSDE Instance.{1}{0}{0}(Optional - " +
"use only if the MSDE is a named instance).", "\t", Environment.NewLine)+nl;
msg += string.Format("{0}-db{0}Required name of database on which to operate.", "\t")+nl;
msg += string.Format("{0}-f{0}File name to attach as database in -db option " +
"{1}{0}{0}(Required with -a option. ignored with -d option).", "\t", Environment.NewLine)+nl;
msg += string.Format("{0}-?{0}Display this message.", "\t") +nl;
return msg;
}
}
}
public class CommandException: ApplicationException
{
public CommandException():base("Invalid Command Line Arguments. . . " + Util.ArgHelpMsg)
{
}
}
}