本文整理汇总了C#中Server.DetachDatabase方法的典型用法代码示例。如果您正苦于以下问题:C# Server.DetachDatabase方法的具体用法?C# Server.DetachDatabase怎么用?C# Server.DetachDatabase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Server
的用法示例。
在下文中一共展示了Server.DetachDatabase方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReplaceDatabase
public void ReplaceDatabase(string connectionString, string dbReplacementPath)
{
var connection = new SqlConnectionStringBuilder(connectionString);
var databaseName = connection.InitialCatalog;
if (!File.Exists(dbReplacementPath))
{
Log.Error($"Can't find file by path {dbReplacementPath}", this);
return;
}
ServerConnection serverConnection = null;
try
{
serverConnection = new ServerConnection(new SqlConnection(connectionString));
var server = new Server(serverConnection);
if (server.Databases[databaseName] != null)
{
server.KillAllProcesses(databaseName);
server.DetachDatabase(databaseName, true);
}
server.AttachDatabase(databaseName,new StringCollection() {dbReplacementPath}, AttachOptions.RebuildLog);
}
catch (Exception ex)
{
Log.Error(ex.Message, ex, this);
}
finally
{
serverConnection?.Disconnect();
}
}
示例2: DeleteDB
public void DeleteDB()
{
ServerConnection c = new ServerConnection(".", "sa", "hudongsoft");
Server s = new Server(c);
if (s.Databases[publicationDatabase] != null)
{
s.DetachDatabase(publicationDatabase, false);
}
}
示例3: DetachDatabase
private void DetachDatabase(Server server, string logicalName)
{
try
{
server.DetachDatabase(logicalName, false);
Application.Current.MainWindow.Close();
}
catch (Exception ex)
{
Helper.HandleError("Cannot detach the database. ", ex);
}
}
示例4: DetachDatabase
private static void DetachDatabase(string serverName, string databaseName)
{
Console.WriteLine(@"Attempting to detach database '{0}\{1}", serverName, databaseName);
// Initialise server object.
var server = new Server(serverName);
// Check if database is current attached to sqlexpress.
if (!server.Databases.Contains(databaseName))
{
Console.WriteLine("Server does not contain db '{0}'", databaseName);
return;
}
Database db = server.Databases[databaseName];
try
{
DataFile dataFile = db.FileGroups[0].Files[0];
if (!File.Exists(dataFile.FileName))
{
Console.WriteLine("'{0}' does not exist. Attempting to detach without altering db..",
dataFile.FileName);
}
db.DatabaseOptions.UserAccess = DatabaseUserAccess.Single;
}
catch (Exception e)
{
Console.WriteLine("Execution failure exception. Continuing.");
Console.WriteLine(e);
}
try
{
server.KillAllProcesses(db.Name);
}
catch (Exception e)
{
Console.WriteLine("Could not kill all processes");
Console.WriteLine(e);
}
Console.WriteLine("Altering database '{0}'", db.Name);
try
{
db.Alter(TerminationClause.FailOnOpenTransactions);
}
catch (Exception e)
{
Console.WriteLine("Could not alter the database.");
Console.WriteLine(e);
}
try
{
Console.WriteLine("Detaching existing database before restore ...");
server.DetachDatabase(db.Name, false);
}
catch (Exception e)
{
Console.WriteLine("Detach failed, let's continue anyways.");
Console.WriteLine(e);
}
}
示例5: CreateDatabase
public static void CreateDatabase(Identifier claimedId, string friendlyId, string databaseName) {
const string SqlFormat = @"
{0}
GO
EXEC [dbo].[AddUser] 'admin', 'admin', '{1}', '{2}'
GO
";
var removeSnippets = new string[] { @"
IF IS_SRVROLEMEMBER(N'sysadmin') = 1
BEGIN
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
EXECUTE sp_executesql N'ALTER DATABASE [$(DatabaseName)]
SET HONOR_BROKER_PRIORITY OFF
WITH ROLLBACK IMMEDIATE';
END
END
ELSE
BEGIN
PRINT N'The database settings cannot be modified. You must be a SysAdmin to apply these settings.';
END
GO" };
string databasePath = HttpContext.Current.Server.MapPath("~/App_Data/" + databaseName + ".mdf");
StringBuilder schemaSqlBuilder = new StringBuilder();
using (var sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(DefaultNamespace + ".CreateDatabase.sql"))) {
schemaSqlBuilder.Append(sr.ReadToEnd());
}
foreach (string remove in removeSnippets) {
schemaSqlBuilder.Replace(remove, string.Empty);
}
schemaSqlBuilder.Replace("$(Path1)", HttpContext.Current.Server.MapPath("~/App_Data/"));
schemaSqlBuilder.Replace("WEBROOT", databasePath);
schemaSqlBuilder.Replace("$(DatabaseName)", databaseName);
string sql = string.Format(CultureInfo.InvariantCulture, SqlFormat, schemaSqlBuilder, claimedId, "Admin");
var serverConnection = new ServerConnection(".\\sqlexpress");
try {
serverConnection.ExecuteNonQuery(sql);
} finally {
try {
var server = new Server(serverConnection);
server.DetachDatabase(databaseName, true);
} catch (FailedOperationException) {
}
serverConnection.Disconnect();
}
}
示例6: upload_DoWork
//.........这里部分代码省略.........
t.CopyData = true;
t.DestinationServer = destinationServer;
t.DestinationDatabase = destinationDatabase;
// if username or password entered so server must be create for mixed authentication
// else connect to server simple only with its name for windows authentication
t.DestinationLoginSecure = true; // for windows authentication
if (!String.IsNullOrEmpty(username) || !String.IsNullOrEmpty(pass))
{
t.DestinationLoginSecure = false;
t.DestinationLogin = username;
t.DestinationPassword = pass;
}
t.Options.ContinueScriptingOnError = true;
t.Options.IncludeIfNotExists = true;
t.DropDestinationObjectsFirst = true;
//t.UseDestinationTransaction = true; <<Check Here>> (long time exec)
if (createDb == true) t.CreateTargetDatabase = true;
progress = 10;
// Transfer Schema and Data
t.TransferData();
progress = 11;
#endregion
#region DestructEveryThing
progress = 12;
if (!serverMode)
{
server.DetachDatabase(sourceDatabase, false);
}
progress = 13;
#endregion
progress = 14;
MessageBox.Show("پایگاه داده ها با موفقیت کامل بر روی وب قرار گرفت", "ارسال موفق", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
#region DetachDb
if (attached && !serverMode)
{
try
{
Server server = new Server(sourceServer);
server.DetachDatabase(sourceDatabase, false);
}
catch (Exception exc)
{
MessageBox.Show("خطایی در هنگام حذف پایگاه داده های موقتی در همین سیستم رخ داده است", "خطا", MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show("خطای تکنیکی \n" + exc.Message + "\n" + exc.StackTrace, "خطا", MessageBoxButton.OK, MessageBoxImage.Error);
if (exc.InnerException.InnerException != null)
{
MessageBox.Show("خطای تکنیکی داخلی \n" + exc.InnerException.InnerException.Message, "خطا", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
#endregion
示例7: Detach
public void Detach(string database)
{
try
{
var cnn = new SqlConnection(_mConnectString);
cnn.Open();
var conn = new ServerConnection(cnn);
var myServer = new Server(conn);
myServer.KillAllProcesses(database);
myServer.DetachDatabase(database, true);
RaiseDetachCompleteFinishEventHander();
cnn.Close();
}
catch (Exception ex)
{
RaiseDetachErrorEventHander(ex.Message);
}
}