码迷,mamicode.com
首页 > 其他好文 > 详细

Main(string[] args)

时间:2014-07-07 23:49:33      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:des   os   art   cti   for   io   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System.IO;


namespace ScriptSqlDatabase
{
class Program
{
private const string L_DOT_SQL = ".sql";

static void Main(string[] args)
{
string server = null;
string database = null;
string username = null;
string password = null;
string filename = null;

if (args.Length == 0)
{
Console.WriteLine(@"Options:
/s:server Required. For example: server\instance
/d:database Required.
/f:outpufile Optional. If not specified, will be set to {database}.sql.
/u:username Optional. If not specified, will use Windows authentication.
/p:password Optional.");
return;
}

foreach (string arg in args)
{
if (arg.StartsWith("/s:", StringComparison.CurrentCultureIgnoreCase))
{
server = arg.Substring(3);
}
else if (arg.StartsWith("/d:", StringComparison.CurrentCultureIgnoreCase))
{
database = arg.Substring(3);
}
else if (arg.StartsWith("/u:", StringComparison.CurrentCultureIgnoreCase))
{
username = arg.Substring(3);
}
else if (arg.StartsWith("/p:", StringComparison.CurrentCultureIgnoreCase))
{
password = arg.Substring(3);
}
else if (arg.StartsWith("/f:", StringComparison.CurrentCultureIgnoreCase)
|| string.Compare(arg, "/f", StringComparison.CurrentCultureIgnoreCase) == 0)
{
filename = arg.Substring(3);
}
else
{
Console.WriteLine("Ignoring invalid command line argument: {0}", arg);
}
}
if (server == null || database == null)
{
Console.WriteLine("No server or database specified, cannot continue.");
return;
}

if (string.IsNullOrEmpty(filename)) filename = database + L_DOT_SQL;
else
{
if (!filename.EndsWith(L_DOT_SQL, StringComparison.InvariantCultureIgnoreCase))
{
filename += L_DOT_SQL;
}
}
var startTime = DateTime.Now;

var c = new ServerConnection(server);
if (username != null)
{
c.LoginSecure = false;
c.Login = username;
c.Password = password;
}
else
c.LoginSecure = true;

Server smoServer;
Database db = null;
try
{
smoServer = new Server(c);
db = smoServer.Databases[database];
}
catch (Exception ex)
{
Console.WriteLine("Unable to connect to server {0}", server);
Console.WriteLine(ex.ToString());
return;
}

var scriptingOptions = new ScriptingOptions();
scriptingOptions.TargetServerVersion = SqlServerVersion.Version105;
scriptingOptions.IncludeDatabaseContext = true;
scriptingOptions.IncludeHeaders = true;
//scriptingOptions.IncludeIfNotExists = true;
scriptingOptions.AllowSystemObjects = false;
scriptingOptions.NoCollation = true;
scriptingOptions.Indexes = true;
scriptingOptions.Triggers = true;
scriptingOptions.DriAll = true;
scriptingOptions.ExtendedProperties = true;
scriptingOptions.AppendToFile = true;
scriptingOptions.ToFileOnly = true;
scriptingOptions.FileName = filename;
scriptingOptions.Encoding = Encoding.UTF8;

var targetVersionString = "(Unknown)";
switch(scriptingOptions.TargetServerVersion)
{
case SqlServerVersion.Version80:
targetVersionString = "SQL SERVER 2000";
break;
case SqlServerVersion.Version90:
targetVersionString = "SQL SERVER 2005";
break;
case SqlServerVersion.Version100:
targetVersionString = "SQL SERVER 2008";
break;
case SqlServerVersion.Version105:
targetVersionString = "SQL SERVER 2008 R2";
break;
}
var fmt = @"-- AUTO-GENERATED BY TOOL ScriptSqlDatabase on {0}
-- Target server version: {1}
-- File encoding: {2}

";
var s = string.Format(fmt, DateTime.Now, targetVersionString, scriptingOptions.Encoding.WebName);
File.WriteAllText(filename, s, scriptingOptions.Encoding);

try
{
Console.WriteLine("Scripting database");
db.Script(scriptingOptions);

var transfer = new Transfer(db);
transfer.Options = scriptingOptions;
transfer.CopySchema = true;
transfer.CopyAllObjects = true;
transfer.DestinationDatabase = database;
transfer.ScriptingProgress += (sender, e) =>
{
try
{
var pattern = string.Format(@"/{0}\[@Name=‘(.+?)‘ and @Schema=‘(.+?)‘\]", e.Current.Type);
var re = new Regex(pattern, RegexOptions.IgnoreCase);
//Console.WriteLine("{0}", e.Current.Value);
var match = re.Match(e.Current.Value);
if (match.Success)
{
Console.WriteLine("{0}/{1}: {3}.{2}", e.TotalCount, e.Total,
match.Groups[1].Value, match.Groups[2].Value);
}
}
catch (Exception ex)
{
Console.WriteLine("{0}: {0}", ex.GetType(), ex.ToString());
}
};
transfer.ScriptTransfer();
}
catch (Exception ex)
{
Console.WriteLine("{0}: {0}", ex.GetType(), ex.ToString());
return;
}

var endTime = DateTime.Now;
Console.WriteLine("Done! Time elapsed: {0}", endTime - startTime);
}

/*
private static void dumpScripts(System.Collections.Specialized.StringCollection x)
{
foreach (var s in x)
{
Console.WriteLine(s);
}
}*/

}
}

Main(string[] args),布布扣,bubuko.com

Main(string[] args)

标签:des   os   art   cti   for   io   

原文地址:http://www.cnblogs.com/wleycn/p/3812705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!